From d5fafd4d78f6194152f02a0d5b0ab346d302b3d1 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 12 Nov 2018 15:36:46 +1100 Subject: [PATCH] refactor friend request message props --- js/models/conversations.js | 23 ++++++++++++++++++++--- js/models/messages.js | 4 ++-- libtextsecure/message_receiver.js | 6 +++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index daa73ea22..8074d374a 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -243,6 +243,17 @@ return false; }, + async getPendingFriendRequests(direction) { + // Theoretically all ouur messages could be friend requests, thus we have to unfortunately go through each one :( + // We are most likely to find the friend request in the more recent conversations first + const messages = await window.Signal.Data.getMessagesByConversation(this.id, { + MessageCollection: Whisper.MessageCollection, + limit: Number.MAX_VALUE, + }).reverse(); + + // Get the messages that are matching the direction and the friendStatus + return messages.filter(m => (m.direction === direction && m.friendStatus === 'pending')); + }, getPropsForListItem() { const result = { ...this.format(), @@ -420,6 +431,10 @@ } this.set({ keyExchangeCompleted: completed }); + + await window.Signal.Data.updateConversation(this.id, this.attributes, { + Conversation: Whisper.Conversation, + }); }, getFriendRequestStatus() { return this.get('friendRequestStatus'); @@ -667,7 +682,7 @@ async addFriendRequest(body, options = {}) { const mergedOptions = { status: 'pending', - type: 'incoming', + direction: 'incoming', preKeyBundle: null, ...options, }; @@ -708,8 +723,8 @@ unread: 1, from: this.id, to: this.ourNumber, - status: mergedOptions.status, - requestType: mergedOptions.type, + friendStatus: mergedOptions.status, + direction: mergedOptions.direction, body, preKeyBundle: mergedOptions.preKeyBundle, }; @@ -936,6 +951,8 @@ now ); + // TODO: Maybe create the friend request here? + // TODO: Make sure we're not adding duplicate messages if keys haven't been exchanged const messageWithSchema = await upgradeMessageSchema({ type: 'outgoing', body, diff --git a/js/models/messages.js b/js/models/messages.js index 998ab3c03..0c5be577f 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -297,8 +297,8 @@ getPropsForFriendRequest() { const source = this.get('from'); const target = this.get('to'); - const status = this.get('status') || 'pending'; - const type = this.get('requestType') || 'incoming'; + const status = this.get('friendStatus') || 'pending'; + const type = this.get('direction') || 'incoming'; const conversation = this.getConversation(); // I.e do we send a network request from the model? or call a function in the conversation to send the new status diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index c8277935d..c643ee850 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -943,7 +943,7 @@ MessageReceiver.prototype.extend({ }, // A handler function for when a friend request is accepted or declined async onFriendRequestUpdate(pubKey, message) { - if (!message || !message.requestType || !message.status) return; + if (!message || !message.requestType || !message.friendStatus) return; // Update the conversation const conversation = ConversationController.get(pubKey); @@ -953,7 +953,7 @@ MessageReceiver.prototype.extend({ } // Send our own prekeys as a response - if (message.requestType === 'incoming' && message.status === 'accepted') { + if (message.requestType === 'incoming' && message.friendStatus === 'accepted') { libloki.sendEmptyMessageWithPreKeys(pubKey); // Register the preKeys used for communication @@ -965,7 +965,7 @@ MessageReceiver.prototype.extend({ } } - console.log(`Friend request for ${pubKey} was ${message.status}`, message); + console.log(`Friend request for ${pubKey} was ${message.friendStatus}`, message); }, async innerHandleContentMessage(envelope, plaintext) { const content = textsecure.protobuf.Content.decode(plaintext);