From 4294429bc2436050fa651f820d119f5070d4162f Mon Sep 17 00:00:00 2001 From: Alvaro <110414366+alvaro-signal@users.noreply.github.com> Date: Wed, 23 Nov 2022 13:52:36 -0700 Subject: [PATCH] Made StoryDataType sourceDevice required --- ts/services/storyLoader.ts | 20 ++++++++++++++++++++ ts/state/ducks/stories.ts | 3 ++- ts/test-electron/state/ducks/stories_test.ts | 4 ++++ ts/textsecure/MessageReceiver.ts | 1 + ts/textsecure/messageReceiverEvents.ts | 2 +- 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ts/services/storyLoader.ts b/ts/services/storyLoader.ts index 52643847dd..60dab944c2 100644 --- a/ts/services/storyLoader.ts +++ b/ts/services/storyLoader.ts @@ -95,6 +95,25 @@ export function getStoryDataFromMessageAttributes( attachment = getPropsForAttachment(attachment); } + // for a story, the message should always include the sourceDevice + // but some messages got saved without one in the past (sync-sent) + // we default those to some reasonable values that won't break the app + let sourceDevice: number; + if (message.sourceDevice !== undefined) { + sourceDevice = message.sourceDevice; + } else { + log.error('getStoryDataFromMessageAttributes: undefined sourceDevice'); + // storage user.getDevice() should always produce a value after registration + const ourDeviceId = window.storage.user.getDeviceId() ?? -1; + if (message.type === 'outgoing') { + sourceDevice = ourDeviceId; + } else if (message.type === 'incoming') { + sourceDevice = 1; + } else { + sourceDevice = -1; + } + } + return { attachment, messageId: message.id, @@ -114,6 +133,7 @@ export function getStoryDataFromMessageAttributes( 'timestamp', 'type', ]), + sourceDevice, expireTimer: message.expireTimer, expirationStartTimestamp: dropNull(message.expirationStartTimestamp), }; diff --git a/ts/state/ducks/stories.ts b/ts/state/ducks/stories.ts index 1dbbae68ef..a5c06e7d6c 100644 --- a/ts/state/ducks/stories.ts +++ b/ts/state/ducks/stories.ts @@ -74,7 +74,6 @@ export type StoryDataType = { | 'sendStateByConversationId' | 'source' | 'sourceUuid' - | 'sourceDevice' | 'storyDistributionListId' | 'timestamp' | 'type' @@ -82,6 +81,7 @@ export type StoryDataType = { // don't want the fields to be optional as in MessageAttributesType expireTimer: DurationInSeconds | undefined; expirationStartTimestamp: number | undefined; + sourceDevice: number; }; export type SelectedStoryDataType = { @@ -1378,6 +1378,7 @@ export function reducer( 'sendStateByConversationId', 'source', 'sourceUuid', + 'sourceDevice', 'storyDistributionListId', 'timestamp', 'type', diff --git a/ts/test-electron/state/ducks/stories_test.ts b/ts/test-electron/state/ducks/stories_test.ts index 1b5e0da24e..842cf8aac6 100644 --- a/ts/test-electron/state/ducks/stories_test.ts +++ b/ts/test-electron/state/ducks/stories_test.ts @@ -79,6 +79,7 @@ describe('both/state/ducks/stories', () => { readStatus: ReadStatus.Unread, timestamp: now - timestampDelta, type: 'story', + sourceDevice: 1, }; } @@ -545,6 +546,7 @@ describe('both/state/ducks/stories', () => { storyDistributionListId, timestamp: now - timestampDelta, type: 'story', + sourceDevice: 1, }; } @@ -930,6 +932,7 @@ describe('both/state/ducks/stories', () => { stories: [ { ...messageAttributes, + sourceDevice: 1, attachment: messageAttributes.attachments[0], messageId: messageAttributes.id, expireTimer: messageAttributes.expireTimer, @@ -984,6 +987,7 @@ describe('both/state/ducks/stories', () => { stories: [ { ...messageAttributes, + sourceDevice: 1, attachment: messageAttributes.attachments[0], messageId: messageAttributes.id, expireTimer: messageAttributes.expireTimer, diff --git a/ts/textsecure/MessageReceiver.ts b/ts/textsecure/MessageReceiver.ts index 99e082ce0c..1194cedfee 100644 --- a/ts/textsecure/MessageReceiver.ts +++ b/ts/textsecure/MessageReceiver.ts @@ -2138,6 +2138,7 @@ export default class MessageReceiver destinationUuid: envelope.destinationUuid.toString(), timestamp: envelope.timestamp, serverTimestamp: envelope.serverTimestamp, + device: envelope.sourceDevice, unidentifiedStatus: Array.from(sentToUuids).map( destinationUuid => ({ destinationUuid, diff --git a/ts/textsecure/messageReceiverEvents.ts b/ts/textsecure/messageReceiverEvents.ts index 902bbb4095..cbe7b2ae55 100644 --- a/ts/textsecure/messageReceiverEvents.ts +++ b/ts/textsecure/messageReceiverEvents.ts @@ -184,7 +184,7 @@ export type SentEventData = Readonly<{ destinationUuid?: string; timestamp?: number; serverTimestamp?: number; - device?: number; + device: number | undefined; unidentifiedStatus: ProcessedSent['unidentifiedStatus']; message: ProcessedDataMessage; isRecipientUpdate: boolean;