Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): store a message delivered deleted as deleted
Yudhi Armyndharis63f7a16786f4
diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.tsindex 528b52df8..84ea33fbc 100644--- a/src/engine/adapters/wwebjs-message-changes.spec.ts+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts@@ -142,6 +142,11 @@ describe.each([// The bodies the message:received and message:sent hooks were given, and a gate that holds the first of them.let hooked: string[];let hookGate: Promise<void> | undefined;+ // The rows handed to message:persisted, as they were then; what a plugin's message hook returns instead; an+ // insert that fails for good (not a unique violation, not transient).+ let persisted: Row[];+ let rewrite: ((data: Row) => Row) | undefined;+ let failInsert: ((row: Row) => boolean) | undefined;let engines: EngineRegistry;let engine: IWhatsAppEngine;let findOne: jest.Mock;@@ -154,6 +159,9 @@ describe.each([rows = [];hooked = [];hookGate = undefined;+ persisted = [];+ rewrite = undefined;+ failInsert = undefined;holdWrite = () => undefined;findOne = jest.fn(({ where }: { where: Row }) => Promise.resolve(rows.find(r => matches(r, where)) ?? null));// The real projector over a stand-in `messages` table, wired as the database wires it: only PostgreSQL@@ -164,6 +172,11 @@ describe.each([findOne,create: jest.fn((x: Row) => ({ ...x })),insert: jest.fn((row: Row) => {+ if (failInsert?.(row)) {+ return Promise.reject(+ Object.assign(new Error('database disk image is malformed'), { code: 'SQLITE_CORRUPT' }),+ );+ }// As the real table's unique index refuses a second row under an id.if (rows.some(r => r.waMessageId === row.waMessageId)) {return Promise.reject(@@ -195,9 +208,11 @@ describe.each([{ dispatch } as unknown as WebhookService,{execute: jest.fn(async (event: string, data: unknown) => {- if (event === 'message:received' || event === 'message:sent') hooked.push(String((data as Row).body));+ if (event === 'message:persisted') persisted.push({ ...(data as { message: Row }).message });+ if (event !== 'message:received' && event !== 'message:sent') return { continue: true, data };+ hooked.push(String((data as Row).body));if (event === 'message:received') await hookGate;- return { continue: true, data };+ return { continue: true, data: rewrite ? rewrite(data as Row) : data };}),} as unknown as HookManager,{ ingest: jest.fn() } as unknown as StatusStoreService,@@ -788,6 +803,91 @@ describe.each([expect(sentMarks()).toEqual([undefined, undefined]);});+ // whatsapp-web.js raises message and message_create for a message added to WhatsApp Web already deleted, with no+ // revoke event: such a delivery takes the message back as the revoke would, and lends nothing of its content.+ const deletedRow = (id: string) => ({ waMessageId: id, body: '', type: 'revoked', metadata: null });+ const deletedDelivery = (id: string, fromMe = false) => ({ ...incoming(id, '', { fromMe }), type: 'revoked' });++ it('stores an own message stored and edited, then delivered again as deleted, as deleted', async () => {+ client.emit('message_create', own('S', 'orig'));+ await flush();+ editEvent(client, 'S', 'e0', { fromMe: true });+ await flush();+ client.emit('message_create', deletedDelivery('S', true));+ await flush();++ expect(rows).toEqual([expect.objectContaining(deletedRow('S'))]);+ expect(persisted.at(-1)).toEqual(expect.objectContaining(deletedRow('S')));+ expect(events('message.sent')).toEqual([+ expect.objectContaining({ body: 'orig', type: 'text' }),+ expect.objectContaining({ body: '', type: 'revoked' }),+ ]);+ });++ it('stores a message stored, then delivered again as deleted, as deleted', async () => {+ client.emit('message', incoming('A', 'secret'));+ await flush();+ client.emit('message', deletedDelivery('A'));+ await flush();++ expect(rows).toEqual([expect.objectContaining(deletedRow('A'))]);+ expect(persisted.at(-1)).toEqual(expect.objectContaining(deletedRow('A')));+ });++ it.each([+ ['a message', false],+ ['an own message', true],+ ])('keeps a message stored in another chat under the id of %s delivered deleted', async (_, fromMe) => {+ rows.push({+ sessionId: 's1',+ waMessageId: 'A',+ chatId: '[email protected]',+ direction: fromMe ? 'outgoing' : 'incoming',+ type: 'text',+ body: 'kept',+ });+ client.emit(fromMe ? 'message_create' : 'message', deletedDelivery('A', fromMe));+ await flush();++ expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'kept', type: 'text' })]);+ });++ // The first delivery's row is lost to a failed insert: the one that came deleted inserts it, with an edit held.+ it.each([+ ['an own message', true],+ ['a message', false],+ ])(+ 'stores %s delivered again as deleted while its edit waits as deleted, the first insert failed',+ async (_, fromMe) => {+ const hook = deferred<void>();+ hookGate = hook.promise;+ client.emit('message', incoming('P', 'earlier'));+ await flush();+ failInsert = row => row.waMessageId === 'S' && row.type !== 'revoked';+ client.emit(fromMe ? 'message_create' : 'message', incoming('S', 'orig', { fromMe }));+ await flush();+ editEvent(client, 'S', 'e0', { fromMe });+ await flush();+ client.emit(fromMe ? 'message_create' : 'message', deletedDelivery('S', fromMe));+ await flush();+ hook.resolve();+ await flush();++ expect(rows.filter(r => r.waMessageId === 'S')).toEqual([expect.objectContaining(deletedRow('S'))]);+ expect(persisted.filter(r => r.waMessageId === 'S')).toEqual([expect.objectContaining(deletedRow('S'))]);+ },+ );++ it('stores a message delivered deleted as deleted whatever its hook returns', async () => {+ rewrite = data => ({ ...data, body: `${String(data.body)} signed` });+ client.emit('message_create', deletedDelivery('S', true));+ client.emit('message', deletedDelivery('A'));+ await flush();++ expect(rows).toEqual([expect.objectContaining(deletedRow('S')), expect.objectContaining(deletedRow('A'))]);+ expect(persisted).toEqual([expect.objectContaining(deletedRow('S')), expect.objectContaining(deletedRow('A'))]);+ });+it('announces an own message stored again after its row was removed without the mark of that row', async () => {client.emit('message_create', own('S', 'orig'));await flush();diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex 2e55b0998..a26c865ec 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -834,7 +834,7 @@ describe('MessageProjector (inbound projection)', () => {releaseHook();await drain();- expect(updatesAfterInsert()).toEqual([[where, { body: 'second fix' }]]);+ expect(updatesAfterInsert()).toEqual([[{ ...where, type: Not('revoked') }, { body: 'second fix' }]]);expect(webhookService.dispatch).toHaveBeenCalledWith(SESSION_ID, 'message.edited', expect.anything());});@@ -960,7 +960,7 @@ describe('MessageProjector (inbound projection)', () => {releaseHook();await drain();- expect(updatesAfterInsert()).toEqual([[where, { body: 'genuine edit' }]]);+ expect(updatesAfterInsert()).toEqual([[{ ...where, type: Not('revoked') }, { body: 'genuine edit' }]]);});it('keeps a reaction from another chat out of the message while its row is not written', async () => {@@ -1534,7 +1534,7 @@ describe('MessageProjector (inbound projection)', () => {releaseA();await flush();- expect(updatesAfterS()).toEqual([[whereS, { body: 'fixed' }]]);+ expect(updatesAfterS()).toEqual([[{ ...whereS, type: Not('revoked') }, { body: 'fixed' }]]);expect(webhookService.dispatch).toHaveBeenCalledWith(SESSION_ID,'message.sent',@@ -1572,7 +1572,7 @@ describe('MessageProjector (inbound projection)', () => {releaseA();await flush();- expect(updatesAfterS()).toEqual([[whereS, { body: 'fixed' }]]);+ expect(updatesAfterS()).toEqual([[{ ...whereS, type: Not('revoked') }, { body: 'fixed' }]]);});it('stores reactions that arrived before the row, without announcing them again', async () => {diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex faa3d1764..f368dcee0 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -1,7 +1,7 @@import { Injectable, Optional } from '@nestjs/common';import { ConfigService } from '@nestjs/config';import { InjectRepository } from '@nestjs/typeorm';-import { Repository, In } from 'typeorm';+import { Repository, In, Not } from 'typeorm';import { QueryDeepPartialEntity } from 'typeorm';import { Session } from './entities/session.entity';import { Message, MessageDirection, MessageStatus } from '../message/entities/message.entity';@@ -778,18 +778,29 @@ export class MessageProjector {// A duplicate on the retry is ambiguous: the first attempt may have committed before its error,// 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) {+ this.applyChangesMadeInFlight(id, incoming.id, false, claimOf(incoming));+ }+ // A delivery that came deleted clears the row it lost to, as a revoke does, once the row proves to be the message.+ if (outcome.landed === 'dup' && incoming.type === 'revoked') {+ void this.recordRevoke(id, incoming.id, claimOf(incoming));+ }if (outcome.landed === 'dup' && !outcome.retried) return 'repeat';- if (outcome.landed === 'dup') this.applyChangesMadeInFlight(id, incoming.id, false, claimOf(incoming));return { dbMessage, persisted: false };}/*** PostgreSQL holds a message from its arrival, so a revoke or edit often lands before its row: write* the row as the change leaves it, so `message:persisted` and the media archive never see content- * that was taken back. A change landing during the insert is still applied after it. Returns whether- * the row is written cleared.+ * that was taken back. A change landing during the insert is still applied after it. A message+ * delivered deleted is written cleared on either database, as the history sync stores one, and takes+ * no edit. Returns whether the row is written cleared.*/private applyHeldChanges(id: string, dbMessage: Message): boolean {+ if (dbMessage.type === 'revoked') {+ Object.assign(dbMessage, REVOKED_ROW_PATCH);+ return true;+ }if (!this.holdsInbound) return false;const entry = dbMessage.waMessageId ? this.inboundInFlight.get(`${id}:${dbMessage.waMessageId}`) : undefined;if (entry?.revoked) Object.assign(dbMessage, REVOKED_ROW_PATCH);@@ -915,8 +926,12 @@ export class MessageProjector {): Promise<void> {if (pending.revoked) return this.revokeRow(id, waMessageId, !insertedCleared);try {+ // Never onto a revoked row, as the edit paths: a message delivered deleted, or deleted before it, stays cleared.if (pending.editedBody !== undefined) {- await this.messageRepository.update({ sessionId: id, waMessageId }, { body: pending.editedBody });+ await this.messageRepository.update(+ { sessionId: id, waMessageId, type: Not('revoked') },+ { body: pending.editedBody },+ );}if (pending.reactions) await this.storeReactions(id, waMessageId, pending.reactions);// The marker an edit of a stored row leaves, written last so a lost metadata race costs it alone.@@ -940,9 +955,9 @@ export class MessageProjector {/*** Clear a revoked message's stored row, on the message's mutation chain so a reaction or edit queued- * before it cannot write content back afterwards. Used by the engine's revoke event and by the REST- * delete. Best-effort like every stored-message mutation: never rejects, and resolves once the write- * has run.+ * before it cannot write content back afterwards. Used by the engine's revoke event, by the REST+ * delete, and by a delivery that came deleted and lost its insert. Best-effort like every stored-message+ * mutation: never rejects, and resolves once the write has run.** With `source`, the caller has not had the stored row judge the revoke yet: the message still arriving* is taken back only if the revoke agrees with it, and the row is cleared only if the revoke is not@@ -1047,7 +1062,9 @@ export class MessageProjector {* engine revoke, and the message still exists for its sender) or when it was delivered deleted, the edited* body when edited (message.edited already went out with it), plus `edited: true` when the row is marked* for it or already was (`rowMarked`): a consumer that held no copy of the message could not apply that- * event. The stored row takes the same changes in {@link applyChangesMadeInFlight}.+ * event. The stored row takes the same changes in {@link applyChangesMadeInFlight}. One delivered deleted+ * is stored cleared and takes no edit ({@link applyHeldChanges}), and clears the row it lost its insert to+ * when that row is the message.*/private withChangesMadeInFlight(id: string,@@ -1200,6 +1217,8 @@ export class MessageProjector {// 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, false, claimOf(outgoing));+ // A delivery that came deleted clears that row, as a revoke does, once the row proves to be the message.+ if (outgoing.type === 'revoked') void this.recordRevoke(id, outgoing.id, claimOf(outgoing));// That row may be older than this delivery and edited since, the edit still being written: announced once the// changes queued for the message have run, with the mark they left.rowMarked = await new Promise<boolean>(resolve =>