Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): mark a message the engine handed over edited
Yudhi Armyndharis1e9a1d7a43cf
diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.tsindex f1a280a22..d23db2cea 100644--- a/src/engine/adapters/inbound-hold.spec.ts+++ b/src/engine/adapters/inbound-hold.spec.ts@@ -1557,6 +1557,61 @@ describe('Baileys inbound messages held for storage', () => {expect(internals(events)).toEqual({ waiting: 0, queued: 0 });});+ // An edit that cannot wait for its message is announced while the session holds nothing to mark. The message is+ // handed on with the edit applied and the text it arrived with, so its row and announcement carry the mark.+ it('marks a message whose edit was announced at once, past the 64 changes that wait', async () => {+ const h = heldProjector({ unique: true, sqlite: true });+ const { events } = build(4, h.callbacks);+ const releaseOther = download();+ const release = download();+ upsert(events, media('O', CHAT), media('M', CHAT, { caption: 'caption' }));+ await flush();+ upsert(events, ...Array.from({ length: 64 }, (_, i) => reaction(`R${i}`, 'O', `r${i}`)));+ await flush();+ upsert(events, edit('E', 'M', 'fixed caption'));+ await flush();+ expect(h.announcedAs('message.edited')).toHaveLength(1);+ releaseOther();+ release();+ await flush();++ const row = h.rows.find(r => r.waMessageId === 'M');+ expect(row).toEqual(expect.objectContaining({ body: 'fixed caption' }));+ expect((row?.metadata as Row).edited).toBe(true);+ expect(h.announcedAs('message.received').filter(m => m.id === 'M')).toEqual([+ expect.objectContaining({ body: 'fixed caption', edited: true }),+ ]);+ });++ it('marks a message whose edit was announced three media deadlines after it began to wait', async () => {+ process.env.MEDIA_DOWNLOAD_TIMEOUT_MS = '30';+ const h = heldProjector({ unique: true, sqlite: true });+ const store = memoryStore();+ const read = store.getStoredMessage;+ const answer = deferred<void>();+ let checked = false;+ // The message's own check for a repeat delivery answers late, as behind a long queue.+ store.getStoredMessage = (id: string) => {+ if (id !== 'M' || checked) return read(id);+ checked = true;+ return answer.promise.then(() => read(id));+ };+ const { events } = build(4, h.callbacks, store);+ upsert(events, text('M', CHAT));+ await flush();+ upsert(events, edit('E', 'M', 'fixed'));+ await sleep(120);+ expect(h.announcedAs('message.edited')).toHaveLength(1);+ answer.resolve();+ await flush();++ expect(h.rows).toEqual([expect.objectContaining({ waMessageId: 'M', body: 'fixed' })]);+ expect((h.rows[0].metadata as Row).edited).toBe(true);+ expect(h.announcedAs('message.received')).toEqual([+ expect.objectContaining({ id: 'M', body: 'fixed', edited: true }),+ ]);+ });+/** How many changes wait, and for how many messages. */const internals = (events: BaileysEvents) => {const state = events as unknown as { changesAwaiting: number; changesAwaitingEmit: Map<string, unknown> };diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.tsindex 2da865281..9638e5664 100644--- a/src/engine/adapters/wwebjs-message-changes.spec.ts+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts@@ -512,6 +512,20 @@ describe.each([expect(events('message.received')).toEqual([expect.objectContaining({ body: 'fixed', edited: true })]);});+ // Past the changes kept for one message they are delivered at once, before the message is handed over.+ it('marks a message edited more times during its contact lookup than are kept for it', async () => {+ const contact = deferred<null>();+ client.emit('message', incoming('A', 'orig', { contact: contact.promise }));+ await flush();+ for (let i = 1; i <= 65; i++) editEvent(client, 'A', `v${i}`);+ await flush();+ contact.resolve(null);+ await flush();++ expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'v65', metadata: { edited: true } })]);+ expect(events('message.received')).toEqual([expect.objectContaining({ body: 'v65', edited: true })]);+ });+// whatsapp-web.js builds a repeat delivery from the message as it is now, so one raised after an edit carries the// edited text, and its lookups can end before those of the first delivery.it.each([diff --git a/src/modules/session/message-edited-marker.spec.ts b/src/modules/session/message-edited-marker.spec.tsindex a89dad134..b27a3fd0e 100644--- a/src/modules/session/message-edited-marker.spec.ts+++ b/src/modules/session/message-edited-marker.spec.ts@@ -480,4 +480,36 @@ describe('edited marker for a message whose row is not written yet', () => {expect(await stored('A')).toEqual(expect.objectContaining({ body: '', type: 'revoked', metadata: null }));});++ // An engine that applied an edit before the hand-over names the text the message arrived with, only when the edit+ // changed it. The edit itself may have reached the session before the message, which then held nothing to mark.+ it('marks a message and a send the engine handed over edited, though no edit reached them here', async () => {+ projector.handleInboundMessage('s1', engine, incoming('A', { body: 'fixed', originalBody: 'original' }));+ projector.handleOwnSendEcho('s1', engine, incoming('S', { fromMe: true, body: 'fixed', originalBody: 'original' }));+ await flush();++ for (const id of ['A', 'S']) {+ expect(await stored(id)).toEqual(expect.objectContaining({ body: 'fixed', metadata: { edited: true } }));+ }+ const sent = announced.filter(a => /^(message\.(received|sent)|socket emitMessage(Sent)?)$/.test(a.event));+ // The webhook and the socket, for each.+ expect(sent).toHaveLength(4);+ for (const { payload } of sent) expect(payload).toEqual(expect.objectContaining({ body: 'fixed', edited: true }));+ });++ it('does not mark a message the engine handed over edited and that was deleted before it was written', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A', { body: 'fixed', originalBody: 'original' }));+ await flush();++ await projector.recordRevoke('s1', 'A');+ insert.resolve();+ await flush();++ expect(await stored('A')).toEqual(expect.objectContaining({ body: '', type: 'revoked', metadata: null }));+ const received = announced.filter(a => a.event === 'message.received').map(a => a.payload);+ expect(received).toEqual([expect.objectContaining({ body: '', type: 'revoked' })]);+ expect(received[0]).not.toHaveProperty('edited');+ });});diff --git a/src/modules/session/message-projector.inbound-hold.spec.ts b/src/modules/session/message-projector.inbound-hold.spec.tsindex 085c9cefa..4ae7a5855 100644--- a/src/modules/session/message-projector.inbound-hold.spec.ts+++ b/src/modules/session/message-projector.inbound-hold.spec.ts@@ -330,6 +330,18 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {expect(emitted.map(([, payload]) => payload)).toEqual(received);});+ // An engine names the text the message arrived with only when the edit it applied changed it.+ it('stores a message the engine handed over edited as marked, though no edit was held for it', async () => {+ const t = admit(arrival('A', 'chat1'));+ projector.handleInboundMessage('s1', engine, message('A', 'chat1', { body: 'fixed', originalBody: 'original' }), t);+ await flush();++ expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } })]);+ expect(persistedHooks()[0]).toEqual(expect.objectContaining({ body: 'fixed', metadata: { edited: true } }));+ const [[, announced]] = eventsGateway.emitMessage.mock.calls as Array<[string, Record<string, unknown>]>;+ expect(announced).toEqual(expect.objectContaining({ body: 'fixed', edited: true }));+ });+it('marks a message edited while its insert is out, where nothing holds the arrival (SQLite)', async () => {build('better-sqlite3');const stall = deferred<void>();diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 1bb53fc8c..dc965bcc3 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -126,7 +126,8 @@ interface InFlightEntry {// The text the message arrived with, which an edit is compared with: one that repeats the text so far changes// nothing. Unset while the message is admitted but not yet emitted.body?: string;- // An edit held for the message changed its text, or was asked for: the row it lands on is marked edited.+ // An edit held for the message or applied by the engine changed its text, or was asked for: the row it lands on is+ // marked edited.edited?: boolean;ackStatus?: MessageStatus;// Latest reaction per sender; '' means the sender withdrew theirs.@@ -417,7 +418,7 @@ export class MessageProjector {const inFlight = turn? Object.assign(turn.entry, { quote: quoteOf(messageData), ref: turn.entry.ref ?? refOf(messageData) }): this.trackInFlight(inFlightKey, messageData);- this.arrive(inFlight, message.originalBody ?? message.body);+ this.arrive(inFlight, message);const onFailure = (err: unknown): null => {this.logger.error(`onMessage handler failed for ${id}`, String(err));@@ -509,10 +510,15 @@ export class MessageProjector {return inFlight;}- /** The engine handed the message over: an edit held until now is compared with the text it arrived with. */- private arrive(entry: InFlightEntry, arrivedWith: string): void {- entry.body = arrivedWith;- if (entry.editedBody !== undefined && entry.editedBody !== arrivedWith) entry.edited = true;+ /**+ * The engine handed the message over: an edit held until now is compared with the text it arrived with. One the+ * engine applied itself changed that text (it names the arrival text only then), and marks the row the same way.+ */+ private arrive(entry: InFlightEntry, message: IncomingMessage): void {+ entry.body = message.originalBody ?? message.body;+ if (message.originalBody !== undefined || (entry.editedBody !== undefined && entry.editedBody !== entry.body)) {+ entry.edited = true;+ }}/** Keep an edit for the row of a message not written yet. One that repeats the text so far changes nothing. */@@ -768,9 +774,9 @@ export class MessageProjector {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);- else if (entry?.editedBody !== undefined) {- dbMessage.body = entry.editedBody;- if (entry.edited) dbMessage.metadata = { ...dbMessage.metadata, edited: true };+ else {+ if (entry?.editedBody !== undefined) dbMessage.body = entry.editedBody;+ if (entry?.edited) dbMessage.metadata = { ...dbMessage.metadata, edited: true };}return entry?.revoked === true;}@@ -847,7 +853,9 @@ export class MessageProjector {const pending = waMessageId ? this.inboundInFlight.get(`${id}:${waMessageId}`) : undefined;if (!pending) return;const { ackStatus } = pending;- const rowChanges = Boolean(pending.revoked || pending.editedBody !== undefined || pending.reactions);+ const rowChanges = Boolean(+ pending.revoked || pending.editedBody !== undefined || pending.edited || pending.reactions,+ );if (!ackStatus && !rowChanges) return;const advance = (): void => {if (!ackStatus) return;@@ -1034,7 +1042,8 @@ export class MessageProjector {message;return { ...kept, body: '', type: 'revoked' };}- if (pending?.editedBody === undefined) return message;+ // An edit the engine applied before the hand-over is in the body already.+ if (pending?.editedBody === undefined) return pending?.edited ? { ...message, edited: true as const } : message;return { ...message, body: pending.editedBody, ...(pending.edited ? { edited: true as const } : {}) };}@@ -1086,7 +1095,7 @@ export class MessageProjector {const inFlight = turn? Object.assign(turn.entry, { quote: quoteOf(messageData), ref: turn.entry.ref ?? refOf(messageData) }): this.trackInFlight(inFlightKey, messageData);- this.arrive(inFlight, message.originalBody ?? message.body);+ this.arrive(inFlight, message);// Execute hook for message sent - plugins can modify or stop processing. Like the inbound path,// the hook chain runs concurrently and the commit waits its turn on the chat's queue.const prepared = this.startHookChain(id, engine, turn, messageData, () =>