Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): keep a lost insert's changes off the row it lost to
Yudhi Armyndharis43e06a67f4a9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ed2691e50..839f89c47 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Drop a Baileys message that reuses the id of one still being downloaded under another chat or sender, so it cannot take the id or stand in for the original when a later delete is judged.- Drop the stored copy of a Baileys message that reuses the id of a message the gateway already holds under another chat, sender or direction, and hold an edit, delete or reaction that reads as foreign to that copy until the gateway has settled the message, so the real message's change is no longer ignored as foreign; a reusing message deleted by its sender while it downloads on SQLite, or skipped as ephemeral, is never settled and keeps its copy.- A Baileys revoke from the chat of a delivery that reuses the id of a message stored under another chat, or sent by the account in a one-to-one chat, no longer clears that stored message or announces `message.revoked` for it; a contact who knew the id could erase it, on SQLite and PostgreSQL.+- A message whose insert lost to the row of another message under the same id no longer writes its pending revoke, edit, reaction or ack onto that row.- Warn at storage export when a file is larger than the per-entry import cap `STORAGE_IMPORT_MAX_BYTES`.- Refuse a `PORT` with surrounding whitespace at boot instead of binding a port the image healthcheck cannot probe.- Skip or cancel the Baileys version lookup of a session stopped while it connects, so no proxy dispatcher outlives it.diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex f9af50508..3d492a591 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -1616,6 +1616,97 @@ describe('MessageProjector (inbound projection)', () => {]);});+ // The retry's duplicate may be the message's own row (its first attempt committed before the error) or+ // the row of another message under a reused id. What was recorded for the message goes only to a row+ // that is it.+ describe('a retry that meets the row of another message', () => {+ const own = '[email protected]';+ const foreign = { chatId: '[email protected]', direction: 'incoming', author: null };+ const same = { chatId: own, direction: 'incoming', author: null };+ let releaseHook!: () => void;++ /** Holds the hook chain, lets `record` reach the message meanwhile, then runs the failing retry. */+ const retried = async (+ record: (engine: IWhatsAppEngine) => void,+ row: Record<string, unknown>,+ echo = false,+ ): Promise<unknown[]> => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ messageRepository.insert.mockRejectedValueOnce(busy()).mockRejectedValueOnce(dup());+ hookManager.execute.mockImplementationOnce(async (_event: string, data: unknown) => {+ await new Promise<void>(resolve => (releaseHook = resolve));+ return { continue: true, data };+ });+ const incoming = makeIncoming({ fromMe: echo });+ if (echo) projector.handleOwnSendEcho(SESSION_ID, engine, incoming);+ else projector.handleInboundMessage(SESSION_ID, engine, incoming);+ await flush();+ record(engine);+ await flush();+ // The row holding the id appears only now: nothing stored it when the change was recorded.+ messageRepository.findOne.mockResolvedValue(row);+ messageRepository.update.mockClear();+ releaseHook();+ await flush();+ await jest.advanceTimersByTimeAsync(PERSIST_RETRY_DELAY_MS);+ await flush();+ return messageRepository.update.mock.calls as unknown[];+ };++ const changes: Array<[string, (engine: IWhatsAppEngine) => void]> = [+ [+ 'a revoke',+ engine =>+ projector.handleMessageRevoked(SESSION_ID, engine, { id: 'wamid.1', chatId: own } as never, {+ fromMe: false,+ }),+ ],+ [+ 'an edit',+ () =>+ projector.applyMessageEditQueued(SESSION_ID, {+ messageId: 'wamid.1',+ body: 'late fix',+ chatId: own,+ fromMe: false,+ } as never),+ ],+ ['an ack', engine => projector.handleMessageAck(SESSION_ID, engine, 'wamid.1', 'read')],+ [+ 'a reaction',+ () =>+ projector.applyReactionQueued(SESSION_ID, {+ messageId: 'wamid.1',+ chatId: own,+ reaction: 'ok',+ senderId: '[email protected]',+ }),+ ],+ ];++ it.each(changes)('leaves that row alone when the message was given %s', async (_name, record) => {+ expect(await retried(record, foreign)).toEqual([]);+ });++ it.each(changes)('still writes %s onto the row when it is the message itself', async (_name, record) => {+ expect((await retried(record, same)).length).toBeGreaterThan(0);+ });++ it('leaves the row of another message alone for an own-send echo too', async () => {+ const record = (engine: IWhatsAppEngine): void =>+ projector.handleMessageRevoked(SESSION_ID, engine, { id: 'wamid.1', chatId: own } as never, { fromMe: true });+ expect(await retried(record, foreign, true)).toEqual([]);+ });++ it('writes the echo changes onto the row a REST send stored under the other dialect', async () => {+ const record = (engine: IWhatsAppEngine): void =>+ projector.handleMessageRevoked(SESSION_ID, engine, { id: 'wamid.1', chatId: own } as never, { fromMe: true });+ const row = { chatId: '[email protected]', direction: 'outgoing', author: null };+ expect((await retried(record, row, true)).length).toBeGreaterThan(0);+ });+ });+it('drops the retry when the engine is retired while it waits', async () => {const engine = makeEngine();engines.set(SESSION_ID, engine);diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 4350574c5..fff97e0be 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -101,6 +101,14 @@ const refOf = (m: Pick<IncomingMessage, 'chatId' | 'fromMe' | 'author'>): Storedauthor: m.author,});+/** What a delivery claims to be, as the rule that refuses a change aimed at a stored row reads it. */+const claimOf = (m: Pick<IncomingMessage, 'chatId' | 'fromMe' | 'author'>): MutationSource => ({+ kind: 'edit',+ chatId: m.chatId,+ fromMe: m.fromMe,+ author: m.author,+});+/** A message not yet written, and the changes that reached it meanwhile (see inboundInFlight). */interface InFlightEntry {// What a quote of it may lend, as the hook chain last left it. Never the message itself: a repeat@@ -650,12 +658,11 @@ export class MessageProjector {* not prove a difference, says nothing and the engine keeps its copy.*/private async judgeRepeat(id: string, message: InboundMessageData): Promise<InboundOutcome | undefined> {- const reason = await this.mutationProjector.foreignRowReason(id, storableWaMessageId(message.id) ?? '', {- kind: 'edit',- chatId: message.chatId,- fromMe: message.fromMe,- author: message.author,- });+ const reason = await this.mutationProjector.foreignRowReason(+ id,+ storableWaMessageId(message.id) ?? '',+ claimOf(message),+ );if (!reason) return undefined;this.logger.warn('Dropping a message that reuses the id of a different stored message', {sessionId: id,@@ -721,7 +728,7 @@ export class MessageProjector {// in which case nothing dispatched yet, so dispatch fail-open (without the row-bound hook), and// that row still has to take a revoke or edit that arrived in flight.if (outcome.landed === 'dup' && !outcome.retried) return 'repeat';- if (outcome.landed === 'dup') this.applyChangesMadeInFlight(id, incoming.id);+ if (outcome.landed === 'dup') this.applyChangesMadeInFlight(id, incoming.id, false, claimOf(incoming));return { dbMessage, persisted: false };}@@ -791,16 +798,51 @@ export class MessageProjector {* 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.+ *+ * `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.*/- private applyChangesMadeInFlight(id: string, waMessageId: string, insertedCleared = false): void {+ private applyChangesMadeInFlight(+ id: string,+ waMessageId: string,+ insertedCleared = false,+ lostInsert?: MutationSource,+ ): void {const pending = waMessageId ? this.inboundInFlight.get(`${id}:${waMessageId}`) : undefined;- const ackStatus = pending?.ackStatus;+ if (+ !pending ||+ (!pending.ackStatus && !pending.revoked && pending.editedBody === undefined && !pending.reactions)+ ) {+ return;+ }+ if (!lostInsert) {+ 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,+ });+ });+ }++ private 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;+ if (!pending.revoked && pending.editedBody === undefined && !pending.reactions) return;this.enqueueMessageMutation(id, waMessageId, async () => {if (pending.revoked) return this.revokeRow(id, waMessageId, !insertedCleared);try {@@ -1054,7 +1096,7 @@ export class MessageProjector {const outcome = await this.insertWithRetry(id, engine, dbMessage, 'outgoing');if (outcome.landed === 'stale') return;// The REST writer may have won either attempt; its row still takes the echo's pending changes.- if (outcome.landed === 'dup') this.applyChangesMadeInFlight(id, outgoing.id);+ if (outcome.landed === 'dup') this.applyChangesMadeInFlight(id, outgoing.id, false, claimOf(outgoing));if (outcome.landed === 'yes') {this.applyChangesMadeInFlight(id, outgoing.id, insertedCleared);// Fire-and-forget, mirroring onMessage: plugin providers (search etc.) see phone-