Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): drop the stored copy of a delivery that reuses a held id
Yudhi Armyndharis98298d38e22c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex caa95bc3c..fff8f42e3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -71,6 +71,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Bound the inbound media a PostgreSQL session holds before storing it to `INBOUND_MEDIA_CONCURRENCY` payloads, keeping each chat's arrival order.- Drop a Baileys edit, revoke or reaction from another chat, an edit from the wrong author, or a one-to-one revoke from the other side, aimed at a message still being downloaded, on SQLite too.- 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 or sender, so that message's later edit, delete or reaction, once the delivery has been handled, is no longer ignored as foreign to it.- 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/docs/03-system-architecture.md b/docs/03-system-architecture.mdindex 394598bb7..b4da3bcd2 100644--- a/docs/03-system-architecture.md+++ b/docs/03-system-architecture.md@@ -925,7 +925,7 @@ export interface EngineEventCallbacks {fromMe?: boolean;author?: string;}) => InboundTicket | undefined; // PostgreSQL: reserves room for a download before the emit, and holds a change to the chat, direction and author it names- onMessage?: (message: IncomingMessage, ticket?: InboundTicket) => void;+ onMessage?: (message: IncomingMessage, ticket?: InboundTicket) => void | InboundReceipt; // the receipt reports { conflict: true } when the id already held another message; Baileys drops its copyonMessageCreate?: (message: IncomingMessage, ticket?: InboundTicket) => void; // outgoing (incl. linked-phone sends)onMessageAck?: (messageId: string, status: DeliveryStatus, chatId?: string) => void;onMessageRevoked?: (message: RevokedMessage, actor?: { fromMe: boolean }) => void;diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.tsindex c81407b3d..1c9867f4a 100644--- a/src/engine/adapters/baileys-events.spec.ts+++ b/src/engine/adapters/baileys-events.spec.ts@@ -113,6 +113,7 @@ function makeHost(overrides: Partial<BaileysEventsHost> = {}): BaileysEventsHostgetStoredMessage: () => undefined,putStoredMessage: () => undefined,updateStoredMessage: () => undefined,+ removeStoredMessage: () => undefined,getOnMessage: () => undefined,getOnMessageCreate: () => undefined,getOnMessageRevoked: () => undefined,diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.tsindex 607553d8f..3d081be45 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -7,6 +7,7 @@ import {EventResponseEvent,GroupEvent,IncomingCallEvent,+ InboundReceipt,InboundTicket,ParticipantPresence,PresenceState,@@ -221,6 +222,8 @@ export interface BaileysEventsHost {* is arriving; false for anything the session did not send (see OwnSendRegistry).*/consumeOwnSend(id: string | null | undefined): boolean;+ /** Delete a stored message while `shouldRemove` accepts the copy held then; undefined without a store. */+ removeStoredMessage(messageId: string, shouldRemove: (stored: WAMessage) => boolean): Promise<void> | undefined;/** A message this session already delivered or sent, from the persistent store; undefined without a store. */getStoredMessage(messageId: string): Promise<WAMessage | null> | undefined;/** The currently-registered admitInbound callback, if any (assigned at initialize()). */@@ -328,6 +331,26 @@ export class BaileysEvents {*/private readonly editedWhileInFlight = new Map<string, { envelope: WAMessageKey; body: string }>();+ /**+ * How many deliveries of an id were handed to onMessage and have no outcome yet, by id. The stored+ * copy of such an id may be a duplicate the session is about to call another message's (the real one+ * lives in its table only), so a change that reads as foreign to it is the session's to judge, which+ * it does against the row (see targetsForeignMessage). An entry lasts at most OUTCOME_HOLD_MS. It begins+ * when the delivery is handed on: a change that arrives while the delivery is still being processed+ * (limiter wait, media download) is judged against the key it arrived under, as before.+ */+ private readonly outcomesPending = new Map<string, number>();++ /**+ * How many conflicting copies have been removed from the store. A read of the store that was out+ * while one went may have returned that copy, which is not what the store holds any more (see+ * targetsForeignMessage).+ */+ private copyRemovals = 0;++ /** How long an id stays held for the session's outcome; a receipt not settled by then is let go. */+ private static readonly OUTCOME_HOLD_MS = 5 * 60_000;+/*** Bumped by fenceStoredWrites when the account is unlinked. A message stores its copy only while the* generation it arrived under is current: one still processing when the store is wiped (a media@@ -809,8 +832,8 @@ export class BaileysEvents {if (!deleted || opts.ticket?.revoked) {const emit = msg.key.fromMe === true ? this.host.getOnMessageCreate() : this.host.getOnMessage();// The ticket goes along only where one was issued, so elsewhere the call is unchanged.- if (opts.ticket) emit?.(incoming, opts.ticket);- else emit?.(incoming);+ const receipt = opts.ticket ? emit?.(incoming, opts.ticket) : emit?.(incoming);+ this.watchOutcome(msg.key, receipt);}this.host.recordMessage(msg, incoming.type);if (deleted) {@@ -826,6 +849,79 @@ export class BaileysEvents {}}+ /**+ * Act on what the session made of a message handed to onMessage, once it says. A delivery that+ * reuses the id of a different message the session holds was dropped there as a duplicate, yet its+ * copy was stored under that id before the emit, and would stand in for the real message when a+ * later edit, delete or reaction of it is judged. Detached: it never delays inbound handling, and+ * nothing it does can fail it, whatever the callback returns or rejects with.+ */+ private watchOutcome(key: WAMessageKey, receipt: void | InboundReceipt): void {+ const id = key.id;+ if (!id || !receipt?.settled) return;+ // The session holds no entry for a status post, so it judges no change aimed at one: only a message+ // it holds can stand in for the stored copy there.+ const release = key.remoteJid === 'status@broadcast' ? undefined : this.holdOutcome(id);+ void Promise.resolve(receipt.settled)+ .then(result => (result?.conflict === true ? this.dropConflictingCopy(key, id) : undefined))+ .catch((err: unknown) =>+ this.host.logger.warn('Could not act on what the session made of a message', {+ msgId: id,+ error: err instanceof Error ? err.message : String(err),+ }),+ )+ .finally(() => release?.());+ }++ /**+ * Count a delivery of `id` as awaiting the session's outcome, until the returned function is called+ * or OUTCOME_HOLD_MS passes, whichever is first, so a receipt that never settles (a hook that never+ * returns) cannot keep the id held for the life of the adapter. Ending twice counts once.+ */+ private holdOutcome(id: string): () => void {+ this.outcomesPending.set(id, (this.outcomesPending.get(id) ?? 0) + 1);+ let ended = false;+ const end = (): void => {+ if (ended) return;+ ended = true;+ clearTimeout(timer);+ const pending = (this.outcomesPending.get(id) ?? 1) - 1;+ if (pending > 0) this.outcomesPending.set(id, pending);+ else this.outcomesPending.delete(id);+ };+ const timer = setTimeout(end, BaileysEvents.OUTCOME_HOLD_MS);+ timer.unref();+ return end;+ }++ /**+ * Remove the copy a conflicting delivery stored, so the real message's later changes are judged+ * against nothing wrong. Only a copy that still carries this delivery's own key goes: the store is+ * read when the delete runs, behind every write of the id queued before it. Settles once the delete+ * has run, so the id stays held until then; never rejects.+ */+ private async dropConflictingCopy(key: WAMessageKey, id: string): Promise<void> {+ this.host.logger.warn('Dropping the stored copy of a message that reuses the id of another message', {+ msgId: id,+ remoteJid: key.remoteJid,+ });+ try {+ await this.host.removeStoredMessage(+ id,+ stored =>+ stored.key?.remoteJid === key.remoteJid &&+ keyParticipant(stored.key ?? {}) === keyParticipant(key) &&+ (stored.key?.fromMe === true) === (key.fromMe === true),+ );+ } catch (err) {+ this.host.logger.warn('Failed to remove the stored copy of a conflicting message', {+ msgId: id,+ error: err instanceof Error ? err.message : String(err),+ });+ }+ this.copyRemovals += 1;+ }+/*** What an edit, revoke or reaction targets: the stored copy, or the one still being processed. The* in-flight copy is taken BEFORE the store is read, because an original that finishes while the read is@@ -981,18 +1077,38 @@ export class BaileysEvents {checkAuthor: boolean,kind?: 'reaction',): Promise<boolean> {- // A message still being processed has no stored copy yet, but the projector may already hold it- // (PostgreSQL), so its own key is checked instead.- const original = targetId- ? (await this.readTarget(targetId, 'checking what an edit, revoke or reaction targets'))?.key- : undefined;- if (!original) return false;+ if (!targetId) return false;+ for (let attempt = 0; ; attempt++) {+ const removals = this.copyRemovals;+ // A message still being processed has no stored copy yet, but the projector may already hold it+ // (PostgreSQL), so its own key is checked instead.+ const original = (await this.readTarget(targetId, 'checking what an edit, revoke or reaction targets'))?.key;+ if (!original || !this.readsAsForeign(original, key, checkAuthor, kind)) return false;+ // The session has not said yet whether the delivery that stored this copy is the message it holds+ // under the id or a duplicate of another one. Its own judgement stands in: it holds the message and+ // checks the change against its key and against the stored row, and the stored copy does not.+ if (this.outcomesPending.has(targetId)) return false;+ // A conflicting copy went while the store was read, and the read may have returned it: it ends the+ // hold only once removed, so the read is the one that can no longer be told from the real copy. Read+ // again, with the copy gone, rather than drop the real author's change against it.+ if (attempt === 0 && this.copyRemovals !== removals) continue;+ this.host.logger.warn('Dropping an edit, revoke or reaction aimed at a message from another chat or author', {+ msgId: key.id ?? 'unknown',+ targetId,+ remoteJid: key.remoteJid,+ });+ return true;+ }+ }++ /** Whether a change arriving under `key` names a message other than the one `original` is. */+ private readsAsForeign(original: WAMessageKey, key: WAMessageKey, checkAuthor: boolean, kind?: 'reaction'): boolean {const broadcast = kind === 'reaction' && !!original.remoteJid?.endsWith('@broadcast');if (broadcast && original.fromMe === true) return false;const originalChats = [original.remoteJid, original.remoteJidAlt];if (broadcast && original.remoteJid !== 'status@broadcast') originalChats.push(original.participant);const neutral = (jid: string): string => this.host.toNeutralJid(jid);- const foreign =+ return (differentWaIds(originalChats, [key.remoteJid, key.remoteJidAlt], neutral) ||(checkAuthor &&((original.fromMe === true) !== (key.fromMe === true) ||@@ -1001,15 +1117,8 @@ export class BaileysEvents {[original.participant, original.participantAlt],[key.participant, key.participantAlt],neutral,- ))));- if (foreign) {- this.host.logger.warn('Dropping an edit, revoke or reaction aimed at a message from another chat or author', {- msgId: key.id ?? 'unknown',- targetId,- remoteJid: key.remoteJid,- });- }- return foreign;+ ))))+ );}/**diff --git a/src/engine/adapters/baileys-message-store.service.spec.ts b/src/engine/adapters/baileys-message-store.service.spec.tsindex 35f0c3b4c..04e5bfcf4 100644--- a/src/engine/adapters/baileys-message-store.service.spec.ts+++ b/src/engine/adapters/baileys-message-store.service.spec.ts@@ -402,6 +402,89 @@ describe('BaileysMessageStoreService', () => {});});+ describe('remove', () => {+ it('deletes the accepted copy of that session only', async () => {+ await seedSession('s1');+ await seedSession('s2');+ await service.put('s1', msg('M1'));+ await service.put('s1', msg('M2'));+ await service.put('s2', msg('M1'));++ await service.remove('s1', 'M1', () => true);++ expect(await service.getMessage('s1', 'M1')).toBeNull();+ expect(await service.getMessage('s1', 'M2')).not.toBeNull();+ expect(await service.getMessage('s2', 'M1')).not.toBeNull();+ });++ it('keeps a copy the caller declines, and leaves an id the store does not hold absent', async () => {+ await seedSession('s1');+ await service.put('s1', msg('M1'));+ const accept = jest.fn(() => false);++ await service.remove('s1', 'M1', accept);+ await service.remove('s1', 'NOPE', accept);++ expect(accept).toHaveBeenCalledTimes(1); // only for the row that exists+ expect((accept.mock.calls as unknown as Array<[WAMessage]>)[0][0].key.id).toBe('M1');+ expect((await service.getMessage('s1', 'M1'))?.message?.conversation).toBe('M1');+ });++ it('judges the copy held when it runs, behind a put of the same id still in flight', async () => {+ await seedSession('s1');+ const seen: unknown[] = [];+ await Promise.all([+ service.put('s1', msg('M1')),+ Promise.resolve().then(() =>+ service.remove('s1', 'M1', stored => {+ seen.push(stored.message?.conversation);+ return true;+ }),+ ),+ ]);++ expect(seen).toEqual(['M1']);+ expect(await service.getMessage('s1', 'M1')).toBeNull();+ });++ it('a read issued while the delete is in flight sees the copy gone', async () => {+ await seedSession('s1');+ await service.put('s1', msg('M1'));+ let release!: () => void;+ const gate = new Promise<void>(resolve => (release = resolve));+ const real = repo.delete.bind(repo);+ jest.spyOn(repo, 'delete').mockImplementationOnce(async (...args: Parameters<typeof real>) => {+ await gate;+ return real(...args);+ });++ const removal = service.remove('s1', 'M1', () => true);+ const read = service.getMessage('s1', 'M1');+ await ticks();+ release();+ await removal;++ expect(await read).toBeNull();+ });++ // An id the caller could not read must not reach the ORM, which drops an undefined condition and+ // would match an arbitrary row of the session.+ it.each([+ ['empty', ''],+ ['missing', undefined as unknown as string],+ ])('does nothing for a %s id', async (_name, id) => {+ await seedSession('s1');+ await service.put('s1', msg('M1'));+ await service.put('s1', msg('M2'));+ const accept = jest.fn(() => true);++ await service.remove('s1', id, accept);++ expect(accept).not.toHaveBeenCalled();+ expect(await repo.count({ where: { sessionId: 's1' } })).toBe(2);+ });+ });+it('clearSession removes only that session', async () => {await seedSession('s1');await seedSession('s2');diff --git a/src/engine/adapters/baileys-message-store.service.ts b/src/engine/adapters/baileys-message-store.service.tsindex 20a126f4a..4850cbf4c 100644--- a/src/engine/adapters/baileys-message-store.service.ts+++ b/src/engine/adapters/baileys-message-store.service.ts@@ -96,6 +96,23 @@ export class BaileysMessageStoreService implements BaileysMessageStore {});}+ /**+ * Delete a stored message while `shouldRemove` accepts the copy held when the delete runs: a copy+ * written or rewritten since the caller looked is judged as it is now. An id the store does not hold+ * stays absent. Queued behind any put() or update() of the same id already called.+ */+ remove(sessionId: string, messageId: string, shouldRemove: (stored: WAMessage) => boolean): Promise<void> {+ if (!messageId) {+ return Promise.resolve();+ }+ return this.track(sessionId, messageId, async () => {+ const stored = await this.readStored(sessionId, messageId);+ if (stored && shouldRemove(stored)) {+ await this.repo.delete({ sessionId, waMessageId: messageId });+ }+ });+ }+/*** Queue `work` on the id's write chain and register it as the id's in-flight write before returning,* so a read issued any time after put() or update() is called waits for it. `work` is called inside adiff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 349b66610..256005caf 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -169,6 +169,7 @@ import {GroupEvent,IncomingCallEvent,IncomingMessage,+ InboundOutcome,} from '../interfaces/whatsapp-engine.interface';import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';@@ -193,6 +194,7 @@ const fakeStore = {getMessages: jest.fn().mockResolvedValue([]),clearSession: jest.fn().mockResolvedValue(undefined),update: jest.fn().mockResolvedValue(undefined),+ remove: jest.fn().mockResolvedValue(undefined),};// clearAllMocks keeps implementations: a store lookup one test taught to return a message must not@@ -8979,4 +8981,628 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'PEER,);});++ // An id names one message of one chat. A delivery that reuses the id of a message the account holds+ // only in its messages table (history-synced, or older than the store's cap) finds nothing in the store,+ // so its copy is stored under that id before the session finds the table already has the id. The+ // session says so through the outcome of onMessage, and the copy must not outlive it: the real+ // author's later edit, delete or reaction would be judged against the wrong chat and dropped.+ describe('a delivery that reuses the id of another message', () => {+ type Copy = { key: { id: string; remoteJid: string; participant?: string; fromMe?: boolean } };+ const OTHER = '[email protected]';+ const copies = new Map<string, Copy>();++ beforeEach(() => {+ copies.clear();+ fakeStore.put.mockReset().mockImplementation((_session: string, msg: Copy) => {+ copies.set(msg.key.id, structuredClone(msg));+ return Promise.resolve();+ });+ fakeStore.getMessage.mockImplementation((_session: string, id: string) =>+ Promise.resolve(structuredClone(copies.get(id) ?? null)),+ );+ fakeStore.update+ .mockReset()+ .mockImplementation((_session: string, id: string, change: (m: Copy) => Copy | null) => {+ const held = copies.get(id);+ const next = held && change(structuredClone(held));+ if (next) copies.set(id, next);+ return Promise.resolve();+ });+ fakeStore.remove.mockReset().mockImplementation((_session: string, id: string, accept: (m: Copy) => boolean) => {+ const held = copies.get(id);+ if (held && accept(structuredClone(held))) copies.delete(id);+ return Promise.resolve();+ });+ });++ afterEach(() => {+ fakeStore.put.mockReset().mockResolvedValue(undefined);+ fakeStore.update.mockReset().mockResolvedValue(undefined);+ fakeStore.remove.mockReset().mockResolvedValue(undefined);+ });++ /** An outcome the test settles itself, so it decides when the session answers. */+ const answer = () => {+ let resolve!: (outcome: InboundOutcome | undefined) => void;+ let reject!: (error: Error) => void;+ const promise = new Promise<InboundOutcome | undefined>((res, rej) => {+ resolve = res;+ reject = rej;+ });+ return { promise, resolve, reject };+ };++ const startWith = async (callbacks: Partial<EngineEventCallbacks>): Promise<BaileysAdapter> => {+ const adapter = newAdapter();+ await adapter.initialize(callbacks);+ fakeSock.fire('connection.update', { connection: 'open' });+ return adapter;+ };++ /** The reusing delivery: a text from another one-to-one chat, under the id of the real message. */+ const deliverReuse = (key: Record<string, unknown> = { remoteJid: OTHER, fromMe: false }) =>+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: [{ key: { id: 'REUSED', ...key }, message: { conversation: 'late' }, messageTimestamp: 1700000100 }],+ });++ const settleAll = async (): Promise<void> => {+ for (let i = 0; i < 6; i++) await settle();+ };++ const realEdit = () =>+ fireProtocol(+ { id: 'EDIT_ENV', remoteJid: PEER, fromMe: false },+ { key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: 'the real edit' } },+ );++ it('removes its own copy once the session says the id belongs to another message', async () => {+ const answered = answer();+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })) });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ deliverReuse();+ await settleAll();+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);++ answered.resolve({ conflict: true });+ await settleAll();++ expect(fakeStore.remove).toHaveBeenCalledWith('db-uuid-1', 'REUSED', expect.any(Function));+ expect(copies.has('REUSED')).toBe(false);+ expect(warn).toHaveBeenCalledWith(+ 'Dropping the stored copy of a message that reuses the id of another message',+ expect.objectContaining({ msgId: 'REUSED', remoteJid: OTHER }),+ );+ });++ it('takes the real message edit, delete and reaction that follow, judged against nothing wrong', async () => {+ const onMessageEdited = jest.fn();+ const onMessageRevoked = jest.fn();+ const onMessageReaction = jest.fn();+ await startWith({+ onMessage: jest.fn(() => ({ settled: Promise.resolve({ conflict: true }) })),+ onMessageEdited,+ onMessageRevoked,+ onMessageReaction,+ });+ deliverReuse();+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);++ realEdit();+ fireProtocol({ id: 'REVOKE_ENV', remoteJid: PEER, fromMe: false }, { key: { id: 'REUSED' }, type: 0 });+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: [+ {+ key: { id: 'REACT_ENV', remoteJid: PEER, fromMe: false },+ message: { reactionMessage: { key: { id: 'REUSED' }, text: '👍' } },+ messageTimestamp: 1700000101,+ },+ ],+ });+ await settleAll();++ expect(onMessageEdited).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),+ );+ expect(onMessageRevoked).toHaveBeenCalledWith(expect.objectContaining({ id: 'REUSED', chatId: '[email protected]' }), {+ fromMe: false,+ });+ expect(onMessageReaction).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),+ );+ });++ it.each([+ ['says nothing', undefined],+ ['reports no conflict', {}],+ ['reports an explicit no', { conflict: false }],+ ])('keeps its copy when the session %s, and keeps judging a change against it', async (_name, outcome) => {+ const onMessageEdited = jest.fn();+ const adapter = await startWith({+ onMessage: jest.fn(() => ({ settled: Promise.resolve(outcome) })),+ onMessageEdited,+ });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ deliverReuse();+ await settleAll();++ expect(fakeStore.remove).not.toHaveBeenCalled();+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);++ // Nothing proved the stored copy is another message's, so the edit from the other chat is foreign to it.+ realEdit();+ await settleAll();+ expect(onMessageEdited).not.toHaveBeenCalled();+ expect(warn).toHaveBeenCalledWith(+ 'Dropping an edit, revoke or reaction aimed at a message from another chat or author',+ expect.objectContaining({ targetId: 'REUSED' }),+ );+ });++ it('removes a copy only while it still carries the key of the delivery that stored it', async () => {+ const answered = answer();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })) });+ deliverReuse({ remoteJid: '[email protected]', participant: OTHER, fromMe: false });+ await settleAll();+ answered.resolve({ conflict: true });+ await settleAll();+ const accept = (fakeStore.remove.mock.calls[0] as [string, string, (m: Copy) => boolean])[2];++ const group = '[email protected]';+ expect(accept({ key: { id: 'REUSED', remoteJid: group, participant: OTHER, fromMe: false } })).toBe(true);+ expect(accept({ key: { id: 'REUSED', remoteJid: group, participant: OTHER } })).toBe(true); // fromMe left out+ expect(accept({ key: { id: 'REUSED', remoteJid: PEER, participant: OTHER, fromMe: false } })).toBe(false);+ expect(accept({ key: { id: 'REUSED', remoteJid: group, participant: PEER, fromMe: false } })).toBe(false);+ expect(accept({ key: { id: 'REUSED', remoteJid: group, participant: OTHER, fromMe: true } })).toBe(false);+ expect(accept({ key: { id: 'REUSED', remoteJid: group, fromMe: false } })).toBe(false);+ });++ it('leaves a copy another write replaced meanwhile', async () => {+ const answered = answer();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })) });+ deliverReuse();+ await settleAll();+ // The real message's own copy took the id's place before the session answered.+ copies.set('REUSED', { key: { id: 'REUSED', remoteJid: PEER, fromMe: false } });++ answered.resolve({ conflict: true });+ await settleAll();++ expect(fakeStore.remove).toHaveBeenCalledTimes(1);+ expect(copies.get('REUSED')?.key.remoteJid).toBe(PEER);+ });++ it('judges a change aimed at a status post by its copy, which the session does not hold, and still removes it', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ deliverReuse({ remoteJid: 'status@broadcast', participant: OTHER, fromMe: false });+ await settleAll();+ expect(copies.get('REUSED')?.key.remoteJid).toBe('status@broadcast');++ realEdit();+ await settleAll();+ expect(onMessageEdited).not.toHaveBeenCalled();+ expect(warn).toHaveBeenCalledWith(+ 'Dropping an edit, revoke or reaction aimed at a message from another chat or author',+ expect.objectContaining({ targetId: 'REUSED' }),+ );++ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);+ });++ it('hands the real edit to the session while it has not answered, then removes the copy', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });+ deliverReuse();+ await settleAll();++ // The edit reads as foreign to the stored copy, which is the delivery the session has yet to judge.+ realEdit();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),+ );+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);++ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);+ });++ // The read of the real edit runs on the store's clock, not the session's: it can begin under the hold and+ // return the copy only after the answer removed it and ended the hold.+ it('hands the real edit to the session when its store read outlasts the hold it began under', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });+ deliverReuse();+ await settleAll();++ let openGate!: () => void;+ const gate = new Promise<void>(resolve => (openGate = resolve));+ fakeStore.getMessage.mockImplementationOnce(async (_session: string, id: string) => {+ const seen = structuredClone(copies.get(id) ?? null);+ await gate;+ return seen;+ });+ realEdit();+ await settleAll();+ expect(onMessageEdited).not.toHaveBeenCalled();++ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);++ openGate();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),+ );+ });++ it('hands the real edit to the session when its store read began before the delivery was stored', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });++ let reading = false;+ let openGate!: () => void;+ const gate = new Promise<void>(resolve => (openGate = resolve));+ fakeStore.getMessage.mockImplementationOnce(async (_session: string, id: string) => {+ reading = true;+ await gate;+ return structuredClone(copies.get(id) ?? null);+ });+ realEdit();+ await settleAll();+ expect(reading).toBe(true);++ deliverReuse();+ await settleAll();+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);++ openGate();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),+ );+ });++ it('reads again when the copy it returned was removed after the hold it began under had ended', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });++ // The read starts before the delivery is stored, sees its copy, and returns only after the answer+ // has removed that copy and ended the hold.+ let stored!: () => void;+ const copyStored = new Promise<void>(resolve => (stored = resolve));+ let openGate!: () => void;+ const gate = new Promise<void>(resolve => (openGate = resolve));+ fakeStore.getMessage.mockImplementationOnce(async (_session: string, id: string) => {+ await copyStored;+ const seen = structuredClone(copies.get(id) ?? null);+ await gate;+ return seen;+ });+ realEdit();+ await settleAll();++ deliverReuse();+ await settleAll();+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);+ stored();+ await settleAll();++ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);++ openGate();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),+ );+ });++ it('still drops a change from a third chat when the answer is no conflict and its read began under the hold', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });+ deliverReuse();+ await settleAll();++ let openGate!: () => void;+ const gate = new Promise<void>(resolve => (openGate = resolve));+ fakeStore.getMessage.mockImplementationOnce(async (_session: string, id: string) => {+ const seen = structuredClone(copies.get(id) ?? null);+ await gate;+ return seen;+ });+ fireProtocol(+ { id: 'FORGED_ENV', remoteJid: '[email protected]', fromMe: false },+ { key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: 'forged' } },+ );+ await settleAll();++ answered.resolve({ conflict: false });+ await settleAll();+ expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);++ openGate();+ await settleAll();+ expect(onMessageEdited).not.toHaveBeenCalled();+ });++ it('keeps the id held until the removal of the copy has run', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });+ deliverReuse();+ await settleAll();++ // The delete is queued behind other writes of the id and has not run when the answer comes.+ let runRemoval!: () => void;+ const removalQueued = new Promise<void>(resolve => (runRemoval = resolve));+ fakeStore.remove.mockImplementationOnce(async (_session: string, id: string) => {+ await removalQueued;+ copies.delete(id);+ });+ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(true);++ realEdit();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),+ );+ runRemoval();+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);+ });++ it('applies the edit of the delivery itself to its copy before the answer, and still removes it', async () => {+ const answered = answer();+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });+ deliverReuse();+ await settleAll();++ fireProtocol(+ { id: 'OWN_EDIT_ENV', remoteJid: OTHER, fromMe: false },+ { key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: 'late, edited' } },+ );+ await settleAll();+ expect(fakeStore.update).toHaveBeenCalledWith('db-uuid-1', 'REUSED', expect.any(Function));+ expect(onMessageEdited).toHaveBeenCalledTimes(1);++ answered.resolve({ conflict: true });+ await settleAll();+ expect(copies.has('REUSED')).toBe(false);+ });++ it('stops judging in the session once every delivery of the id has been answered', async () => {+ const first = answer();+ const second = answer();+ const outcomes = [first.promise, second.promise];+ const onMessageEdited = jest.fn();+ await startWith({ onMessage: jest.fn(() => ({ settled: outcomes.shift()! })), onMessageEdited });+ // Both pass the repeat check before either is stored, as a repeat sent while the first is processed does.+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: [1, 2].map(() => ({+ key: { id: 'REUSED', remoteJid: OTHER, fromMe: false },+ message: { conversation: 'late' },+ messageTimestamp: 1700000100,+ })),+ });+ await settleAll();+ expect(fakeStore.put).toHaveBeenCalledTimes(2);++ first.resolve(undefined);+ await settleAll();+ realEdit();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledTimes(1); // the second delivery is still unanswered++ second.resolve(undefined);+ await settleAll();+ realEdit();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledTimes(1); // now judged against the copy again+ });++ describe('an answer that does not come', () => {+ const HOLD_MS = 5 * 60_000;+ const edited = 'Dropping an edit, revoke or reaction aimed at a message from another chat or author';++ beforeEach(() => {+ jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] });+ });++ afterEach(() => {+ jest.useRealTimers();+ });++ it('stops holding the id for the session after a while, so a change is judged against the copy again', async () => {+ const onMessageEdited = jest.fn();+ const adapter = await startWith({+ onMessage: jest.fn(() => ({ settled: new Promise<InboundOutcome | undefined>(() => undefined) })),+ onMessageEdited,+ });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ deliverReuse();+ await settleAll();++ realEdit();+ await settleAll();+ expect(onMessageEdited).toHaveBeenCalledTimes(1);++ jest.advanceTimersByTime(HOLD_MS);+ realEdit();+ await settleAll();++ expect(onMessageEdited).toHaveBeenCalledTimes(1);+ expect(warn).toHaveBeenCalledWith(edited, expect.objectContaining({ targetId: 'REUSED' }));+ });++ it('still removes the copy when the session answers after the hold ended', async () => {+ const answered = answer();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })) });+ deliverReuse();+ await settleAll();+ jest.advanceTimersByTime(HOLD_MS);++ answered.resolve({ conflict: true });+ await settleAll();++ expect(copies.has('REUSED')).toBe(false);+ });++ it('lets a late answer of one delivery leave the hold of a later delivery of the id alone', async () => {+ const first = answer();+ const second = answer();+ const outcomes = [first.promise, second.promise];+ const onMessageEdited = jest.fn();+ const adapter = await startWith({+ onMessage: jest.fn(() => ({ settled: outcomes.shift()! })),+ onMessageEdited,+ });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ deliverReuse();+ await settleAll();+ jest.advanceTimersByTime(60_000);+ copies.delete('REUSED'); // the store let it go, so the repeat is not skipped as one it holds+ deliverReuse();+ await settleAll();++ jest.advanceTimersByTime(HOLD_MS - 60_000); // the first delivery's hold ends, the second's has a minute left+ first.resolve(undefined);+ await settleAll();+ realEdit();+ await settleAll();++ expect(onMessageEdited).toHaveBeenCalledTimes(1);+ expect(warn).not.toHaveBeenCalledWith(edited, expect.anything());+ });++ it('frees the hold at once when the answer comes', async () => {+ const answered = answer();+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })) });+ const idle = jest.getTimerCount();+ deliverReuse();+ await settleAll();+ expect(jest.getTimerCount()).toBeGreaterThan(idle);++ answered.resolve(undefined);+ await settleAll();++ expect(jest.getTimerCount()).toBe(idle);+ });+ });++ it('is not held up by an answer that rejects, and warns once', async () => {+ const unhandled: unknown[] = [];+ const listen = (reason: unknown): number => unhandled.push(reason);+ process.on('unhandledRejection', listen);+ try {+ const onMessage = jest+ .fn()+ .mockImplementationOnce(() => ({ settled: Promise.reject(new Error('projector went away')) }))+ .mockImplementation(() => undefined);+ const adapter = await startWith({ onMessage });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ deliverReuse();+ await settleAll();+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: [+ {+ key: { id: 'NEXT', remoteJid: PEER, fromMe: false },+ message: { conversation: 'hi' },+ messageTimestamp: 1,+ },+ ],+ });+ await settleAll();++ expect(unhandled).toEqual([]);+ expect(warn).toHaveBeenCalledWith(+ 'Could not act on what the session made of a message',+ expect.objectContaining({ msgId: 'REUSED', error: 'projector went away' }),+ );+ expect(onMessage).toHaveBeenCalledTimes(2);+ expect(copies.has('REUSED')).toBe(true);+ expect(copies.has('NEXT')).toBe(true);+ } finally {+ process.off('unhandledRejection', listen);+ }+ });++ it('is not held up by a callback that throws, or one that answers with something else', async () => {+ const onMessage = jest+ .fn()+ .mockImplementationOnce(() => {+ throw new Error('callback exploded');+ })+ .mockImplementationOnce(() => 'accepted')+ .mockImplementation(() => undefined);+ await startWith({ onMessage });+ for (const id of ['A1', 'A2', 'A3']) {+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: [+ { key: { id, remoteJid: PEER, fromMe: false }, message: { conversation: id }, messageTimestamp: 1 },+ ],+ });+ await settleAll();+ }++ expect(onMessage).toHaveBeenCalledTimes(3);+ expect(fakeStore.remove).not.toHaveBeenCalled();+ });++ it('logs a store that cannot remove the copy instead of failing', async () => {+ const unhandled: unknown[] = [];+ const listen = (reason: unknown): number => unhandled.push(reason);+ process.on('unhandledRejection', listen);+ try {+ fakeStore.remove.mockReset().mockRejectedValueOnce(new Error('SQLITE_BUSY: database is locked'));+ const adapter = await startWith({+ onMessage: jest.fn(() => ({ settled: Promise.resolve({ conflict: true }) })),+ });+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);+ deliverReuse();+ await settleAll();++ expect(unhandled).toEqual([]);+ expect(warn).toHaveBeenCalledWith(+ 'Failed to remove the stored copy of a conflicting message',+ expect.objectContaining({ msgId: 'REUSED', error: 'SQLITE_BUSY: database is locked' }),+ );+ } finally {+ process.off('unhandledRejection', listen);+ }+ });++ it('does nothing for a callback that returns no answer, as before', async () => {+ const onMessage = jest.fn();+ await startWith({ onMessage });+ deliverReuse();+ await settleAll();++ expect(onMessage).toHaveBeenCalledTimes(1);+ expect(fakeStore.remove).not.toHaveBeenCalled();+ expect(copies.has('REUSED')).toBe(true);+ });+ });});diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.tsindex 4e7a5a7bd..5d9ddc091 100644--- a/src/engine/adapters/baileys.adapter.ts+++ b/src/engine/adapters/baileys.adapter.ts@@ -146,6 +146,8 @@ export class BaileysAdapter implements IWhatsAppEngine {putStoredMessage: msg => this.config.messageStore?.put(this.config.dbSessionId, msg),updateStoredMessage: (messageId, change) =>this.config.messageStore?.update(this.config.dbSessionId, messageId, change),+ removeStoredMessage: (messageId, shouldRemove) =>+ this.config.messageStore?.remove(this.config.dbSessionId, messageId, shouldRemove),wasDeletedForEveryone: messageId => this.events.wasDeletedForEveryone(messageId),markDeletedForEveryone: messageId => this.events.markDeletedForEveryone(messageId),applyHistoryRevoke: (target, envelope) => this.events.applyHistoryRevoke(target, envelope),diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.tsindex bd934edac..5e0515e61 100644--- a/src/engine/adapters/inbound-hold.spec.ts+++ b/src/engine/adapters/inbound-hold.spec.ts@@ -34,8 +34,11 @@ const flush = async (): Promise<void> => {};const sleep = (ms: number): Promise<void> => new Promise(resolve => setTimeout(resolve, ms));-/** The real projector over a stand-in `messages` table whose inserts a test can stall per message. */-function heldProjector() {+/**+ * The real projector over a stand-in `messages` table whose inserts a test can stall per message. With+ * `unique`, an insert of an id the table already holds fails as the real table's unique index does.+ */+function heldProjector(options: { unique?: boolean } = {}) {const rows: Row[] = [];const hookStarts: string[] = [];const hookedBodies: Record<string, string> = {};@@ -49,6 +52,9 @@ function heldProjector() {create: jest.fn((x: Row) => ({ ...x })),insert: jest.fn(async (row: Row) => {await stalls.get(String(row.waMessageId));+ if (options.unique && rows.some(r => r.waMessageId === row.waMessageId)) {+ throw Object.assign(new Error('UNIQUE constraint failed'), { code: 'SQLITE_CONSTRAINT_UNIQUE' });+ }rows.push({ ...row });return { identifiers: [{ id: `pk-${String(row.waMessageId)}` }], generatedMaps: [] };}),@@ -156,7 +162,31 @@ describe('Baileys inbound messages held for storage', () => {destroy: jest.fn(),});- const build = (concurrency: number, callbacks?: EngineEventCallbacks) => {+ /** The adapter's message store, in memory: what the host reads, writes and deletes copies through. */+ const memoryStore = () => {+ const copies = new Map<string, WAMessage>();+ return {+ copies,+ getStoredMessage: (id: string) => Promise.resolve(structuredClone(copies.get(id) ?? null)),+ putStoredMessage: (msg: WAMessage) => {+ copies.set(msg.key.id!, structuredClone(msg));+ return Promise.resolve();+ },+ updateStoredMessage: (id: string, change: (stored: WAMessage) => WAMessage | null) => {+ const held = copies.get(id);+ const next = held && change(structuredClone(held));+ if (next) copies.set(id, next);+ return Promise.resolve();+ },+ removeStoredMessage: (id: string, accept: (stored: WAMessage) => boolean) => {+ const held = copies.get(id);+ if (held && accept(structuredClone(held))) copies.delete(id);+ return Promise.resolve();+ },+ };+ };++ const build = (concurrency: number, callbacks?: EngineEventCallbacks, store?: ReturnType<typeof memoryStore>) => {const emitted: string[] = [];const recordKeyLidMappings = jest.fn();const host = {@@ -179,7 +209,7 @@ describe('Baileys inbound messages held for storage', () => {getAdmitInbound: () => callbacks?.admitInbound,getOnMessage: () => (m: IncomingMessage, t?: never) => {emitted.push(m.id);- callbacks?.onMessage?.(m, t);+ return callbacks?.onMessage?.(m, t);},getOnMessageCreate: () => callbacks?.onMessageCreate,getOnMessageRevoked: () => callbacks?.onMessageRevoked,@@ -190,6 +220,7 @@ describe('Baileys inbound messages held for storage', () => {getOnCall: () => undefined,getOnPresenceUpdate: () => undefined,getOnCallOutcome: () => undefined,+ ...store,} as unknown as BaileysEventsHost;return { events: new BaileysEvents(host), emitted, recordKeyLidMappings };};@@ -636,6 +667,101 @@ describe('Baileys inbound messages held for storage', () => {expect(held.recordKeyLidMappings).toHaveBeenCalledTimes(1);expect(plain.recordKeyLidMappings).toHaveBeenCalledTimes(1);});++ // A message the table holds from history is not in the adapter's store, so a delivery that reuses its id+ // from another chat is stored there under its own key before the table refuses its row.+ describe('a delivery that reuses the id of a message the table holds', () => {+ const CHAT_A = '[email protected]';+ const CHAT_B = '[email protected]';+ const seeded = (): Row => ({ waMessageId: 'X', chatId: CHAT_A, direction: 'incoming', body: 'the real one' });+ /** An edit of X sent from `chat`, under an envelope id of its own. */+ const editFrom = (chat: string, envelopeId: string, body: string): WAMessage => ({+ key: { id: envelopeId, remoteJid: chat, fromMe: false },+ messageTimestamp: 1_700_000_002,+ message: {+ protocolMessage: {+ type: 14,+ key: { id: 'X', remoteJid: chat, fromMe: false },+ editedMessage: { conversation: body },+ timestampMs: 1_700_000_002_000,+ },+ },+ });+ const realEdit = (): WAMessage => editFrom(CHAT_A, 'EDIT_ENV', 'fixed');++ it('removes the copy it stored, so the real message edit lands on the row', async () => {+ const h = heldProjector({ unique: true });+ h.rows.push(seeded());+ const store = memoryStore();+ const { events } = build(4, h.callbacks, store);++ upsert(events, text('X', CHAT_B));+ await flush();++ // The row is not touched and nothing is announced; the copy that stood in for it is gone.+ expect(h.rows).toEqual([seeded()]);+ expect(h.announced()).toEqual([]);+ expect(store.copies.has('X')).toBe(false);++ upsert(events, realEdit());+ await flush();+ expect(h.rows[0]).toEqual(expect.objectContaining({ body: 'fixed' }));+ });++ it('keeps the copy of a re-fire of the message the table holds', async () => {+ const h = heldProjector({ unique: true });+ h.rows.push({ ...seeded(), chatId: CHAT_B });+ const store = memoryStore();+ const { events } = build(4, h.callbacks, store);++ upsert(events, text('X', CHAT_B));+ await flush();++ expect(store.copies.get('X')?.key.remoteJid).toBe(CHAT_B);+ expect(h.announced()).toEqual([]);+ });++ it('hands the real edit to the session while the repeat is held, and removes the copy once it is refused', async () => {+ const h = heldProjector({ unique: true });+ h.rows.push(seeded());+ const release = h.stall('X');+ const store = memoryStore();+ const { events } = build(4, h.callbacks, store);++ upsert(events, text('X', CHAT_B));+ await flush();+ expect(store.copies.get('X')?.key.remoteJid).toBe(CHAT_B);++ // Judged against the copy alone it would be foreign, and lost with the copy's removal still to come.+ upsert(events, realEdit());+ await flush();+ expect(h.rows[0]).toEqual(expect.objectContaining({ body: 'fixed' }));+ expect(store.copies.has('X')).toBe(true);++ release();+ await flush();+ expect(store.copies.has('X')).toBe(false);+ expect(h.rows).toEqual([expect.objectContaining({ chatId: CHAT_A, body: 'fixed' })]);+ });++ it('still drops an edit from a third chat, before and after the session answers', async () => {+ const h = heldProjector({ unique: true });+ h.rows.push(seeded());+ const release = h.stall('X');+ const { events } = build(4, h.callbacks, memoryStore());++ upsert(events, text('X', CHAT_B));+ await flush();+ upsert(events, editFrom('[email protected]', 'FORGED_1', 'forged'));+ await flush();+ release();+ await flush();+ upsert(events, editFrom('[email protected]', 'FORGED_2', 'forged again'));+ await flush();++ expect(h.rows).toEqual([seeded()]);+ });+ });});describe('whatsapp-web.js inbound messages held for storage', () => {diff --git a/src/engine/builtin/baileys/index.spec.ts b/src/engine/builtin/baileys/index.spec.tsindex dd7099a40..e45f7020a 100644--- a/src/engine/builtin/baileys/index.spec.ts+++ b/src/engine/builtin/baileys/index.spec.ts@@ -69,6 +69,7 @@ describe('BaileysPlugin.createEngine (opaque config)', () => {getMessage: jest.fn(),getMessages: jest.fn(),update: jest.fn(),+ remove: jest.fn(),clearSession: jest.fn(),};const plugin = new BaileysPlugin(store);diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.tsindex dd7b69fc4..1b017a4c0 100644--- a/src/engine/interfaces/whatsapp-engine.interface.ts+++ b/src/engine/interfaces/whatsapp-engine.interface.ts@@ -850,6 +850,30 @@ export interface InboundTicket {readonly revoked: boolean;}+/**+ * What the session made of a message handed to `EngineEventCallbacks.onMessage`, for an engine that+ * keeps its own copy of messages keyed by id. An engine that keeps none ignores it.+ */+export interface InboundOutcome {+ /**+ * The session already holds a different message under this id (another chat, another side of the+ * conversation, or another author), so the delivery was dropped as a duplicate. The engine's copy+ * under that id is this delivery's, not the message the session holds, and must not stand in for it.+ * Left out for a delivery that landed and for one the session cannot prove is another message.+ */+ conflict?: boolean;+}++/**+ * The session's answer to `onMessage`, for an engine that wants to know what became of the message.+ * Wrapped rather than a bare promise: most engines ignore it, and a promise nobody awaits reads as a+ * mistake wherever a callback is called.+ */+export interface InboundReceipt {+ /** Settles once the message's row is settled, with what became of it; never rejects. */+ settled: Promise<InboundOutcome | undefined>;+}+export interface EngineEventCallbacks {onQRCode?: (qr: string) => void;onReady?: (phone: string, pushName: string) => void;@@ -869,7 +893,12 @@ export interface EngineEventCallbacks {/** The neutral id of the sender behind a group, status or list message, as `IncomingMessage.author`. */author?: string;}) => InboundTicket | undefined;- onMessage?: (message: IncomingMessage, ticket?: InboundTicket) => void;+ /**+ * Fired for each received message. The session may answer with a receipt for what it makes of the+ * message once its row is settled (see {@link InboundReceipt}); the engine never waits for it, and+ * a receipt that fails, or is still unsettled after minutes, is let go of.+ */+ onMessage?: (message: IncomingMessage, ticket?: InboundTicket) => void | InboundReceipt;/*** Fired for messages the account itself created (outgoing) — including sends composed on a* linked phone, which the `message`/`onMessage` event never delivers. Used to emit `message.sent`.diff --git a/src/engine/types/baileys.types.ts b/src/engine/types/baileys.types.tsindex ec3923e76..88767ffc6 100644--- a/src/engine/types/baileys.types.ts+++ b/src/engine/types/baileys.types.ts@@ -25,6 +25,11 @@ export interface BaileysMessageStore {* replacement, or null to leave it untouched; an id the store does not hold stays absent.*/update(sessionId: string, messageId: string, change: (stored: WAMessage) => WAMessage | null): Promise<void>;+ /**+ * Delete a stored message, but only while `shouldRemove` accepts the copy held when the delete runs.+ * An id the store does not hold stays absent. Queued behind any put() or update() of the same id.+ */+ remove(sessionId: string, messageId: string, shouldRemove: (stored: WAMessage) => boolean): Promise<void>;/** Remove all stored messages for a session (called on logout). */clearSession(sessionId: string): Promise<void>;}diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex da796f8c3..188fb1172 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -1547,6 +1547,22 @@ describe('MessageProjector (inbound projection)', () => {expect(received()).toHaveLength(1);});+ it('reports nothing for a duplicate of the retry, which may be the message itself', async () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ messageRepository.insert.mockRejectedValueOnce(busy()).mockRejectedValueOnce(dup());+ messageRepository.findOne.mockResolvedValue({ chatId: '[email protected]', direction: 'incoming', author: null });++ const receipt = projector.handleInboundMessage(SESSION_ID, engine, makeIncoming());+ await flush();+ await jest.advanceTimersByTimeAsync(PERSIST_RETRY_DELAY_MS);+ await flush();++ await expect(receipt?.settled).resolves.toBeUndefined();+ expect(messageRepository.findOne).not.toHaveBeenCalled();+ expect(received()).toHaveLength(1);+ });+it('still clears that row when a revoke landed while its hook chain ran', async () => {const engine = makeEngine();engines.set(SESSION_ID, engine);@@ -1630,6 +1646,185 @@ describe('MessageProjector (inbound projection)', () => {});});+ // The table holds one row per id, so a delivery that reuses the id of a message it holds is refused as a+ // repeat. A re-fire of the same message is a repeat too, and the engine keeps its copy of that one; an id+ // reused from another chat, side or author is not the message the row holds, and the engine is told so.+ describe('a repeat of an id the table already holds', () => {+ const dup = (): Error => Object.assign(new Error('UNIQUE constraint failed'), { code: 'SQLITE_CONSTRAINT_UNIQUE' });+ const flush = async (): Promise<void> => {+ for (let i = 0; i < 5; i++) await new Promise(resolve => setImmediate(resolve));+ };+ const received = (): unknown[] => webhookService.dispatch.mock.calls.filter(([, e]) => e === 'message.received');++ /** Hand over a delivery whose insert the table refuses; `row` is what it holds under the id. */+ const repeat = async (+ over: Partial<IncomingMessage>,+ row?: Record<string, unknown> | null | Error,+ ): Promise<unknown> => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ messageRepository.insert.mockRejectedValueOnce(dup());+ if (row instanceof Error) messageRepository.findOne.mockRejectedValue(row);+ else if (row !== undefined) messageRepository.findOne.mockResolvedValue(row);+ const receipt = projector.handleInboundMessage(SESSION_ID, engine, makeIncoming(over));+ await flush();+ return receipt?.settled;+ };++ it('reports a conflict when the row is another chat', async () => {+ const outcome = await repeat({}, { chatId: '[email protected]', direction: 'incoming', author: null });++ expect(outcome).toEqual({ conflict: true });+ expect(messageRepository.findOne).toHaveBeenCalledWith(+ expect.objectContaining({ where: { sessionId: SESSION_ID, waMessageId: 'wamid.1' } }),+ );+ expect(received()).toHaveLength(0);+ });++ it('reports a conflict when the row is a message the account sent', async () => {+ const outcome = await repeat({}, { chatId: '[email protected]', direction: 'outgoing', author: null });++ expect(outcome).toEqual({ conflict: true });+ expect(received()).toHaveLength(0);+ });++ it('reports a conflict when the row was written by another member of the group', async () => {+ const outcome = await repeat(+ { chatId: '[email protected]', from: '[email protected]', isGroup: true, kind: 'group', author: '[email protected]' },+ { chatId: '[email protected]', direction: 'incoming', author: '[email protected]' },+ );++ expect(outcome).toEqual({ conflict: true });+ });++ it('reports nothing for a re-fire of the message the row holds', async () => {+ const outcome = await repeat({}, { chatId: '[email protected]', direction: 'incoming', author: null });++ expect(outcome).toBeUndefined();+ expect(received()).toHaveLength(0);+ });++ it('reports nothing for a re-fire of a group message by its own author', async () => {+ const outcome = await repeat(+ { chatId: '[email protected]', from: '[email protected]', isGroup: true, kind: 'group', author: '[email protected]' },+ { chatId: '[email protected]', direction: 'incoming', author: '[email protected]' },+ );++ expect(outcome).toBeUndefined();+ });++ it('reports nothing when the row names the same chat in the other id dialect', async () => {+ const outcome = await repeat(+ { chatId: '[email protected]' },+ { chatId: '[email protected]', direction: 'incoming', author: null },+ );++ expect(outcome).toBeUndefined();+ });++ it('reports nothing for a chat it cannot compare with the row, as a lid against a phone number', async () => {+ const outcome = await repeat(+ { chatId: '99887766@lid' },+ { chatId: '[email protected]', direction: 'incoming', author: null },+ );++ expect(outcome).toBeUndefined();+ });++ it('judges the message a hook left, as the row it would have written', async () => {+ hookManager.execute.mockImplementation((_event: string, data: IncomingMessage) =>+ Promise.resolve({ continue: true, data: { ...data, chatId: '[email protected]' } }),+ );++ const outcome = await repeat({}, { chatId: '[email protected]', direction: 'incoming', author: null });++ expect(outcome).toBeUndefined();+ });++ it.each([+ ['has no row to read', null],+ ['cannot be read', new Error('SQLITE_BUSY: database is locked')],+ ])('reports nothing when the id %s, so the engine keeps its copy', async (_name, row) => {+ const outcome = await repeat({}, row);++ expect(outcome).toBeUndefined();+ expect(received()).toHaveLength(0);+ });++ it('reports nothing for a message whose row landed', async () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);++ const receipt = projector.handleInboundMessage(SESSION_ID, engine, makeIncoming());+ await flush();++ await expect(receipt?.settled).resolves.toBeUndefined();+ expect(messageRepository.findOne).not.toHaveBeenCalled();+ expect(received()).toHaveLength(1);+ });++ it('answers nothing for a message it never commits', () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ const retired = makeEngine();++ expect(projector.handleInboundMessage(SESSION_ID, retired, makeIncoming())).toBeUndefined();+ });++ it('settles with nothing when the hook chain fails', async () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ hookManager.execute.mockRejectedValueOnce(new Error('hook exploded'));++ const receipt = projector.handleInboundMessage(SESSION_ID, engine, makeIncoming());+ await flush();++ await expect(receipt?.settled).resolves.toBeUndefined();+ expect(messageRepository.insert).not.toHaveBeenCalled();+ });++ describe('a status post', () => {+ const status = { isStatusBroadcast: true, chatId: 'status@broadcast', from: 'status@broadcast' };++ it('reports a conflict when a message of a chat holds its id', async () => {+ statusStore.ingest.mockResolvedValue({ row: {}, created: true });+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ messageRepository.findOne.mockResolvedValue({+ chatId: '[email protected]',+ direction: 'incoming',+ author: null,+ });++ const receipt = projector.handleInboundMessage(+ SESSION_ID,+ engine,+ makeIncoming({ ...status, author: '[email protected]' }),+ );+ await flush();++ await expect(receipt?.settled).resolves.toEqual({ conflict: true });+ expect(statusStore.ingest).toHaveBeenCalledTimes(1);+ });++ it('reports nothing for a story no message shares an id with', async () => {+ statusStore.ingest.mockResolvedValue({ row: {}, created: true });+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);++ const receipt = projector.handleInboundMessage(+ SESSION_ID,+ engine,+ makeIncoming({ ...status, author: '[email protected]' }),+ );+ await flush();++ await expect(receipt?.settled).resolves.toBeUndefined();+ expect(statusStore.ingest).toHaveBeenCalledTimes(1);+ });+ });+ });+describe('handleOwnSendEcho', () => {it('lends a quote the body its message:sent chain rewrote, until the row is written', async () => {const engine = makeEngine();diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 8170fecde..dace336f1 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -24,6 +24,8 @@ import {IWhatsAppEngine,DeliveryStatus,IncomingMessage,+ InboundOutcome,+ InboundReceipt,InboundTicket,ReactionEvent,EditedMessage,@@ -341,24 +343,38 @@ export class MessageProjector {/*** Engine callback body, lifted out of initializeEngine so the wiring table stays readable. With a* ticket (PostgreSQL), the hook chain starts in the chat's arrival order and the commit takes the- * turn admitInbound reserved; whatever path returns early drops the ticket.+ * turn admitInbound reserved; whatever path returns early drops the ticket. The receipt, for an+ * engine that wants to know what became of its message, settles once the commit ends (see+ * {@link InboundOutcome}); there is none when the message is not committed at all.*/- handleInboundMessage(id: string, engine: IWhatsAppEngine, message: IncomingMessage, ticket?: InboundTicket): void {+ handleInboundMessage(+ id: string,+ engine: IWhatsAppEngine,+ message: IncomingMessage,+ ticket?: InboundTicket,+ ): InboundReceipt | undefined {const turn = ticket instanceof InboundTurn ? ticket : undefined;try {- this.receiveInbound(id, engine, message, turn);+ return this.receiveInbound(id, engine, message, turn);} finally {turn?.drop();}}- private receiveInbound(id: string, engine: IWhatsAppEngine, message: IncomingMessage, turn?: InboundTurn): void {+ private receiveInbound(+ id: string,+ engine: IWhatsAppEngine,+ message: IncomingMessage,+ turn?: InboundTurn,+ ): InboundReceipt | undefined {if (!this.engines.isLive(id, engine)) return;if (message.isStatusBroadcast) {const ingest = this.ingestInboundStatus(id, engine, message);// The status store holds the payload until the ingest settles.turn?.holdUntil(ingest);- return;+ // Its own table takes the status whatever the id, and a story is never a row of `messages`: one+ // that finds a message there under its id reuses that id.+ return { settled: ingest.then(() => this.judgeRepeat(id, message)) };}if (this.shouldSkipEphemeralMessage(id, message)) return;this.logger.debug(`Message received from ${message.from}`, {@@ -400,16 +416,19 @@ export class MessageProjector {this.prepareInboundMessage(id, this.messageOrEngineCopy(id, 'message:received', data, message)),),).catch(onFailure);+ const outcome = deferred<InboundOutcome | undefined>();this.commitInTurn(id, message, turn, async () => {try {const finalMessage = await prepared;- if (finalMessage) await this.commitInboundMessage(id, engine, finalMessage);+ if (finalMessage) outcome.resolve(await this.commitInboundMessage(id, engine, finalMessage));} catch (err) {onFailure(err);} finally {+ outcome.resolve(undefined); // no effect once settled aboveif (!turn) this.untrackInFlight(inFlightKey, inFlight);}});+ return { settled: outcome.promise };}/**@@ -607,21 +626,48 @@ export class MessageProjector {id: string,engine: IWhatsAppEngine,finalMessage: InboundMessageData,- ): Promise<void> {+ ): Promise<InboundOutcome | undefined> {const outcome = await this.persistInboundMessage(id, engine, finalMessage);- if (!outcome) return;- this.dispatchInboundMessage(id, finalMessage, outcome);+ if (outcome === 'repeat') return this.judgeRepeat(id, finalMessage);+ if (outcome) this.dispatchInboundMessage(id, finalMessage, outcome);+ return undefined;+ }++ /**+ * Whether a message the insert refused as a repeat is another message than the one holding its id.+ * A re-fire of the same message is not, but an id reused from another chat, another side of the+ * conversation or another author of a group is: the engine is told, so its copy under that id does+ * not stand in for the stored message. Judged by the rule that refuses a change aimed at the row, so+ * what that rule would refuse to edit is not this message. A row that cannot be read, or that does+ * 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,+ });+ if (!reason) return undefined;+ this.logger.warn('Dropping a message that reuses the id of a different stored message', {+ sessionId: id,+ messageId: message.id,+ reason,+ action: 'message_id_reused',+ });+ return { conflict: true };}/*** Build the inbound row and insert it under the UNIQUE dedup oracle. Returns the row plus whether- * the insert landed, or null when the pipeline must stop (stale engine, or duplicate re-fire).+ * the insert landed, 'repeat' when the id already has a row (an engine re-fire, or another message+ * under the same id), or null when the pipeline must stop (stale engine).*/private async persistInboundMessage(id: string,engine: IWhatsAppEngine,incoming: IncomingMessage,- ): Promise<InboundPersistOutcome | null> {+ ): Promise<InboundPersistOutcome | 'repeat' | null> {const metadata = buildMessageMetadata(incoming);const chatName = incoming.contact?.pushName ?? incoming.contact?.name ?? undefined;@@ -666,7 +712,7 @@ 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) return null;+ if (outcome.landed === 'dup' && !outcome.retried) return 'repeat';if (outcome.landed === 'dup') this.applyChangesMadeInFlight(id, incoming.id);return { dbMessage, persisted: false };}diff --git a/src/modules/session/session-engine-event-wiring.ts b/src/modules/session/session-engine-event-wiring.tsindex 0892ea3e2..2d87c5a61 100644--- a/src/modules/session/session-engine-event-wiring.ts+++ b/src/modules/session/session-engine-event-wiring.ts@@ -237,7 +237,7 @@ export class SessionEngineEventWiring {...(host.messages.holdsInbound? { admitInbound: arrival => host.messages.admitInbound(id, engine, arrival) }: {}),- onMessage: (message, ticket): void => host.messages.handleInboundMessage(id, engine, message, ticket),+ onMessage: (message, ticket) => host.messages.handleInboundMessage(id, engine, message, ticket),onHistoryMessages: messages => {if (!host.isLiveEngine(id, engine)) return;// Persist for the chat view only; no dispatch (these predate the live session).diff --git a/src/modules/session/session-inbound-outcome-wiring.spec.ts b/src/modules/session/session-inbound-outcome-wiring.spec.tsnew file mode 100644index 000000000..cc0043a56--- /dev/null+++ b/src/modules/session/session-inbound-outcome-wiring.spec.ts@@ -0,0 +1,36 @@+import { SessionEngineEventWiring, SessionEngineWiringHost } from './session-engine-event-wiring';+import type { IncomingMessage, IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';+import { createLogger } from '../../common/services/logger.service';++describe('onMessage wiring', () => {+ const message = { id: 'M1', chatId: '[email protected]', body: 'hi' } as IncomingMessage;++ const setup = (answer: unknown) => {+ const engine = {} as IWhatsAppEngine;+ const handleInboundMessage = jest.fn(() => answer);+ const host = { isLiveEngine: () => true, messages: { handleInboundMessage } } as unknown as SessionEngineWiringHost;+ const callbacks = new SessionEngineEventWiring({ logger: createLogger('test') }).buildCallbacks(+ 'sess-1',+ engine,+ 'sess-1-name',+ host,+ );+ return { callbacks, engine, handleInboundMessage };+ };++ it('answers the engine with the receipt the projector gave for the message', () => {+ const receipt = { settled: Promise.resolve({ conflict: true }) };+ const { callbacks, engine, handleInboundMessage } = setup(receipt);++ const returned = callbacks.onMessage?.(message);++ expect(returned).toBe(receipt);+ expect(handleInboundMessage).toHaveBeenCalledWith('sess-1', engine, message, undefined);+ });++ it('answers nothing when the projector commits nothing', () => {+ const { callbacks } = setup(undefined);++ expect(callbacks.onMessage?.(message)).toBeUndefined();+ });+});