From 5610ec5a9fc3a575da34a665bbbcd0e28930bf69 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 2 Jul 2020 11:29:02 +1000 Subject: [PATCH] move handling of message event sending to message.js --- js/models/messages.js | 72 ++++++++++++++++++++++++++++++++++++- js/views/inbox_view.js | 80 +++++------------------------------------- 2 files changed, 80 insertions(+), 72 deletions(-) diff --git a/js/models/messages.js b/js/models/messages.js index ea04cd15e..f10665e9f 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -990,7 +990,8 @@ }); }, - /* Uploads attachments, previews and quotes. + /** + * Uploads attachments, previews and quotes. * If body is too long then it is also converted to an attachment. * * @returns The uploaded data which includes: body, attachments, preview and quote. @@ -1215,6 +1216,75 @@ return errors[0][0]; }, + async handleMessageSentSuccess(sentMessage) { + const sentTo = this.get('sent_to') || []; + + const isOurDevice = window.libsession.Protocols.MultiDeviceProtocol.isOurDevice( + sentMessage.device + ); + + // Handle the sync logic here + if (!isOurDevice && !this.get('synced') && !this.get('sentSync')) { + const contentDecoded = textsecure.protobuf.Content.decode( + sentMessage.plainTextBuffer + ); + const { dataMessage } = contentDecoded; + this.sendSyncMessageOnly(dataMessage); + + this.set({ sentSync: true }); + } else if (isOurDevice && this.get('sentSync')) { + this.set({ synced: true }); + } + const primaryPubKey = await libsession.Protocols.MultiDeviceProtocol.getPrimaryDevice( + sentMessage.device + ); + this.set({ + sent_to: _.union(sentTo, [primaryPubKey.key]), + sent: true, + expirationStartTimestamp: Date.now(), + // unidentifiedDeliveries: result.unidentifiedDeliveries, + }); + + await window.Signal.Data.saveMessage(this.attributes, { + Message: Whisper.Message, + }); + this.getConversation().updateLastMessage(); + + this.trigger('sent', this); + }, + + async handleMessageSentFailure(sentMessage, error) { + if (error instanceof Error) { + this.saveErrors(error); + if (error.name === 'SignedPreKeyRotationError') { + await window.getAccountManager().rotateSignedPreKey(); + } else if (error.name === 'OutgoingIdentityKeyError') { + const c = ConversationController.get(sentMessage.device); + await c.getProfiles(); + } + } + + const expirationStartTimestamp = Date.now(); + if ( + sentMessage.device === window.textsecure.storage.user.getNumber() && + !this.get('sync') + ) { + this.set({ sentSync: false }); + } + this.set({ + sent: true, + expirationStartTimestamp, + // unidentifiedDeliveries: result.unidentifiedDeliveries, + }); + await window.Signal.Data.saveMessage(this.attributes, { + Message: Whisper.Message, + }); + this.trigger('change', this); + + this.getConversation().updateLastMessage(); + this.trigger('done'); + }, + getConversation() { // This needs to be an unsafe call, because this method is called during // initial module setup. We may be in the middle of the initial fetch to diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 96a343342..0e4c89681 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -10,8 +10,6 @@ Whisper, textsecure, Signal, - libsession, - _ */ // eslint-disable-next-line func-names @@ -299,87 +297,27 @@ const msg = conv.messageCollection.models.find( convMsg => convMsg.id === tmpMsg.id ); - return { conv, msg }; + return { msg }; }, - async handleMessageSentSuccess(m) { - const fetchedData = await this.fetchHandleMessageSentData(m); + async handleMessageSentSuccess(sentMessage) { + const fetchedData = await this.fetchHandleMessageSentData(sentMessage); if (!fetchedData) { return; } - const { msg, conv } = fetchedData; + const { msg } = fetchedData; - const sentTo = msg.get('sent_to') || []; - - const isOurDevice = window.libsession.Protocols.MultiDeviceProtocol.isOurDevice( - m.device - ); - - // Handle the sync logic here - if (!isOurDevice && !msg.get('synced') && !msg.get('sentSync')) { - const contentDecoded = textsecure.protobuf.Content.decode( - m.plainTextBuffer - ); - const { dataMessage } = contentDecoded; - msg.sendSyncMessageOnly(dataMessage); - - msg.set({ sentSync: true }); - } else if (isOurDevice && msg.get('sentSync')) { - msg.set({ synced: true }); - } - const primaryPubKey = await libsession.Protocols.MultiDeviceProtocol.getPrimaryDevice( - m.device - ); - msg.set({ - sent_to: _.union(sentTo, [primaryPubKey.key]), - sent: true, - expirationStartTimestamp: Date.now(), - // unidentifiedDeliveries: result.unidentifiedDeliveries, - }); - - await window.Signal.Data.saveMessage(msg.attributes, { - Message: Whisper.Message, - }); - conv.updateLastMessage(); - - msg.trigger('sent', msg); + msg.handleMessageSentSuccess(sentMessage); }, - async handleMessageSentFailure(m, error) { - const fetchedData = await this.fetchHandleMessageSentData(m); + async handleMessageSentFailure(sentMessage, error) { + const fetchedData = await this.fetchHandleMessageSentData(sentMessage); if (!fetchedData) { return; } - const { msg, conv } = fetchedData; - if (error instanceof Error) { - msg.saveErrors(error); - if (error.name === 'SignedPreKeyRotationError') { - await window.getAccountManager().rotateSignedPreKey(); - } else if (error.name === 'OutgoingIdentityKeyError') { - const c = ConversationController.get(m.device); - await c.getProfiles(); - } - } - - const expirationStartTimestamp = Date.now(); - if ( - m.device === window.textsecure.storage.user.getNumber() && - !msg.get('sync') - ) { - msg.set({ sentSync: false }); - } - msg.set({ - sent: true, - expirationStartTimestamp, - // unidentifiedDeliveries: result.unidentifiedDeliveries, - }); - await window.Signal.Data.saveMessage(msg.attributes, { - Message: Whisper.Message, - }); - msg.trigger('change', msg); + const { msg } = fetchedData; - conv.updateLastMessage(); - msg.trigger('done'); + await msg.handleMessageSentFailure(sentMessage, error); }, startConnectionListener() {