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 edited in flight on its announcement
Yudhi Armyndharisec944b97f156
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 76bb8760e..1f30cabb3 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -7272,6 +7272,8 @@ These are the events OpenWA actually emits. A webhook is registered with an `eve> **`contact` on `message.received` is minimal unless you opt in (`WEBHOOK_CONTACT_DETAILS`).** By default the payload carries only `contact { name?, pushName? }`. Setting `WEBHOOK_CONTACT_DETAILS=true` adds `id`, `number`, `shortName`, `type`, `isMyContact`, `isWAContact`, `isBusiness`, `isEnterprise`, `verifiedName`, `verifiedLevel`, `isBlocked` and `labels`, each present only when the engine has a value for it. Every field is copied from the contact record the engine has already loaded for the message, so opting in costs no extra WhatsApp lookup and cannot add rate-limiting pressure. The same enrichment reaches the websocket event, since both are dispatched from one payload. `contact` itself is omitted entirely when the engine has no value for any of its fields, so treat the whole object as optional rather than just its members. **whatsapp-web.js only** — the Baileys path emits `contact { pushName }` when a push name is available, and never reads this flag.+> **`edited` on `message.received` and `message.sent`.** When an edit reached a message before the gateway stored it, the message is announced with the edited `body` and `edited: true`, as its stored row carries `metadata.edited` (an edit event that repeats the text sets neither). Its `message.edited` can arrive before the message itself, so a client that holds no copy of it yet reads the mark from here. The field is absent otherwise.+> **Large media is not inlined into webhook payloads.** A `media` blob whose decoded size exceeds `WEBHOOK_MEDIA_INLINE_MAX_BYTES` (default **1 MiB**; `0` = never inline) is replaced — before the payload is fanned out to your webhook — with the marker form `media: { mimetype, filename?, omitted: true, sizeBytes }`, the same shape the engine emits for capped inbound media. Media at or under the cap stays inline unchanged. Additionally, if the serialized body still exceeds `WEBHOOK_MAX_PAYLOAD_BYTES` (default **1 MiB**) after `webhook:before` hooks ran, any remaining inline media is shed the same way so the event is still delivered; only a payload that is over budget _without_ inline media is dropped (recorded in `GET /api/webhooks/delivery-failures`). Because shedding happens before enqueue, queued and failed BullMQ jobs in Redis never carry the blob either — failed-job retention is bounded by the queue's `removeOnComplete`/`removeOnFail` windows (1h/1000 completed, 24h/5000 failed). The two limits compound: `WEBHOOK_MAX_PAYLOAD_BYTES` is measured on the serialized JSON body after `WEBHOOK_MEDIA_INLINE_MAX_BYTES` has been applied, and base64 inflates media by a third, so at the defaults media above roughly **768 KiB** reaches your webhook as the omitted marker. To inline larger media, raise both together and keep the payload limit at least 4/3 of the inline limit plus room for the rest of the event (for example `5242880` inline with `7340032` payload). The **WebSocket** `message.received` / `message.sent` events apply only the inline cap, with the same marker: an over-cap blob is never broadcast to subscribed sockets (or, with the Redis adapter enabled, replicated across every replica's pub/sub link). On either sink, media the engine downloaded stays retrievable from [`GET /api/sessions/:sessionId/messages/:chatId/:messageId/media`](#get-apisessionssessionidmessageschatidmessageidmedia), which returns `404` when nothing was stored.> **`event.response` reports RSVPs to WhatsApp events (Baileys only).** A WhatsApp event (the calendar-style invitation created from the attach menu, with "Going" / "Not going" / "Maybe" buttons) arrives as an ordinary `message.received` when it is posted. Each answer to it arrives as `event.response`:diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.tsindex 8804612fb..f1a280a22 100644--- a/src/engine/adapters/inbound-hold.spec.ts+++ b/src/engine/adapters/inbound-hold.spec.ts@@ -1268,7 +1268,7 @@ describe('Baileys inbound messages held for storage', () => {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) => {+ ])('marks the row and the message 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();@@ -1282,6 +1282,10 @@ describe('Baileys inbound messages held for storage', () => {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 })]);+ const received = h.announcedAs('message.received');+ expect(received).toEqual([expect.objectContaining({ id: 'M', body })]);+ if (edited) expect(received[0]).toEqual(expect.objectContaining({ edited: true }));+ else expect(received[0]).not.toHaveProperty('edited');});it(diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.tsindex 59221f88c..908f5db32 100644--- a/src/engine/adapters/wwebjs-message-changes.spec.ts+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts@@ -269,7 +269,7 @@ describe.each([expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } })]);expect(hooked).toEqual(['fixed']);- expect(events('message.received').map(m => m.body)).toEqual(['fixed']);+ expect(events('message.received')).toEqual([expect.objectContaining({ body: 'fixed', edited: true })]);expect(events('message.edited').map(m => m.body)).toEqual(['fixed']);});@@ -290,7 +290,7 @@ describe.each([}),]);expect(hooked).toEqual(['new cap']);- expect(events('message.received').map(m => m.body)).toEqual(['new cap']);+ expect(events('message.received')).toEqual([expect.objectContaining({ body: 'new cap', edited: true })]);});it('keeps the caption of a message whose thumbnail changed during the media download, and announces no edit', async () => {@@ -459,7 +459,7 @@ describe.each([expect(rows).toEqual([expect.objectContaining({ waMessageId: 'S', body: 'sent again' })]);expect((rows[0].metadata as Row).edited).toBe(true);expect(hooked).toEqual(['sent again']);- expect(events('message.sent').map(m => m.body)).toEqual(['sent again']);+ expect(events('message.sent')).toEqual([expect.objectContaining({ body: 'sent again', edited: true })]);});// An edit that repeats the text the message carries changes nothing: it is announced, and leaves no mark.@@ -476,6 +476,8 @@ describe.each([expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'same' })]);expect(rows[0].metadata ?? {}).not.toHaveProperty('edited');+ expect(events('message.received')).toHaveLength(1);+ expect(events('message.received')[0]).not.toHaveProperty('edited');});// The hook of message:received can run for a while, and the row is written when it returns.@@ -492,6 +494,8 @@ describe.each([expect(events('message.edited')).toHaveLength(1);expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'same' })]);expect(rows[0].metadata ?? {}).not.toHaveProperty('edited');+ expect(events('message.received')).toHaveLength(1);+ expect(events('message.received')[0]).not.toHaveProperty('edited');});it('stores an edit that changes the text while the hook runs, marked edited', async () => {@@ -505,6 +509,7 @@ describe.each([await flush();expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } })]);+ expect(events('message.received')).toEqual([expect.objectContaining({ body: 'fixed', edited: true })]);});});diff --git a/src/modules/session/message-edited-marker.spec.ts b/src/modules/session/message-edited-marker.spec.tsindex efd2f1b8e..a89dad134 100644--- a/src/modules/session/message-edited-marker.spec.ts+++ b/src/modules/session/message-edited-marker.spec.ts@@ -412,6 +412,50 @@ describe('edited marker for a message whose row is not written yet', () => {);});+ // The message.edited of a message without a row reaches consumers before the message itself, so the+ // announcement of the message says what its row is stored with.+ it('announces each message with the edited mark its row is stored with, and no mark on the others', async () => {+ const insert = deferred<void>();+ const ids = ['A', 'R', 'V', 'N', 'T', 'S', 'D'];+ for (const id of ids) stalled.set(id, insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ projector.handleInboundMessage('s1', engine, incoming('R'));+ projector.handleInboundMessage('s1', engine, incoming('V'));+ projector.handleInboundMessage('s1', engine, incoming('N'));+ projector.handleInboundMessage('s1', engine, incoming('T', { body: 'fixed', originalBody: 'original' }));+ projector.handleOwnSendEcho('s1', engine, incoming('S', { fromMe: true, body: 'sent' }));+ projector.handleInboundMessage('s1', engine, incoming('D'));+ await flush();++ projector.applyMessageEditQueued('s1', editOf('A', 'edited'));+ projector.applyMessageEditQueued('s1', editOf('R', 'original'));+ projector.applyMessageEditQueued('s1', editOf('V', 'v1'));+ projector.applyMessageEditQueued('s1', editOf('V', 'original'));+ projector.applyMessageEditQueued('s1', editOf('T', 'fixed'));+ await projector.recordOutboundMessageEdit('s1', 'S', 'sent', { kind: 'edit', chatId: CHAT, fromMe: true });+ projector.applyMessageEditQueued('s1', editOf('D', 'edited'));+ await projector.recordRevoke('s1', 'D');+ insert.resolve();+ await flush();++ const marked: string[] = [];+ for (const id of ids) {+ const row = await stored(id);+ const payloads = announced+ .filter(a => /^(message\.(received|sent)|socket emitMessage(Sent)?)$/.test(a.event) && a.payload.id === id)+ .map(a => a.payload);+ // The webhook and the socket.+ expect(payloads).toHaveLength(2);+ for (const payload of payloads) {+ expect(payload.body).toBe(row.body);+ if (row.metadata?.edited) expect(payload.edited).toBe(true);+ else expect(payload).not.toHaveProperty('edited');+ }+ if (row.metadata?.edited) marked.push(id);+ }+ expect(marked).toEqual(['A', 'V', 'T', 'S']);+ });+it('leaves a message nobody edited unmarked', 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 4e4f1824e..085c9cefa 100644--- a/src/modules/session/message-projector.inbound-hold.spec.ts+++ b/src/modules/session/message-projector.inbound-hold.spec.ts@@ -289,6 +289,9 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {expect(rows).toEqual([expect.objectContaining({ body: 'original', metadata: undefined })]);expect(persistedHooks()[0]).toEqual(expect.objectContaining({ body: 'original', metadata: undefined }));expect(eventsGateway.emitMessageEdited).toHaveBeenCalledTimes(1);+ expect(eventsGateway.emitMessage).toHaveBeenCalledTimes(1);+ const [[, announced]] = eventsGateway.emitMessage.mock.calls as Array<[string, Record<string, unknown>]>;+ expect(announced).not.toHaveProperty('edited');});it('marks an edit that restores the text after another one, before the emit', async () => {@@ -316,6 +319,15 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } }),expect.objectContaining({ waMessageId: 'B', body: 'original', metadata: undefined }),]);+ // The webhook and the socket are told alike.+ const received = (webhookService.dispatch.mock.calls as Array<[string, string, Record<string, unknown>]>)+ .filter(([, event]) => event === 'message.received')+ .map(([, , payload]) => payload);+ expect(received).toEqual([expect.objectContaining({ id: 'A', body: 'fixed', edited: true }), expect.anything()]);+ expect(received[1]).toEqual(expect.objectContaining({ id: 'B', body: 'original' }));+ expect(received[1]).not.toHaveProperty('edited');+ const emitted = eventsGateway.emitMessage.mock.calls as Array<[string, Record<string, unknown>]>;+ expect(emitted.map(([, payload]) => payload)).toEqual(received);});it('marks a message edited while its insert is out, where nothing holds the arrival (SQLite)', async () => {diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 96516a140..1bb53fc8c 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -1017,10 +1017,14 @@ export class MessageProjector {* null once the engine revoked it (message.revoked already went out, so the deleted content is not* announced after it), the revoked placeholder when only the REST delete did (a delete-for-me emits no* engine revoke, and the message still exists for its sender), the edited body when edited- * (message.edited already went out with it). The stored row takes the same changes in+ * (message.edited already went out with it), plus `edited: true` when the row is marked for it: a consumer+ * that held no copy of the message could not apply that event. The stored row takes the same changes in* {@link applyChangesMadeInFlight}.*/- private withChangesMadeInFlight(id: string, message: InboundMessageData): InboundMessageData | null {+ private withChangesMadeInFlight(+ id: string,+ message: InboundMessageData,+ ): (InboundMessageData & { edited?: true }) | null {const pending = this.inboundInFlight.get(`${id}:${message.id}`);if (pending?.revokeAnnounced) return null;if (pending?.revoked) {@@ -1030,7 +1034,8 @@ export class MessageProjector {message;return { ...kept, body: '', type: 'revoked' };}- return pending?.editedBody === undefined ? message : { ...message, body: pending.editedBody };+ if (pending?.editedBody === undefined) return message;+ return { ...message, body: pending.editedBody, ...(pending.edited ? { edited: true as const } : {}) };}/** Engine callback body, lifted out of initializeEngine so the wiring table stays readable. */