From 0cf616328e9b3b803e8a2a3e3bcf612f127f9986 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 12 Nov 2018 11:02:08 +1100 Subject: [PATCH] Fixed saving prekey bundle once friend request is accepted. Added option to delete conversation after it is declined. --- js/models/conversations.js | 14 +++++-- js/models/messages.js | 7 ++++ js/views/app_view.js | 6 ++- libtextsecure/message_receiver.js | 39 +++++++++----------- stylesheets/_modules.scss | 8 ++++ ts/components/conversation/FriendRequest.tsx | 33 +++++++++++------ 6 files changed, 69 insertions(+), 38 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 6cd0deb5f..3142a7b68 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -610,7 +610,14 @@ ); }, // This will add a message which will allow the user to reply to a friend request - async addFriendRequest(body, status = 'pending', type = 'incoming') { + async addFriendRequest(body, options = {}) { + const mergedOptions = { + status: 'pending', + type: 'incoming', + preKeyBundle: null, + ...options, + }; + if (this.isMe()) { window.log.info( 'refusing to send friend request to ourselves' @@ -647,9 +654,10 @@ unread: 1, from: this.id, to: this.ourNumber, - status, - requestType: type, + status: mergedOptions.status, + requestType: mergedOptions.type, body, + preKeyBundle: mergedOptions.preKeyBundle, }; const id = await window.Signal.Data.saveMessage(message, { diff --git a/js/models/messages.js b/js/models/messages.js index a92e17119..6f1444403 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -328,6 +328,12 @@ }); }; + const onDelete = async () => { + await window.Signal.Data.removeConversation(conversation.id, { + Conversation: Whisper.Conversation, + }) + }; + return { text: this.createNonBreakingLastSeparator(this.get('body')), source: this.findAndFormatContact(source), @@ -336,6 +342,7 @@ type, onAccept, onDecline, + onDelete, } }, findContact(phoneNumber) { diff --git a/js/views/app_view.js b/js/views/app_view.js index aa0a8c0ae..d1dd803c2 100644 --- a/js/views/app_view.js +++ b/js/views/app_view.js @@ -178,11 +178,13 @@ }); } }, - async showFriendRequest({ pubKey, message }) { + async showFriendRequest({ pubKey, message, preKeyBundle }) { const controller = window.ConversationController; const conversation = await controller.getOrCreateAndWait(pubKey, 'private'); if (conversation) { - conversation.addFriendRequest(message); + conversation.addFriendRequest(message, { + preKeyBundle: preKeyBundle || null, + }); } }, }); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 79de1bf0d..5ec970029 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -836,14 +836,15 @@ MessageReceiver.prototype.extend({ } }); }, - promptUserToAcceptFriendRequest(pubKey, message) { + promptUserToAcceptFriendRequest(pubKey, message, preKeyBundle) { window.Whisper.events.trigger('showFriendRequest', { pubKey, message, + preKeyBundle, }); }, // A handler function for when a friend request is accepted or declined - onFriendRequestUpdate(pubKey, message) { + async onFriendRequestUpdate(pubKey, message) { if (!message || !message.requestType || !message.status) return; // Update the conversation @@ -856,6 +857,15 @@ MessageReceiver.prototype.extend({ // Send our own prekeys as a response if (message.requestType === 'incoming' && message.status === 'accepted') { libloki.sendEmptyMessageWithPreKeys(pubKey); + + // Register the preKeys used for communication + if (message.preKeyBundle) { + await this.handlePreKeyBundleMessage( + pubKey, + message.preKeyBundle + ); + } + } console.log(`Friend request for ${pubKey} was ${message.status}`, message); }, @@ -871,28 +881,13 @@ MessageReceiver.prototype.extend({ if (!conversation) { this.promptUserToAcceptFriendRequest( envelope.source, - content.dataMessage.body + content.dataMessage.body, + content.preKeyBundle, ); return; - // if (accepted) { - // // send our own prekeys as a response - no need to wait - // libloki.sendEmptyMessageWithPreKeys(envelope.source); - // } else { - // console.log('friend request declined!'); - // return; - // } } } - //TODO: Check with sacha if this code needs to be called after friend request is accepted - - if (content.preKeyBundleMessage) { - await this.handlePreKeyBundleMessage( - envelope, - content.preKeyBundleMessage - ); - } - if (content.syncMessage) { return this.handleSyncMessage(envelope, content.syncMessage); } else if (content.dataMessage) { @@ -1114,7 +1109,7 @@ MessageReceiver.prototype.extend({ return this.removeFromCache(envelope); }, - async handlePreKeyBundleMessage(envelope, preKeyBundleMessage) { + async handlePreKeyBundleMessage(pubKey, preKeyBundleMessage) { const { preKeyId, signedKeyId } = preKeyBundleMessage; const [identityKey, preKey, signedKey, signature] = [ preKeyBundleMessage.identityKey, @@ -1123,12 +1118,11 @@ MessageReceiver.prototype.extend({ preKeyBundleMessage.signature, ].map(k => dcodeIO.ByteBuffer.wrap(k).toArrayBuffer()); - if (envelope.source != StringView.arrayBufferToHex(identityKey)) { + if (pubKey != StringView.arrayBufferToHex(identityKey)) { throw new Error( 'Error in handlePreKeyBundleMessage: envelope pubkey does not match pubkey in prekey bundle' ); } - const pubKey = envelope.source; return await libloki.savePreKeyBundleForNumber({ pubKey, @@ -1469,6 +1463,7 @@ textsecure.MessageReceiver = function MessageReceiverWrapper( this.getStatus = messageReceiver.getStatus.bind(messageReceiver); this.close = messageReceiver.close.bind(messageReceiver); this.onFriendRequestUpdate = messageReceiver.onFriendRequestUpdate.bind(messageReceiver); + this.handlePreKeyBundleMessage = messageReceiver.handlePreKeyBundleMessage.bind(messageReceiver); messageReceiver.connect(); }; diff --git a/stylesheets/_modules.scss b/stylesheets/_modules.scss index cd0a2c0c7..e4575b250 100644 --- a/stylesheets/_modules.scss +++ b/stylesheets/_modules.scss @@ -1057,6 +1057,14 @@ border-right: 1px solid $color-white-07; } +.module-friend-request__buttonContainer button:last-of-type { + border-right: 0; +} + +.module-friend-request__buttonContainer--incoming button:last-of-type { + border-right: 0; +} + .module-friend-request__text { padding-top: 6px; padding-bottom: 4px; diff --git a/ts/components/conversation/FriendRequest.tsx b/ts/components/conversation/FriendRequest.tsx index 88a15a0e9..8fe2164ef 100644 --- a/ts/components/conversation/FriendRequest.tsx +++ b/ts/components/conversation/FriendRequest.tsx @@ -21,6 +21,7 @@ interface Props { status: 'pending' | 'accepted' | 'declined'; onAccept: () => void; onDecline: () => void; + onDelete: () => void; } export class FriendRequest extends React.Component { @@ -84,26 +85,36 @@ export class FriendRequest extends React.Component { } public renderButtons() { - const { type, onAccept, onDecline } = this.props; - return ( -
- - -
- ) + const { status, type, onAccept, onDecline, onDelete } = this.props; + + if (type === 'incoming') { + if (status === 'pending') { + return ( +
+ + +
+ ); + } else if (status === 'declined') { + return ( +
+ +
+ ); + } + } + return null; } public render() { - const { type, status} = this.props; - - const shouldRenderButtons = (status === 'pending' && type === 'incoming'); + const { type} = this.props; return (
{this.renderContents()} - {shouldRenderButtons && this.renderButtons()} + {this.renderButtons()}