Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): leave a held edit that repeats the text unmarked
Yudhi Armyndharis62c28ecb4dfb
diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.tsindex 1a69d30c3..16126a27a 100644--- a/src/engine/adapters/inbound-hold.spec.ts+++ b/src/engine/adapters/inbound-hold.spec.ts@@ -1265,6 +1265,25 @@ describe('Baileys inbound messages held for storage', () => {expect(handedOver).toEqual([{ body, originalBody }]);});+ it.each([+ ['an edit', 'fixed caption', true],+ ['an edit that repeats the text', 'caption', undefined],+ ])('marks the row edited for %s only if it changed the text', async (_, body, edited) => {+ const h = heldProjector({ unique: true, sqlite });+ const { events } = build(4, h.callbacks);+ const release = download();+ upsert(events, media('M', CHAT, { caption: 'caption' }));+ await flush();+ upsert(events, edit('E', 'M', body));+ await flush();+ release();+ await flush();++ expect(h.rows).toEqual([expect.objectContaining({ waMessageId: 'M', body })]);+ expect((h.rows[0].metadata as Row).edited).toBe(edited);+ expect(h.announcedAs('message.edited')).toEqual([expect.objectContaining({ messageId: 'M', body })]);+ });+it(sqlite? 'announces them once the message has been handed on'diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.tsindex 34ecf7362..0fe7be82c 100644--- a/src/engine/adapters/wwebjs-message-changes.spec.ts+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts@@ -109,13 +109,15 @@ describe.each([let rows: Row[];let dispatch: jest.Mock;let client: EventEmitter;- // The bodies the message:received and message:sent hooks were given.+ // 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;beforeEach(() => {process.env.INBOUND_MEDIA_CONCURRENCY = '1';rows = [];hooked = [];+ hookGate = undefined;// The real projector over a stand-in `messages` table, wired as the database wires it: only PostgreSQL// admits a message on arrival.const repo = {@@ -150,9 +152,10 @@ describe.each([new Proxy({}, { get: () => jest.fn() }) as unknown as EventsGateway,{ dispatch } as unknown as WebhookService,{- execute: jest.fn((event: string, data: unknown) => {+ execute: jest.fn(async (event: string, data: unknown) => {if (event === 'message:received' || event === 'message:sent') hooked.push(String((data as Row).body));- return Promise.resolve({ continue: true, data });+ if (event === 'message:received') await hookGate;+ return { continue: true, data };}),} as unknown as HookManager,{ ingest: jest.fn() } as unknown as StatusStoreService,@@ -402,25 +405,50 @@ describe.each([expect(events('message.sent').map(m => m.body)).toEqual(['sent again']);});- // PostgreSQL holds the message in the session, which takes such an edit itself. Here the adapter holds it, and- // an edit that repeats the text the message carries so far changes nothing: it goes through at once, as it did- // before, and leaves no mark.- if (type === 'better-sqlite3') {- it('announces an edit that repeats the text at once, and does not mark the message edited', async () => {- const contact = deferred<null>();- client.emit('message', incoming('A', 'same', { contact: contact.promise }));- await flush();- editEvent(client, 'A', 'same');- await flush();+ // An edit that repeats the text the message carries changes nothing: it is announced, and leaves no mark.+ it('announces an edit that repeats the text during the contact lookup, and does not mark the message edited', async () => {+ const contact = deferred<null>();+ client.emit('message', incoming('A', 'same', { contact: contact.promise }));+ await flush();+ editEvent(client, 'A', 'same');+ await flush();- expect(events('message.edited')).toHaveLength(1);- contact.resolve(null);- await flush();+ expect(events('message.edited')).toHaveLength(1);+ contact.resolve(null);+ await flush();- expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'same' })]);- expect(rows[0].metadata ?? {}).not.toHaveProperty('edited');- });- }+ expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'same' })]);+ expect(rows[0].metadata ?? {}).not.toHaveProperty('edited');+ });++ // The hook of message:received can run for a while, and the row is written when it returns.+ it('announces an edit that repeats the text while the hook runs, and does not mark the message edited', async () => {+ const hook = deferred<void>();+ hookGate = hook.promise;+ client.emit('message', incoming('A', 'same'));+ await flush();+ editEvent(client, 'A', 'same');+ await flush();+ hook.resolve();+ await flush();++ expect(events('message.edited')).toHaveLength(1);+ expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'same' })]);+ expect(rows[0].metadata ?? {}).not.toHaveProperty('edited');+ });++ it('stores an edit that changes the text while the hook runs, marked edited', async () => {+ const hook = deferred<void>();+ hookGate = hook.promise;+ client.emit('message', incoming('A', 'orig'));+ await flush();+ editEvent(client, 'A', 'fixed');+ await flush();+ hook.resolve();+ await flush();++ expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } })]);+ });});describe('changes kept while whatsapp-web.js processes a message (adapter level)', () => {diff --git a/src/modules/session/message-edited-marker.spec.ts b/src/modules/session/message-edited-marker.spec.tsindex a2159b36f..1796da902 100644--- a/src/modules/session/message-edited-marker.spec.ts+++ b/src/modules/session/message-edited-marker.spec.ts@@ -276,6 +276,85 @@ describe('edited marker for a message whose row is not written yet', () => {});});+ it('leaves an edit that repeats the text unmarked while the insert is out, and announces it', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ await flush();++ projector.applyMessageEditQueued('s1', editOf('A', 'original'));+ await flush();+ insert.resolve();+ await flush();++ expect(await stored('A')).toEqual(expect.objectContaining({ body: 'original', metadata: null }));+ expect(announced.filter(a => a.event === 'message.edited')).toHaveLength(1);+ });++ it('marks a message edited and then restored to its text while the insert is out', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ await flush();++ projector.applyMessageEditQueued('s1', editOf('A', 'v1'));+ projector.applyMessageEditQueued('s1', editOf('A', 'original'));+ await flush();+ insert.resolve();+ await flush();++ expect(await stored('A')).toEqual(expect.objectContaining({ body: 'original', metadata: { edited: true } }));+ });++ it('keeps the mark of an edit that restored the text when the message is delivered again', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ await flush();+ projector.applyMessageEditQueued('s1', editOf('A', 'v1'));+ projector.applyMessageEditQueued('s1', editOf('A', 'original'));+ projector.handleInboundMessage('s1', engine, incoming('A'));+ await flush();+ insert.resolve();+ await flush();++ expect(await stored('A')).toEqual(expect.objectContaining({ body: 'original', metadata: { edited: true } }));+ });++ // The engine applied the edit before the hand-over and told the session of it, so the text the message was+ // handed over with is already the edited one.+ it('compares an edit with the text the message arrived with, not the one it was handed over with', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ stalled.set('B', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A', { body: 'fixed', originalBody: 'original' }));+ projector.handleInboundMessage('s1', engine, incoming('B', { body: 'original' }));+ await flush();++ projector.applyMessageEditQueued('s1', editOf('A', 'fixed'));+ projector.applyMessageEditQueued('s1', editOf('B', 'original'));+ await flush();+ insert.resolve();+ await flush();++ expect(await stored('A')).toEqual(expect.objectContaining({ body: 'fixed', metadata: { edited: true } }));+ expect(await stored('B')).toEqual(expect.objectContaining({ body: 'original', metadata: null }));+ });++ // A REST edit is asked for by the caller, and the stored row is marked for it whatever the text.+ it('marks a send a REST edit reaches while its insert is out, even when the edit repeats the text', async () => {+ const insert = deferred<void>();+ stalled.set('S', insert.promise);+ projector.handleOwnSendEcho('s1', engine, incoming('S', { fromMe: true, body: 'sent' }));+ await flush();++ await projector.recordOutboundMessageEdit('s1', 'S', 'sent', { kind: 'edit', chatId: CHAT, fromMe: true });+ insert.resolve();+ await flush();++ expect(await stored('S')).toEqual(expect.objectContaining({ body: 'sent', metadata: { edited: true } }));+ });+it('marks a message edited while it waits behind an earlier message of its chat', async () => {const insert = deferred<void>();stalled.set('A', insert.promise);diff --git a/src/modules/session/message-projector.inbound-hold.spec.ts b/src/modules/session/message-projector.inbound-hold.spec.tsindex 9e9064a79..d90f51d56 100644--- a/src/modules/session/message-projector.inbound-hold.spec.ts+++ b/src/modules/session/message-projector.inbound-hold.spec.ts@@ -264,6 +264,56 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {expect(dispatched('message.received')[0]).toEqual(expect.objectContaining({ body: 'edited' }));});+ // An edit that repeats the text the message carries changes nothing, so it leaves no mark, though it is announced.+ it.each([+ ['before the emit', false],+ ['after the emit, while the insert is out', true],+ ])('leaves an edit that repeats the text unmarked, %s', async (_when, afterEmit) => {+ const stall = deferred<void>();+ if (afterEmit) insertGate = () => stall.promise;+ const t = admit(arrival('A', 'chat1'));+ if (!afterEmit) projector.applyMessageEditQueued('s1', { messageId: 'A', body: 'original' } as never);+ projector.handleInboundMessage('s1', engine, message('A', 'chat1', { body: 'original' }), t);+ if (afterEmit) {+ await flush();+ projector.applyMessageEditQueued('s1', { messageId: 'A', body: 'original' } as never);+ await flush();+ stall.resolve();+ }+ await flush();++ expect(rows).toEqual([expect.objectContaining({ body: 'original', metadata: undefined })]);+ expect(persistedHooks()[0]).toEqual(expect.objectContaining({ body: 'original', metadata: undefined }));+ expect(eventsGateway.emitMessageEdited).toHaveBeenCalledTimes(1);+ });++ it('marks an edit that restores the text after another one, before the emit', async () => {+ const t = admit(arrival('A', 'chat1'));+ projector.applyMessageEditQueued('s1', { messageId: 'A', body: 'v1' } as never);+ projector.applyMessageEditQueued('s1', { messageId: 'A', body: 'original' } as never);+ projector.handleInboundMessage('s1', engine, message('A', 'chat1', { body: 'original' }), t);+ await flush();++ expect(rows).toEqual([expect.objectContaining({ body: 'original', metadata: { edited: true } })]);+ });++ // The engine applied the edit and told the session of it before the hand-over, so the message is handed over+ // already edited, with the text it arrived with beside it.+ it('compares an edit held before the emit with the text the message arrived with', async () => {+ const t = admit(arrival('A', 'chat1'));+ projector.applyMessageEditQueued('s1', { messageId: 'A', body: 'fixed' } as never);+ projector.handleInboundMessage('s1', engine, message('A', 'chat1', { body: 'fixed', originalBody: 'original' }), t);+ const same = admit(arrival('B', 'chat1'));+ projector.applyMessageEditQueued('s1', { messageId: 'B', body: 'original' } as never);+ projector.handleInboundMessage('s1', engine, message('B', 'chat1', { body: 'original' }), same);+ await flush();++ expect(rows).toEqual([+ expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } }),+ expect.objectContaining({ waMessageId: 'B', body: 'original', metadata: undefined }),+ ]);+ });+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 e0e46ec4d..96516a140 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -123,6 +123,11 @@ interface InFlightEntry {// Set when the revoke came from the engine, so message.revoked went out for it.revokeAnnounced?: boolean;editedBody?: string;+ // 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.+ edited?: boolean;ackStatus?: MessageStatus;// Latest reaction per sender; '' means the sender withdrew theirs.reactions?: Record<string, string>;@@ -412,6 +417,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);const onFailure = (err: unknown): null => {this.logger.error(`onMessage handler failed for ${id}`, String(err));@@ -484,9 +490,10 @@ export class MessageProjector {}/** Record a message as not yet written; a re-fire of the same id keeps the changes recorded so far. */- private trackInFlight(key: string, message: InboundMessageData) {- const { ref, revoked, revokeAnnounced, editedBody, ackStatus, reactions } = this.inboundInFlight.get(key) ?? {};- const inFlight = {+ private trackInFlight(key: string, message: InboundMessageData): InFlightEntry {+ const { ref, revoked, revokeAnnounced, editedBody, edited, ackStatus, reactions } =+ this.inboundInFlight.get(key) ?? {};+ const inFlight: InFlightEntry = {quote: quoteOf(message),// The first delivery of an id says whose message it is: a repeat sent from another chat must not// take that over from it, or a change aimed through the repeat would be judged by the wrong rule.@@ -494,6 +501,7 @@ export class MessageProjector {revoked,revokeAnnounced,editedBody,+ edited,ackStatus,reactions,};@@ -501,6 +509,19 @@ 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;+ }++ /** Keep an edit for the row of a message not written yet. One that repeats the text so far changes nothing. */+ private holdEdit(entry: InFlightEntry, body: string): void {+ const soFar = entry.editedBody ?? entry.body;+ if (soFar !== undefined && body !== soFar) entry.edited = true;+ entry.editedBody = body;+ }+/** Drop the entry once its commit ended, unless a re-fire of the same id replaced it. */private untrackInFlight(key: string, inFlight: object): void {if (this.inboundInFlight.get(key) === inFlight) this.inboundInFlight.delete(key);@@ -749,7 +770,7 @@ export class MessageProjector {if (entry?.revoked) Object.assign(dbMessage, REVOKED_ROW_PATCH);else if (entry?.editedBody !== undefined) {dbMessage.body = entry.editedBody;- dbMessage.metadata = { ...dbMessage.metadata, edited: true };+ if (entry.edited) dbMessage.metadata = { ...dbMessage.metadata, edited: true };}return entry?.revoked === true;}@@ -867,11 +888,12 @@ export class MessageProjector {): Promise<void> {if (pending.revoked) return this.revokeRow(id, waMessageId, !insertedCleared);try {- const edited = pending.editedBody !== undefined;- if (edited) await this.messageRepository.update({ sessionId: id, waMessageId }, { body: pending.editedBody });+ if (pending.editedBody !== undefined) {+ await this.messageRepository.update({ sessionId: id, waMessageId }, { 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.- if (edited) await this.mutationProjector.markMessageEdited(id, waMessageId);+ if (pending.edited) await this.mutationProjector.markMessageEdited(id, waMessageId);} catch (err) {this.logger.error(`Failed to apply changes to message ${waMessageId}`, String(err));}@@ -1059,6 +1081,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);// 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, () =>@@ -1416,7 +1439,7 @@ export class MessageProjector {// See applyReactionQueued: judged foreign, it is not kept for the row, and a stored row decides.const source = { kind: 'edit', chatId: message.chatId, fromMe: message.fromMe, author: message.author } as const;if (this.foreignToInFlight(inFlight, source)) needsRow = true;- else inFlight.editedBody = message.body;+ else this.holdEdit(inFlight, message.body);}this.mutationProjector.applyMessageEditQueued(id, message, needsRow);}@@ -1453,7 +1476,11 @@ export class MessageProjector {// With a `source`, the message still arriving takes the edit only if it agrees with it, and the row// only if it is not foreign to it, as for recordRevoke.const inFlight = this.inboundInFlight.get(`${sessionId}:${messageId}`);- if (inFlight && !(source && this.foreignToInFlight(inFlight, source))) inFlight.editedBody = body;+ if (inFlight && !(source && this.foreignToInFlight(inFlight, source))) {+ this.holdEdit(inFlight, body);+ // Asked for by the caller, and marked as the stored row is: the text it carries does not matter.+ inFlight.edited = true;+ }return this.mutationProjector.recordOutboundMessageEdit(sessionId, messageId, body, source);}