From 245ae721c741bc2aa3765edf7cfc97ff3b4bc130 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 14 Nov 2018 12:02:08 +1100 Subject: [PATCH] Fix checking for when a friend request was accepted. --- libtextsecure/message_receiver.js | 60 ++++++++++++++----------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index ed912030a..2d34aa7de 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -991,45 +991,39 @@ MessageReceiver.prototype.extend({ content.dataMessage.body, content.preKeyBundleMessage, ); + } else { + const keyExchangeComplete = conversation.isKeyExchangeCompleted(); + + // Check here if we received preKeys from the other user + // We are certain that other user accepted the friend request IF: + // - The message has a preKeyBundleMessage + // - We have an outgoing friend request that is pending + // The second check is crucial because it makes sure we don't save the preKeys of the incoming friend request (which is saved only when we press accept) + if (!keyExchangeComplete && content.preKeyBundleMessage) { + // Check for any outgoing friend requests + const pending = await conversation.getPendingFriendRequests('outgoing'); + const successful = pending.filter(p => !p.hasErrors()); + + // Save the key only if we have an outgoing friend request + const savePreKey = (successful.length > 0); + + // Save the pre key + if (savePreKey) { + await this.handlePreKeyBundleMessage( + envelope.source, + this.decodePreKeyBundleMessage(content.preKeyBundleMessage), + ); + + // Update the conversation + await conversation.onFriendRequestAccepted(); + } + } } // Exit early since the friend request reply will be a regular empty message return; } - // Check if our friend request got accepted - if (content.preKeyBundleMessage) { - // 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, - this.decodePreKeyBundleMessage(content.preKeyBundleMessage), - ); - - // Update the conversation - await conversation.onFriendRequestAccepted(); - - return; - } - } - if (content.syncMessage) { return this.handleSyncMessage(envelope, content.syncMessage); } else if (content.dataMessage) {