From 6aab900da2385df7e2fe3dc9307746e4c7560866 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 13 Nov 2018 12:25:50 +1100 Subject: [PATCH] Handle friend request updated. --- js/models/conversations.js | 29 +++++++++++++++++++++------ libtextsecure/message_receiver.js | 33 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 7ae0d390e..1cebf227a 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -120,6 +120,7 @@ this.on('change:profileKey', this.onChangeProfileKey); // Listening for out-of-band data updates + this.on('updateMessage', this.updateAndMerge); this.on('delivered', this.updateAndMerge); this.on('read', this.updateAndMerge); this.on('expiration-change', this.updateAndMerge); @@ -466,19 +467,35 @@ } }, async onFriendRequestAccepted() { - this.trigger('disable:input', false); - this.trigger('change:placeholder', 'chat'); this.set({ friendRequestStatus: null, + keyExchangeCompleted: true, }); await window.Signal.Data.updateConversation(this.id, this.attributes, { Conversation: Whisper.Conversation, }); + + // Enable the text inputs early + this.updateTextInputState(); + + // Update any pending outgoing messages + const pending = await this.getPendingFriendRequests('outgoing'); + for (const request of pending) { + // Only update successfully sent requests + if (request.hasErrors()) continue; + + request.set({ friendStatus: 'accepted' }); + await window.Signal.Data.saveMessage(request.attributes, { + Message: Whisper.Message, + }); + this.trigger('updateMessage', request); + } + + await this.updatePendingFriendRequests(); }, async onFriendRequestTimedOut() { - this.trigger('disable:input', false); - this.trigger('change:placeholder', 'friend-request'); + this.updateTextInputState(); const friendRequestStatus = this.getFriendRequestStatus(); friendRequestStatus.allowSending = true; @@ -500,8 +517,8 @@ const delayMs = 60 * 60 * 1000 * friendRequestLockDuration; friendRequestStatus.unlockTimestamp = Date.now() + delayMs; - this.trigger('disable:input', true); - this.trigger('change:placeholder', 'disabled'); + // Update the text input state + this.updateTextInputState(); this.set({ friendRequestStatus }); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 07a4b65d2..c61af47df 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -987,6 +987,39 @@ MessageReceiver.prototype.extend({ } } + // Check if our friend request got accepted + if (content.preKeyBundle) { + // By default we don't want to save the preKey + let savePreKey = false; + + // The conversation + let conversation = null; + + try { + conversation = ConversationController.get(envelope.source); + + // We only want to save the preKey if we have a outgoing friend request which is pending + const pending = await conversation.getPendingFriendRequests('outgoing'); + const successful = pending.filter(p => !p.hasErrors()); + + // Save the key only if we have an outgoing friend request + savePreKey = (successful.length > 0); + } catch (e) {} + + // Save the pre key if we have a conversation + if (savePreKey && conversation) { + await this.handlePreKeyBundleMessage( + envelope.source, + content.preKeyBundle + ); + + // Update the conversation + await conversation.onFriendRequestAccepted(); + + return; + } + } + if (content.syncMessage) { return this.handleSyncMessage(envelope, content.syncMessage); } else if (content.dataMessage) {