From fb8b0e1d4085054145a51a612c9e830a2a002e2d Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Nov 2018 14:25:20 +1100 Subject: [PATCH] Auto accept friend request if we have both incoming and outgoing friend requests. --- js/models/conversations.js | 26 +++++++++--------- js/models/messages.js | 54 ++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index e1d367eec..2db01dc45 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -554,7 +554,8 @@ ); // Update the UI - this.updateFriendRequestUI(); + await this.updatePendingFriendRequests(); + await this.updateFriendRequestUI(); }, async onFriendRequestSent() { // Check if we need to set the friend request expiry @@ -1285,20 +1286,17 @@ .map(request => this._removeMessage(request.id)) ); - // We also need to update any outgoing pending requests and set them to denied. - // when we get an incoming friend request. + // If we have an outgoing friend request then + // we auto accept the incoming friend request const outgoing = await this.getPendingFriendRequests('outgoing'); - await Promise.all( - outgoing.map(async request => { - if (request.hasErrors()) return; - - request.set({ friendStatus: 'declined' }); - await window.Signal.Data.saveMessage(request.attributes, { - Message: Whisper.Message, - }); - this.trigger('updateMessage', request); - }) - ); + if (outgoing.length > 0) { + const current = this.messageCollection.find(i => i.id === message.id); + if (current) { + await current.acceptFriendRequest(); + } else { + window.log.debug('onNewMessage: Failed to find incoming friend request'); + } + } // Trigger an update if we removed or updated messages if (outgoing.length > 0 || incoming.length > 0) diff --git a/js/models/messages.js b/js/models/messages.js index b7483f1fe..d42606aea 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -297,34 +297,42 @@ // It doesn't need anything right now! return {}; }, - getPropsForFriendRequest() { - const friendStatus = this.get('friendStatus') || 'pending'; - const direction = this.get('direction') || 'incoming'; + + async acceptFriendRequest() { + if (this.get('friendStatus') !== 'pending') return; const conversation = this.getConversation(); - const onAccept = async () => { - this.set({ friendStatus: 'accepted' }); - await window.Signal.Data.saveMessage(this.attributes, { - Message: Whisper.Message, - }); + this.set({ friendStatus: 'accepted' }); + await window.Signal.Data.saveMessage(this.attributes, { + Message: Whisper.Message, + }); - window.Whisper.events.trigger('friendRequestUpdated', { - pubKey: conversation.id, - ...this.attributes, - }); - }; + window.Whisper.events.trigger('friendRequestUpdated', { + pubKey: conversation.id, + ...this.attributes, + }); + }, + async declineFriendRequest() { + if (this.get('friendStatus') !== 'pending') return; + const conversation = this.getConversation(); - const onDecline = async () => { - this.set({ friendStatus: 'declined' }); - await window.Signal.Data.saveMessage(this.attributes, { - Message: Whisper.Message, - }); + this.set({ friendStatus: 'declined' }); + await window.Signal.Data.saveMessage(this.attributes, { + Message: Whisper.Message, + }); - window.Whisper.events.trigger('friendRequestUpdated', { - pubKey: conversation.id, - ...this.attributes, - }); - }; + window.Whisper.events.trigger('friendRequestUpdated', { + pubKey: conversation.id, + ...this.attributes, + }); + }, + getPropsForFriendRequest() { + const friendStatus = this.get('friendStatus') || 'pending'; + const direction = this.get('direction') || 'incoming'; + const conversation = this.getConversation(); + + const onAccept = () => this.acceptFriendRequest(); + const onDecline = () => this.declineFriendRequest() const onDeleteConversation = async () => { // Delete the whole conversation