From 96aaa7851ddc57605c1fed9c889047f15ed67ad2 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Thu, 18 Jul 2019 13:28:36 +1000 Subject: [PATCH] Send broadcast message instead of empty message to trigger things like session reset and disappearing messages. Also fix small bug where deleting a contact kept the (now removed) conversation open --- js/models/conversations.js | 9 ++++-- js/models/messages.js | 4 +-- js/views/inbox_view.js | 8 +++++ libloki/api.js | 52 ++----------------------------- libtextsecure/message_receiver.js | 32 +++++++++---------- 5 files changed, 34 insertions(+), 71 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 0ac846898..0474f7a94 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -714,11 +714,14 @@ direction: 'incoming', status: ['pending', 'expired'], }); - window.libloki.api.sendFriendRequestAccepted(this.id); + window.libloki.api.sendBackgroundMessage(this.id); } }, // Our outgoing friend request has been accepted async onFriendRequestAccepted() { + if (this.isFriend()) { + return false; + } if (this.unlockTimer) clearTimeout(this.unlockTimer); if (this.hasSentFriendRequest()) { this.setFriendRequestStatus(FriendRequestStatusEnum.friends); @@ -1738,7 +1741,7 @@ await this.setSessionResetStatus(SessionResetEnum.request_received); // send empty message, this will trigger the new session to propagate // to the reset initiator. - await window.libloki.api.sendEmptyMessage(this.id); + await window.libloki.api.sendBackgroundMessage(this.id); }, isSessionResetReceived() { @@ -1774,7 +1777,7 @@ async onNewSessionAdopted() { if (this.get('sessionResetStatus') === SessionResetEnum.initiated) { // send empty message to confirm that we have adopted the new session - await window.libloki.api.sendEmptyMessage(this.id); + await window.libloki.api.sendBackgroundMessage(this.id); } await this.createAndStoreEndSessionMessage({ type: 'incoming', diff --git a/js/models/messages.js b/js/models/messages.js index b8e45969c..c8865c820 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -1986,9 +1986,7 @@ autoAccept = true; message.set({ friendStatus: 'accepted' }); await conversation.onFriendRequestAccepted(); - window.libloki.api.sendFriendRequestAccepted( - message.get('source') - ); + window.libloki.api.sendBackgroundMessage(message.get('source')); } else { await conversation.onFriendRequestReceived(); } diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 757b6db18..54762ecba 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -101,6 +101,14 @@ // Inbox const inboxCollection = getInboxCollection(); + // ConversationCollection + const conversations = getConversations(); + this.listenTo(conversations, 'remove', conversation => { + if (this.conversation_stack) { + this.conversation_stack.close(conversation); + } + }); + this.listenTo(inboxCollection, 'messageError', () => { if (this.networkStatusView) { this.networkStatusView.render(); diff --git a/libloki/api.js b/libloki/api.js index d67561954..e25d69cba 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -4,8 +4,8 @@ (function() { window.libloki = window.libloki || {}; - async function sendFriendRequestAccepted(pubKey) { - return sendEmptyMessage(pubKey, true); + async function sendBackgroundMessage(pubKey) { + return sendOnlineBroadcastMessage(pubKey); } async function broadcastOnlineStatus() { @@ -30,9 +30,6 @@ let type; if (!window.localLokiServer.isListening()) { - // Skip if server is not running AND we're not trying to ping a contact - if (!isPing) return; - type = textsecure.protobuf.LokiAddressMessage.Type.HOST_UNREACHABLE; } else { // clearnet change: getMyLokiAddress -> getMyClearIP @@ -66,51 +63,8 @@ await outgoingMessage.sendToNumber(pubKey); } - async function sendEmptyMessage(pubKey, sendContentMessage = false) { - const options = {}; - // send an empty message. - if (sendContentMessage) { - // The logic downstream will attach the prekeys and our profile. - await textsecure.messaging.sendMessageToNumber( - pubKey, // number - null, // messageText - [], // attachments - null, // quote - [], // preview - Date.now(), // timestamp - null, // expireTimer - null, // profileKey - options - ); - } else { - // empty content message - const content = new textsecure.protobuf.Content(); - - // will be called once the transmission succeeded or failed - const callback = res => { - if (res.errors.length > 0) { - res.errors.forEach(error => log.error(error)); - } else { - log.info('empty message sent successfully'); - } - }; - // send an empty message. The logic in ougoing_message will attach the prekeys. - const outgoingMessage = new textsecure.OutgoingMessage( - null, // server - Date.now(), // timestamp, - [pubKey], // numbers - content, // message - true, // silent - callback, // callback - options - ); - await outgoingMessage.sendToNumber(pubKey); - } - } - window.libloki.api = { - sendFriendRequestAccepted, - sendEmptyMessage, + sendBackgroundMessage, sendOnlineBroadcastMessage, broadcastOnlineStatus, }; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 026ac4032..b119b87ce 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -892,12 +892,27 @@ MessageReceiver.prototype.extend({ } return promise - .then(plaintext => { + .then(async plaintext => { const { isMe, isBlocked } = plaintext || {}; if (isMe || isBlocked) { this.removeFromCache(envelope); return null; } + if ( + envelope.type !== textsecure.protobuf.Envelope.Type.FRIEND_REQUEST + ) { + // If we got here there is a valid session, which meants friend request + // is complete (if it wasn't already) + if (conversation) { + const isFriendRequestAccept = await conversation.onFriendRequestAccepted(); + if (isFriendRequestAccept) { + await conversation.notifyFriendRequest( + envelope.source, + 'accepted' + ); + } + } + } this.updateCache(envelope, plaintext).catch(error => { window.log.error( @@ -1065,21 +1080,6 @@ MessageReceiver.prototype.extend({ return this.removeFromCache(envelope); } - if (!message.body) { - // Trigger conversation friend request event for empty message - if (conversation && !message.flags) { - const isFriendRequestAccept = await conversation.onFriendRequestAccepted(); - if (isFriendRequestAccept) { - await conversation.notifyFriendRequest( - envelope.source, - 'accepted' - ); - } - } - this.removeFromCache(envelope); - return null; - } - const ev = new Event('message'); ev.confirm = this.removeFromCache.bind(this, envelope); ev.data = {