Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): order a lost insert's changes before later ones
Yudhi Armyndharis7d8430ab6f2b
diff --git a/src/modules/session/message-projector.inbound-hold.spec.ts b/src/modules/session/message-projector.inbound-hold.spec.tsindex f0a7e7b8d..cb2cf8744 100644--- a/src/modules/session/message-projector.inbound-hold.spec.ts+++ b/src/modules/session/message-projector.inbound-hold.spec.ts@@ -144,6 +144,13 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {.map(c => c[1].message);const room = () =>(projector as unknown as { rooms: Map<string, { gate: { activeCount: number } }> }).rooms.get('s1');+ // As the real table's unique index refuses a second row under an id.+ const uniqueIndex = (): void => {+ insertGate = row =>+ rows.some(r => r.waMessageId === row.waMessageId)+ ? Promise.reject(Object.assign(new Error('UNIQUE constraint failed'), { code: 'SQLITE_CONSTRAINT_UNIQUE' }))+ : Promise.resolve();+ };beforeEach(() => {process.env.INBOUND_MEDIA_CONCURRENCY = '1';@@ -529,14 +536,6 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {body: '',timestamp: 1,});- // As the real table's unique index refuses a second row under an id.- const uniqueIndex = (): void => {- insertGate = row =>- rows.some(r => r.waMessageId === row.waMessageId)- ? Promise.reject(Object.assign(new Error('UNIQUE constraint failed'), { code: 'SQLITE_CONSTRAINT_UNIQUE' }))- : Promise.resolve();- };-it('leaves the stored row alone and announces nothing, while the delivery is held', async () => {rows.push(stored());uniqueIndex();@@ -658,6 +657,93 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {});});+ // The echo of a send the REST writer already stored loses its insert to that row. What reached the message+ // while it waited is written onto the row once the row says it is the message, and a change that arrives+ // while the row is read came after it and is stored after it.+ describe.each([+ ['PostgreSQL', 'postgres'],+ ['SQLite', 'better-sqlite3'],+ ])('an own-send echo whose insert lost to the stored send, on %s', (_name, type) => {+ const CHAT = '[email protected]';+ const edit = (body: string): void =>+ projector.applyMessageEditQueued('s1', { messageId: 'A', body, chatId: CHAT, fromMe: true } as never);+ const react = (reaction: string): void =>+ projector.applyReactionQueued('s1', { messageId: 'A', chatId: CHAT, reaction, senderId: CHAT });++ /**+ * The echo waits in its hook chain while `earlier` reaches it and before the REST send's row exists.+ * Its insert then loses to that row, and the read that judges the row is held while `later` arrives.+ */+ const lose = async (earlier: () => void, later: () => void): Promise<void> => {+ build(type);+ uniqueIndex();+ const hook = deferred<void>();+ hookManager.execute.mockImplementationOnce(async (_event: string, data: unknown) => {+ await hook.promise;+ return { continue: true, data };+ });+ const ticket = type === 'postgres' ? admit(arrival('A', CHAT, false, false, true)) : undefined;+ projector.handleOwnSendEcho('s1', engine, message('A', CHAT, { fromMe: true, body: 'orig' }), ticket);+ await flush();+ earlier();+ await flush();+ rows.push({ sessionId: 's1', waMessageId: 'A', chatId: CHAT, direction: 'outgoing', type: 'text', body: 'orig' });+ const read = repo.findOne as jest.Mock;+ const open = deferred<void>();+ const unrestricted = read.getMockImplementation() as (arg: unknown) => Promise<unknown>;+ read.mockImplementationOnce(async (arg: unknown) => {+ await open.promise;+ return unrestricted(arg);+ });+ hook.resolve();+ await flush();+ later();+ await flush();+ open.resolve();+ await flush();+ expect(dispatched('message.sent')).toHaveLength(1);+ };++ it('writes the edit recorded for the echo onto the row', async () => {+ await lose(+ () => edit('v1'),+ () => undefined,+ );++ expect(rows[0].body).toBe('v1');+ });++ it('writes the reaction recorded for the echo onto the row', async () => {+ await lose(+ () => react('thumb'),+ () => undefined,+ );++ expect((rows[0].metadata as Row).reactions).toEqual({ [CHAT]: 'thumb' });+ });++ it('stores an edit that arrives while the row is read after the one recorded for the echo', async () => {+ await lose(+ () => edit('v1'),+ () => edit('v2'),+ );++ expect(rows[0].body).toBe('v2');+ expect(+ (eventsGateway.emitMessageEdited.mock.calls as Array<[string, { body: string }]>).map(c => c[1].body),+ ).toEqual(['v1', 'v2']);+ });++ it('stores a reaction that arrives while the row is read after the one recorded for the echo', async () => {+ await lose(+ () => react('thumb'),+ () => react('heart'),+ );++ expect((rows[0].metadata as Row).reactions).toEqual({ [CHAT]: 'heart' });+ });+ });+it('stores a reaction and an ack that arrived before the emit once the row is written', async () => {const t = admit(arrival('A', 'chat1'));projector.applyReactionQueued('s1', { messageId: 'A', chatId: 'chat1', reaction: 'ok', senderId: '[email protected]' });diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex f3cfed2b8..d81854708 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -796,12 +796,17 @@ export class MessageProjector {* just inserted: its own write matched no row then. Queued on the message's mutation chain, so it* lands after any edit already queued, and a revoke wins over the rest. A change arriving after the* insert finds the row itself. Reactions are only stored here; their event already went out. A row- * inserted cleared is cleared again, in case a change raced the insert, but `message:persisted`- * already carried it cleared and does not fire twice.+ * inserted cleared is cleared again, in case a change raced the insert, without announcing it:+ * `message:persisted` already carried it cleared. A revoke event still waiting for its own row read+ * when the insert lands clears the row and announces it once more: a consumer sees the cleared row+ * twice, under the same id.** `lostInsert`, set when the delivery's insert lost to a row under the id, says what the delivery claims* to be. That row may be the message itself (its first attempt committed before the error) or another- * message's (an id reused from another chat): the changes go to it only if it is the message.+ * message's (an id reused from another chat): the changes go to it only if it is the message. The+ * judgement is the first step of the slot taken on the chain here, not a read ahead of it, so a change+ * of the message that arrives while the row is read queues behind the write instead of being+ * overwritten by it.*/private applyChangesMadeInFlight(id: string,@@ -810,50 +815,56 @@ export class MessageProjector {lostInsert?: MutationSource,): void {const pending = waMessageId ? this.inboundInFlight.get(`${id}:${waMessageId}`) : undefined;- if (- !pending ||- (!pending.ackStatus && !pending.revoked && pending.editedBody === undefined && !pending.reactions)- ) {- return;- }+ if (!pending) return;+ const { ackStatus } = pending;+ const rowChanges = Boolean(pending.revoked || pending.editedBody !== undefined || pending.reactions);+ if (!ackStatus && !rowChanges) return;+ const advance = (): void => {+ if (!ackStatus) return;+ void this.advanceAck(id, waMessageId, ackStatus).catch(err =>+ this.logger.error(`Failed to advance ack for ${waMessageId}`, String(err)),+ );+ };if (!lostInsert) {- this.writeChangesMadeInFlight(id, waMessageId, pending, insertedCleared);+ advance();+ if (rowChanges) {+ this.enqueueMessageMutation(id, waMessageId, () =>+ this.writeChangesMadeInFlight(id, waMessageId, pending, insertedCleared),+ );+ }return;}- void this.mutationProjector.foreignRowReason(id, waMessageId, lostInsert).then(reason => {- if (!reason) return this.writeChangesMadeInFlight(id, waMessageId, pending, insertedCleared);- this.logger.warn('Not applying the changes of a message to the stored message that holds its id', {- sessionId: id,- messageId: waMessageId,- reason,- });+ this.enqueueMessageMutation(id, waMessageId, async () => {+ const reason = await this.mutationProjector.foreignRowReason(id, waMessageId, lostInsert);+ if (reason) {+ this.logger.warn('Not applying the changes of a message to the stored message that holds its id', {+ sessionId: id,+ messageId: waMessageId,+ reason,+ });+ return;+ }+ advance();+ await this.writeChangesMadeInFlight(id, waMessageId, pending, insertedCleared);});}- private writeChangesMadeInFlight(+ /** The row part of {@link applyChangesMadeInFlight}. Must run on the mutation chain. */+ private async writeChangesMadeInFlight(id: string,waMessageId: string,pending: InFlightEntry,insertedCleared: boolean,- ): void {- const { ackStatus } = pending;- if (ackStatus) {- void this.advanceAck(id, waMessageId, ackStatus).catch(err =>- this.logger.error(`Failed to advance ack for ${waMessageId}`, String(err)),- );- }- if (!pending.revoked && pending.editedBody === undefined && !pending.reactions) return;- this.enqueueMessageMutation(id, waMessageId, async () => {- if (pending.revoked) return this.revokeRow(id, waMessageId, !insertedCleared);- try {- if (pending.editedBody !== undefined) {- await this.messageRepository.update({ sessionId: id, waMessageId }, { body: pending.editedBody });- }- if (pending.reactions) await this.storeReactions(id, waMessageId, pending.reactions);- } catch (err) {- this.logger.error(`Failed to apply changes to message ${waMessageId}`, String(err));+ ): Promise<void> {+ if (pending.revoked) return this.revokeRow(id, waMessageId, !insertedCleared);+ try {+ if (pending.editedBody !== undefined) {+ await this.messageRepository.update({ sessionId: id, waMessageId }, { body: pending.editedBody });}- });+ if (pending.reactions) await this.storeReactions(id, waMessageId, pending.reactions);+ } catch (err) {+ this.logger.error(`Failed to apply changes to message ${waMessageId}`, String(err));+ }}/** Merge reactions into the row's metadata, withdrawing a sender's on ''. Must run on the mutation chain. */