From b502fcc3f9206b08299d24e1b408a3f5f02e1812 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 9 Jul 2020 16:26:25 +1000 Subject: [PATCH] clean code and add comments --- js/models/messages.js | 23 ++++++++++++++++++----- ts/test/test-utils/utils/message.ts | 4 ++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/js/models/messages.js b/js/models/messages.js index 5f714f83a..00efaed58 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -1209,6 +1209,10 @@ return errors[0][0]; }, + /** + * This function is called by inbox_view.js when a message was successfully sent for one device. + * So it might be called several times for the same message + */ async handleMessageSentSuccess(sentMessage) { let sentTo = this.get('sent_to') || []; @@ -1220,13 +1224,22 @@ sentMessage.group && sentMessage.group instanceof libsession.Types.OpenGroup; - // Handle the sync logic here - if ( + // We trigger a sync message only when the message is not to one of our devices, AND + // the message is not for an open group (there is no sync for opengroups, each device pulls all messages), AND + // if we did not sync or trigger a sync message for this specific message already + const shouldTriggerSyncMessage = !isOurDevice && !isOpenGroupMessage && !this.get('synced') && - !this.get('sentSync') - ) { + !this.get('sentSync'); + + // A message is synced if we triggered a sync message (sentSync) + // and the current message was sent to our device (so a sync message) + const shouldMarkMessageAsSynced = + isOurDevice && !isOpenGroupMessage && this.get('sentSync'); + + // Handle the sync logic here + if (shouldTriggerSyncMessage) { const contentDecoded = textsecure.protobuf.Content.decode( sentMessage.plainTextBuffer ); @@ -1234,7 +1247,7 @@ if (dataMessage) { this.sendSyncMessage(dataMessage); } - } else if (isOurDevice && this.get('sentSync')) { + } else if (shouldMarkMessageAsSynced) { this.set({ synced: true }); } if (!isOpenGroupMessage) { diff --git a/ts/test/test-utils/utils/message.ts b/ts/test/test-utils/utils/message.ts index 4aef39cd0..4d904b02b 100644 --- a/ts/test/test-utils/utils/message.ts +++ b/ts/test/test-utils/utils/message.ts @@ -111,4 +111,8 @@ export class MockConversation { return this.isPrimary ? this.id : generateFakePubKey().key; } + + public get(obj: string) { + return (this.attributes as any)[obj]; + } }