From accd16cae1e2c9890fd167642ba37e38a5d00a9a Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Wed, 6 Nov 2019 11:49:27 +1100 Subject: [PATCH] Trigger friend request received update for the correct device conversation, throttle the sending of automated friend requests and don't send them to devices which sent *us* a friend request --- js/models/conversations.js | 7 +++++++ js/models/messages.js | 10 +++++++++- libtextsecure/outgoing_message.js | 24 ++++++++++++++++++++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 1fd596829..541a7c434 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -931,6 +931,13 @@ } await this.setFriendRequestStatus(FriendRequestStatusEnum.requestSent); }, + friendRequestTimerIsExpired() { + const unlockTimestamp = this.get('unlockTimestamp'); + if (unlockTimestamp && unlockTimestamp > Date.now()) { + return false; + } + return true; + }, setFriendRequestExpiryTimeout() { if (this.isFriend()) { return; diff --git a/js/models/messages.js b/js/models/messages.js index 7c88423d9..c9ec73246 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -361,7 +361,7 @@ if (this.get('friendStatus') !== 'pending') { return; } - const conversation = this.getConversation(); + const conversation = await this.getSourceDeviceConversation(); this.set({ friendStatus: 'accepted' }); await window.Signal.Data.saveMessage(this.attributes, { @@ -1119,6 +1119,14 @@ // the database. return ConversationController.getUnsafe(this.get('conversationId')); }, + getSourceDeviceConversation() { + // This gets the conversation of the device that sent this message + // while getConversation will return the primary device conversation + return ConversationController.getOrCreateAndWait( + this.get('source'), + 'private' + ); + }, getIncomingContact() { if (!this.isIncoming()) { return null; diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index aea4db575..a326596cd 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -355,10 +355,23 @@ OutgoingMessage.prototype = { } catch (e) { // do nothing } - // TODO: Make sure we retry sending friend request messages to all our friends - if (conversation && !conversation.isFriend()) { - isMultiDeviceRequest = true; - thisDeviceMessageType = 'friend-request'; + if ( + conversation && + !conversation.isFriend() && + !conversation.hasReceivedFriendRequest() + ) { + // We want to send an automated friend request if: + // - We aren't already friends + // - We haven't received a friend request from this device + // - We haven't sent a friend request recently + if (conversation.friendRequestTimerIsExpired()) { + isMultiDeviceRequest = true; + thisDeviceMessageType = 'friend-request'; + } else { + // Throttle automated friend requests + this.successfulNumbers.push(devicePubKey); + return null; + } } } @@ -456,6 +469,9 @@ OutgoingMessage.prototype = { .then(async outgoingObjects => { // TODO: handle multiple devices/messages per transmit const promises = outgoingObjects.map(async outgoingObject => { + if (!outgoingObject) { + return; + } const destination = outgoingObject.pubKey; try { const socketMessage = await this.wrapInWebsocketMessage(