From 8716fbf49544d9d4eaade971b7d8b4f546a2962c Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Fri, 19 Feb 2021 17:47:54 +1100 Subject: [PATCH] improve sent message handling by setting the correct convoId at start --- ts/models/message.ts | 9 +++++++- ts/models/messageType.ts | 4 ++-- ts/receiver/attachments.ts | 8 +------- ts/receiver/dataMessage.ts | 26 ++++++++++-------------- ts/receiver/receiver.ts | 9 ++++---- ts/session/sending/MessageSentHandler.ts | 12 +++++++---- 6 files changed, 35 insertions(+), 33 deletions(-) diff --git a/ts/models/message.ts b/ts/models/message.ts index f260487b9..75bc5ffa1 100644 --- a/ts/models/message.ts +++ b/ts/models/message.ts @@ -41,6 +41,13 @@ export class MessageModel extends Backbone.Model { }) ); + if (!this.attributes.id) { + throw new Error('A message always needs to have an id.'); + } + if (!this.attributes.conversationId) { + throw new Error('A message always needs to have an conversationId.'); + } + // this.on('expired', this.onExpired); void this.setToExpire(); autoBind(this); @@ -239,7 +246,7 @@ export class MessageModel extends Backbone.Model { public getPropsForTimerNotification() { const timerUpdate = this.get('expirationTimerUpdate'); - if (!timerUpdate) { + if (!timerUpdate || !timerUpdate.source) { return null; } diff --git a/ts/models/messageType.ts b/ts/models/messageType.ts index 32e5aae69..c7139479a 100644 --- a/ts/models/messageType.ts +++ b/ts/models/messageType.ts @@ -37,7 +37,7 @@ export interface MessageAttributes { groupInvitation?: any; attachments?: any; contact?: any; - conversationId: any; + conversationId: string; errors?: any; flags?: number; hasAttachments: boolean; @@ -90,7 +90,7 @@ export interface MessageAttributesOptionals { groupInvitation?: any; attachments?: any; contact?: any; - conversationId: any; + conversationId: string; errors?: any; flags?: number; hasAttachments?: boolean; diff --git a/ts/receiver/attachments.ts b/ts/receiver/attachments.ts index f65d19b4a..91e5e1818 100644 --- a/ts/receiver/attachments.ts +++ b/ts/receiver/attachments.ts @@ -222,9 +222,7 @@ async function processGroupAvatar(message: MessageModel): Promise { export async function queueAttachmentDownloads( message: MessageModel -): Promise { - const { Whisper } = window; - +): Promise { let count = 0; count += await processNormalAttachments(message, message.get('attachments')); @@ -241,9 +239,5 @@ export async function queueAttachmentDownloads( if (count > 0) { await saveMessage(message.attributes); - - return true; } - - return false; } diff --git a/ts/receiver/dataMessage.ts b/ts/receiver/dataMessage.ts index 28824cc3b..82f59b594 100644 --- a/ts/receiver/dataMessage.ts +++ b/ts/receiver/dataMessage.ts @@ -491,32 +491,28 @@ function createSentMessage(data: MessageCreationData): MessageModel { isPublic, receivedAt, sourceDevice, - unidentifiedStatus, expirationStartTimestamp, destination, + message, } = data; - let unidentifiedDeliveries; - - if (unidentifiedStatus && unidentifiedStatus.length) { - sentTo = unidentifiedStatus.map((item: any) => item.destination); - const unidentified = _.filter(unidentifiedStatus, (item: any) => - Boolean(item.unidentified) - ); - // eslint-disable-next-line no-param-reassign - unidentifiedDeliveries = unidentified.map((item: any) => item.destination); - } - const sentSpecificFields = { - sent_to: sentTo, + sent_to: [], sent: true, - unidentifiedDeliveries: unidentifiedDeliveries || [], expirationStartTimestamp: Math.min( expirationStartTimestamp || data.timestamp || now, now ), }; + const messageGroupId = message?.group?.id; + let groupId = + messageGroupId && messageGroupId.length > 0 ? messageGroupId : null; + + if (groupId) { + groupId = PubKey.removeTextSecurePrefixIfNeeded(groupId); + } + const messageData = { source: UserUtils.getOurPubKeyStrFromCache(), sourceDevice, @@ -525,7 +521,7 @@ function createSentMessage(data: MessageCreationData): MessageModel { sent_at: timestamp, received_at: isPublic ? receivedAt : now, isPublic, - conversationId: destination, // conversation ID will might change later (if it is a group) + conversationId: groupId ?? destination, type: 'outgoing' as MessageModelType, ...sentSpecificFields, }; diff --git a/ts/receiver/receiver.ts b/ts/receiver/receiver.ts index 7507ccd05..9aef77f2a 100644 --- a/ts/receiver/receiver.ts +++ b/ts/receiver/receiver.ts @@ -194,14 +194,15 @@ export async function queueAllCached() { export async function queueAllCachedFromSource(source: string) { const items = await getAllFromCacheForSource(source); - items.forEach(async item => { + + // queue all cached for this source, but keep the order + await items.reduce(async (promise, item) => { + await promise; await queueCached(item); - }); + }, Promise.resolve()); } async function queueCached(item: any) { - const { textsecure } = window; - try { const envelopePlaintext = StringUtils.encode(item.envelope, 'base64'); const envelopeArray = new Uint8Array(envelopePlaintext); diff --git a/ts/session/sending/MessageSentHandler.ts b/ts/session/sending/MessageSentHandler.ts index 9e3b8ae8a..e7c7bb920 100644 --- a/ts/session/sending/MessageSentHandler.ts +++ b/ts/session/sending/MessageSentHandler.ts @@ -116,10 +116,14 @@ export class MessageSentHandler { // Handle the sync logic here if (shouldTriggerSyncMessage) { if (dataMessage) { - await fetchedMessage.sendSyncMessage( - dataMessage as SignalService.DataMessage, - sentMessage.timestamp - ); + try { + await fetchedMessage.sendSyncMessage( + dataMessage as SignalService.DataMessage, + sentMessage.timestamp + ); + } catch (e) { + window.log.warn('Got an error while trying to sendSyncMessage():', e); + } } } else if (shouldMarkMessageAsSynced) { fetchedMessage.set({ synced: true });