From 5db97950761d134464dcbb531a41b06dfb591530 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 19 May 2020 17:19:09 +1000 Subject: [PATCH] Accepting FRs to all devices --- js/models/conversations.js | 103 ++++++++++++++++++++++++++++++------- js/models/messages.js | 21 ++++++-- 2 files changed, 101 insertions(+), 23 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 6be3aef1b..b18c15b95 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -4,6 +4,7 @@ log, i18n, Backbone, + libloki, ConversationController, MessageController, storage, @@ -245,6 +246,12 @@ this.messageCollection.forEach(m => m.trigger('change')); }, async acceptFriendRequest() { + + console.log(`[vince][fr] accepting friend request from conversations.js`); + console.log('[vince][fr] this.id:', this.id); + console.log(`[vince][fr] primary pubkey`, this.getPrimaryDevicePubKey()); + + // Friend request messages are always send to primary device conversation const messages = await window.Signal.Data.getMessagesByConversation( this.id, { @@ -253,6 +260,24 @@ type: 'friend-request', } ); + + const priamryMessages = await window.Signal.Data.getMessagesByConversation( + this.getPrimaryDevicePubKey(), + { + limit: 1, + MessageCollection: Whisper.MessageCollection, + type: 'friend-request', + } + ); + + + + console.log(`[vince][fr] messages: `, messages); + console.log(`[vince][fr] Primary messages: `, priamryMessages); + + + + // LAST MODEL IS POINTING TO THE PRIMARY DEVICE'S LASTMODEL. NOT SECONDARY FOR A2 --> B const lastMessageModel = messages.at(0); if (lastMessageModel) { lastMessageModel.acceptFriendRequest(); @@ -970,35 +995,75 @@ }); }, async respondToAllFriendRequests(options) { + + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + console.log(`[vince][fr] respondToAllFriendRequests: IM BIENG CALLLLEDED`); + const { response, status, direction = null } = options; // Ignore if no response supplied if (!response) { return; } - const primaryConversation = ConversationController.get( + + // const primaryConversation = ConversationController.get( + // this.getPrimaryDevicePubKey() + // ); + + // // Should never happen + // if (!primaryConversation) { + // return; + // } + // const pending = await primaryConversation.getFriendRequests( + // direction, + // status + // ); + // await Promise.all( + // pending.map(async request => { + // if (request.hasErrors()) { + // return; + // } + + // request.set({ friendStatus: response }); + // await window.Signal.Data.saveMessage(request.attributes, { + // Message: Whisper.Message, + // }); + // primaryConversation.trigger('updateMessage', request); + // }) + // ); + + // Accept FRs from all the user's devices + const allDevices = await libloki.storage.getAllDevicePubKeysForPrimaryPubKey( this.getPrimaryDevicePubKey() ); - // Should never happen - if (!primaryConversation) { + + const allConversationsWithUser = allDevices.map(d => ConversationController.get(d)); + + + console.log(`[vince][fr] allDevices: `, allDevices); + console.log('[vince][fr] allConversationsWithUser:', allConversationsWithUser); + + + + + if (!allDevices.length) { return; } - const pending = await primaryConversation.getFriendRequests( - direction, - status - ); - await Promise.all( - pending.map(async request => { - if (request.hasErrors()) { - return; - } - request.set({ friendStatus: response }); - await window.Signal.Data.saveMessage(request.attributes, { - Message: Whisper.Message, - }); - primaryConversation.trigger('updateMessage', request); - }) - ); + const pendingRequests = allConversationsWithUser.map(async c => { + await c.getFriendRequests(direction, status); + }); + + console.log(`[vince][fr] Pending requests: `, pendingRequests); + + + + }, async respondToAllPendingFriendRequests(options) { return this.respondToAllFriendRequests({ diff --git a/js/models/messages.js b/js/models/messages.js index 516d8f1f8..6d44e2b22 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -415,10 +415,18 @@ }, async acceptFriendRequest() { + // THIS FUNCTION RUNS WHEN "ACCEPT" BUTTON IN CHAT IS CLICKED + if (this.get('friendStatus') !== 'pending') { return; } + const conversation = await this.getSourceDeviceConversation(); + + console.log('[vince][fr] conversation:', conversation); + console.log('[vince][fr] sourceDeviceConversation():', await this.getSourceDeviceConversation()); + + // If we somehow received an old friend request (e.g. after having restored // from seed, we won't be able to accept it, we should initiate our own // friend request to reset the session: @@ -2548,11 +2556,16 @@ // We need to map the original message source to the primary device // only map to primary device if this is NOT a friend request. // Otherwise you can enter a stalemate. - const conditionalSource = message.get('type') === 'friend-request' - ? source - : primarySource; + + // const conditionalSource = message.get('type') === 'friend-request' + // ? source + // : primarySource; + // if (source !== ourNumber) { + // message.set({ source: conditionalSource }); + // } + if (source !== ourNumber) { - message.set({ source: conditionalSource }); + message.set({ primarySource }); } const id = await window.Signal.Data.saveMessage(message.attributes, {