diff --git a/ts/background.ts b/ts/background.ts index 1fe684fc0b95..5af76d3748e0 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -83,7 +83,8 @@ import type { ConfigurationEvent, DecryptionErrorEvent, DeliveryEvent, - EnvelopeEvent, + EnvelopeQueuedEvent, + EnvelopeUnsealedEvent, ErrorEvent, FetchLatestEvent, GroupEvent, @@ -339,8 +340,12 @@ export async function startApp(): Promise { } messageReceiver.addEventListener( - 'envelope', - queuedEventListener(onEnvelopeReceived, false) + 'envelopeUnsealed', + queuedEventListener(onEnvelopeUnsealed, false) + ); + messageReceiver.addEventListener( + 'envelopeQueued', + queuedEventListener(onEnvelopeQueued, false) ); messageReceiver.addEventListener( 'message', @@ -2430,9 +2435,15 @@ export async function startApp(): Promise { { leading: false } ); - async function onEnvelopeReceived({ + async function onEnvelopeQueued({ envelope, - }: EnvelopeEvent): Promise { + }: EnvelopeQueuedEvent): Promise { + throttledSetInboxEnvelopeTimestamp(envelope.serverTimestamp); + } + + async function onEnvelopeUnsealed({ + envelope, + }: EnvelopeUnsealedEvent): Promise { throttledSetInboxEnvelopeTimestamp(envelope.serverTimestamp); const ourUuid = window.textsecure.storage.user.getUuid()?.toString(); @@ -2441,7 +2452,7 @@ export async function startApp(): Promise { window.ConversationController.maybeMergeContacts({ e164: envelope.source, aci: envelope.sourceUuid, - reason: `onEnvelopeReceived(${envelope.timestamp})`, + reason: `onEnvelopeUnsealed(${envelope.timestamp})`, }); if (mergePromises.length > 0) { diff --git a/ts/textsecure/MessageReceiver.ts b/ts/textsecure/MessageReceiver.ts index 7e74a9152c4c..28559e2b0589 100644 --- a/ts/textsecure/MessageReceiver.ts +++ b/ts/textsecure/MessageReceiver.ts @@ -88,7 +88,8 @@ import type { } from './Types.d'; import { EmptyEvent, - EnvelopeEvent, + EnvelopeQueuedEvent, + EnvelopeUnsealedEvent, ProgressEvent, TypingEvent, ErrorEvent, @@ -631,8 +632,13 @@ export default class MessageReceiver ): void; public override addEventListener( - name: 'envelope', - handler: (ev: EnvelopeEvent) => void + name: 'envelopeQueued', + handler: (ev: EnvelopeQueuedEvent) => void + ): void; + + public override addEventListener( + name: 'envelopeUnsealed', + handler: (ev: EnvelopeUnsealedEvent) => void ): void; public override addEventListener( @@ -896,6 +902,19 @@ export default class MessageReceiver }; // Maintain invariant: encrypted queue => decrypted queue + const envelopeId = getEnvelopeId(decryptedEnvelope); + const taskId = `queueCached(EnvelopeEvent(${envelopeId}))`; + drop( + this.addToQueue( + async () => + this.dispatchAndWait( + taskId, + new EnvelopeQueuedEvent(decryptedEnvelope) + ), + taskId, + TaskType.Decrypted + ) + ); drop( this.addToQueue( async () => { @@ -1225,11 +1244,14 @@ export default class MessageReceiver logId = getEnvelopeId(unsealedEnvelope); - const taskId = `dispatchEvent(EnvelopeEvent(${logId}))`; + const taskId = `dispatchEvent(EnvelopeUnsealedEvent(${logId}))`; drop( this.addToQueue( async () => - this.dispatchAndWait(taskId, new EnvelopeEvent(unsealedEnvelope)), + this.dispatchAndWait( + taskId, + new EnvelopeUnsealedEvent(unsealedEnvelope) + ), taskId, TaskType.Decrypted ) diff --git a/ts/textsecure/messageReceiverEvents.ts b/ts/textsecure/messageReceiverEvents.ts index f62ca94c740f..2f7c1d256865 100644 --- a/ts/textsecure/messageReceiverEvents.ts +++ b/ts/textsecure/messageReceiverEvents.ts @@ -104,9 +104,17 @@ export class GroupSyncEvent extends Event { } } -export class EnvelopeEvent extends Event { +// Emitted right before we do full decrypt on a message, but after Sealed Sender unseal +export class EnvelopeUnsealedEvent extends Event { constructor(public readonly envelope: ProcessedEnvelope) { - super('envelope'); + super('envelopeUnsealed'); + } +} + +// Emitted when we queue previously-decrypted events from the cache +export class EnvelopeQueuedEvent extends Event { + constructor(public readonly envelope: ProcessedEnvelope) { + super('envelopeQueued'); } }