diff --git a/js/models/conversations.js b/js/models/conversations.js index 6efffe316..e1d367eec 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -79,6 +79,7 @@ return { unreadCount: 0, verified: textsecure.storage.protocol.VerifiedStatus.DEFAULT, + isFriend: false, keyExchangeCompleted: false, blockInput: false, unlockTimestamp: null, // Timestamp used for expiring friend requests. @@ -481,14 +482,8 @@ return (incoming.length > 0 || successfulOutgoing.length > 0); }, - async isFriend() { - // We are a friend IF: - // - We have the preKey bundle of the user OR - // - We have a session with the user - const preKeys = await window.Signal.Data.getContactPreKeyByIdentityKey(this.id); - // const session = await window.Signal.Data.getSessionsByNumber(this.id); - - return !!preKeys; + isFriend() { + return this.get('isFriend'); }, // Update any pending friend requests for the current user async updateFriendRequestUI() { @@ -496,8 +491,7 @@ this.updateTextInputState(); // We only update our friend requests if we have the user as a friend - const isFriend = await this.isFriend(); - if (!isFriend) return; + if (!this.isFriend()) return; // Update any pending outgoing messages const pending = await this.getPendingFriendRequests('outgoing'); @@ -520,6 +514,16 @@ if (pending.length > 0) this.notifyFriendRequest(this.id, 'accepted') }, + async onFriendRequestAccepted() { + if (!this.isFriend()) { + this.set({ isFriend: true }); + await window.Signal.Data.updateConversation(this.id, this.attributes, { + Conversation: Whisper.Conversation, + }); + } + + await this.updateFriendRequestUI(); + }, async onFriendRequestTimeout() { // Unset the timer if (this.unlockTimer) @@ -555,8 +559,7 @@ async onFriendRequestSent() { // Check if we need to set the friend request expiry const unlockTimestamp = this.get('unlockTimestamp'); - const isFriend = await this.isFriend(); - if (!isFriend && !unlockTimestamp) { + if (!this.isFriend() && !unlockTimestamp) { // Expire the messages after 72 hours const hourLockDuration = 72; const ms = 60 * 60 * 1000 * hourLockDuration; @@ -963,8 +966,7 @@ let messageWithSchema = null; // If we are a friend then let the user send the message normally - const isFriend = await this.isFriend(); - if (isFriend) { + if (this.isFriend()) { messageWithSchema = await upgradeMessageSchema({ type: 'outgoing', body, @@ -1101,8 +1103,7 @@ }, async updateTextInputState() { // Check if we need to disable the text field - const isFriend = await this.isFriend(); - if (!isFriend) { + if (!this.isFriend()) { // Disable the input if we're waiting for friend request approval const waiting = await this.waitingForFriendRequestApproval(); if (waiting) { diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 2ddd81fe1..360764446 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1020,12 +1020,13 @@ MessageReceiver.prototype.extend({ ); } - // Send a reply back - libloki.sendEmptyMessageWithPreKeys(pubKey); - + // Accept the friend request if (conversation) { - await conversation.updateFriendRequestUI(); + await conversation.onFriendRequestAccepted(); } + + // Send a reply back + libloki.sendEmptyMessageWithPreKeys(pubKey); } window.log.info(`Friend request for ${pubKey} was ${message.friendStatus}`, message); }, @@ -1047,10 +1048,12 @@ MessageReceiver.prototype.extend({ // ref: libsignal-protocol.js:36120 envelope.type === textsecure.protobuf.Envelope.Type.PREKEY_BUNDLE ) { - // If we get a cipher text and we are friends then we can mark keys as exchanged - if (conversation && conversation.isFriend()) { + // We know for sure that keys are exhanged + if (conversation) { await conversation.setKeyExchangeCompleted(true); - await conversation.updateFriendRequestUI(); + + // TODO: We should probably set this based on the PKB type + await conversation.onFriendRequestAccepted(); } }