Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): hand over a wwebjs message delivered deleted with no text
Yudhi Armyndharis762b82b6afd6
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex d08751e17..b06f43512 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -7292,6 +7292,8 @@ These are the events OpenWA actually emits. A webhook is registered with an `eve> **`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`. This holds when a later edit restored the text it arrived with too, and the `body` is then that text; edit events that each repeat the text set 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. An own message the engine delivers again after an edit marked its stored row is announced in `message.sent` with `edited: true` too, after the `message.edited` of an edit that reached the gateway first (it remembers up to 500 marked messages per session, the oldest forgotten first, while the session has an engine). A message announced deleted (`type: 'revoked'`) carries an empty `body` and never the field. The field is absent otherwise.+> **A message delivered already deleted.** whatsapp-web.js raises its message events for a message that WhatsApp Web adds already deleted for everyone, with no revoke before it (not yet seen on a live session). Its `message.sent`, and its `message.received` when it is the first delivery of the id, carry `type: 'revoked'` and an empty `body`; the `message:sent` and `message:received` plugin hooks get it with no content, and it is stored as a deleted message. The message already stored under its id, unless it is of another chat or sender, is cleared as a revoke clears it; no `message.revoked` goes out for that.+> **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/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.tsindex 84ea33fbc..2abaedad9 100644--- a/src/engine/adapters/wwebjs-message-changes.spec.ts+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts@@ -888,6 +888,100 @@ describe.each([expect(persisted).toEqual([expect.objectContaining(deletedRow('S')), expect.objectContaining(deletedRow('A'))]);});+ const contentAnnounced = (): Row[] =>+ (dispatch.mock.calls as Array<[string, string, Row]>)+ .filter(c => c[1] === 'message.sent' || c[1] === 'message.received')+ .map(c => c[2])+ .filter(m => m.type !== 'revoked');+ const dispatchedAfterDeleted = (): string[] => {+ const calls = (dispatch.mock.calls as Array<[string, string, Row]>).filter(c =>+ ['message.sent', 'message.received', 'message.revoked'].includes(c[1]),+ );+ const first = calls.findIndex(c => c[2].type === 'revoked');+ if (first < 0) return [];+ return calls.slice(first + 1).flatMap(c => (c[2].type === 'revoked' ? [] : [`${c[1]} ${String(c[2].body)}`]));+ };++ it('stores an own send delivered again as deleted during its media download as deleted, hooked with no text', async () => {+ const media = download();+ client.emit('message_create', incoming('S', 'orig', { page: media.page, fromMe: true }));+ await flush();+ editEvent(client, 'S', 'e0', { media: true, fromMe: true });+ await flush();+ client.emit('message_create', deletedDelivery('S', true));+ await flush();+ media.finish();+ await flush();++ expect(rows).toEqual([expect.objectContaining(deletedRow('S'))]);+ expect(persisted.filter(r => r.waMessageId === 'S').at(-1)).toEqual(expect.objectContaining(deletedRow('S')));+ expect(hooked.at(-1)).toBe('');+ expect(events('message.sent').at(-1)).toEqual(expect.objectContaining({ body: '', type: 'revoked' }));+ expect(dispatchedAfterDeleted()).toEqual([]);+ });++ it('stores a message delivered again as deleted during its contact lookup as deleted, hooked with no text', async () => {+ const contact = deferred<null>();+ client.emit('message', incoming('A', 'orig', { contact: contact.promise }));+ await flush();+ editEvent(client, 'A', 'e0');+ await flush();+ client.emit('message', deletedDelivery('A'));+ await flush();+ contact.resolve(null);+ await flush();++ expect(rows).toEqual([expect.objectContaining(deletedRow('A'))]);+ expect(persisted.filter(r => r.waMessageId === 'A').at(-1)).toEqual(expect.objectContaining(deletedRow('A')));+ expect(hooked.at(-1)).toBe('');+ expect(dispatchedAfterDeleted()).toEqual([]);+ });++ it('announces nothing of an own media send after it was delivered again as deleted and deleted for everyone', async () => {+ const media = download();+ client.emit('message_create', incoming('S', 'orig', { page: media.page, fromMe: true }));+ await flush();+ client.emit('message_create', deletedDelivery('S', true));+ await flush();+ revokeEvent(client, 'S', { fromMe: true });+ await flush();+ media.finish();+ await flush();++ expect(rows).toEqual([expect.objectContaining(deletedRow('S'))]);+ expect(contentAnnounced()).toEqual([]);+ expect(hooked).not.toContain('orig');+ expect(events('message.revoked')).toHaveLength(1);+ });++ // Whatever an engine hands over for a message that came deleted, none of it reaches the hooks.+ it('hooks a message delivered deleted with no content, whatever the engine hands over', async () => {+ const delivered = (id: string, fromMe: boolean) =>+ ({+ id,+ chatId: CHAT,+ from: fromMe ? SELF : CHAT,+ to: fromMe ? CHAT : SELF,+ fromMe,+ isGroup: false,+ type: 'revoked',+ body: 'left over',+ timestamp: 1_700_000_000,+ media: { mimetype: 'image/png', data: 'QUJD' },+ }) as IncomingMessage;+ const seen: Row[] = [];+ rewrite = data => (seen.push({ ...data }), data);+ projector.handleOwnSendEcho('s1', engine, delivered('S', true));+ projector.handleInboundMessage('s1', engine, delivered('A', false));+ await flush();++ expect(seen).toHaveLength(2);+ for (const data of seen) {+ expect(data).toEqual(expect.objectContaining({ body: '', type: 'revoked' }));+ expect(data).not.toHaveProperty('media');+ }+ });+it('announces an own message stored again after its row was removed without the mark of that row', async () => {client.emit('message_create', own('S', 'orig'));await flush();@@ -1337,6 +1431,24 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)expect(order).toEqual(['message A', 'edit A fixed', 'message A', 'edit A late']);});+ it('hands a delivery that came deleted over with no text, and not the delivery still processed after it', async () => {+ const { client, order, bodies, arrivals } = wire();+ const first = deferred<null>();+ client.emit('message', incoming('A', 'orig', { contact: first.promise }));+ editEvent(client, 'A', 'v1');+ client.emit('message', { ...incoming('A', ''), type: 'revoked' });+ await flush();+ expect(order).toEqual(['message A']);++ first.resolve(null);+ await flush();+ editEvent(client, 'A', 'late');++ expect(order).toEqual(['message A', 'edit A v1', 'edit A late']);+ expect(bodies).toEqual(['']);+ expect(arrivals).toEqual([undefined]);+ });+it('delivers the rest when one kept change fails', async () => {const { client, order } = wire({onMessageEdited: () => {diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.tsindex 9e28f1def..165ed1e21 100644--- a/src/engine/adapters/wwebjs-message-events.ts+++ b/src/engine/adapters/wwebjs-message-events.ts@@ -110,6 +110,12 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHoconst keepChanges = (message: IncomingMessage, ticket: InboundTicket | undefined): Processing => {if (ticket || !message.id || message.isStatusBroadcast) return NOT_KEPT;let kept = inProcessing.get(message.id);+ // whatsapp-web.js raises message and message_create for a message WhatsApp Web adds already deleted. Such a+ // delivery takes no text from one of the id still processed, which is then not handed over, and keeps nothing.+ if (message.type === 'revoked') {+ if (kept) kept.deleted = true;+ return NOT_KEPT;+ }if (!kept) {// Past the bound a message is not tracked, and what reaches it is delivered at once, as it always was.if (inProcessing.size >= MAX_MESSAGES_IN_PROCESSING) return NOT_KEPT;diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex f368dcee0..2fa6570c7 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -98,6 +98,14 @@ type InboundMessageData = { [K in keyof IncomingMessage]: IncomingMessage[K] };const quoteOf = (m: Pick<IncomingMessage, 'chatId' | 'body'>) => ({ chatId: m.chatId, body: m.body });+/** A deleted message as hooks and consumers see it: nothing of its content survives, as in REVOKED_ROW_PATCH. */+const deletedPlaceholder = (message: InboundMessageData): InboundMessageData => {+ // eslint-disable-next-line @typescript-eslint/no-unused-vars+ const { media, quotedMessage, call, buttons, button, location, order, product, poll, mentionedIds, ...kept } =+ message;+ return { ...kept, body: '', type: 'revoked' };+};+/** Where a message lives and who wrote it: what a change aimed at it is held to (see mutationTargetsForeignRow). */const refOf = (m: Pick<IncomingMessage, 'chatId' | 'fromMe' | 'author'>): StoredMessageRef => ({chatId: m.chatId,@@ -425,8 +433,9 @@ export class MessageProjector {});// Update last active timestampvoid this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);- // Convert IncomingMessage to plain object for dispatch- const messageData = { ...message };+ // Convert IncomingMessage to plain object for dispatch. One delivered deleted reaches the hooks with no content,+ // whatever text the engine handed over with it.+ const messageData = message.type === 'revoked' ? deletedPlaceholder(message) : { ...message };// What an engine says of the edit it applied is for the session alone: no hook, row or consumer sees it.delete messageData.originalBody;// Tracks what the chain's current copy lends a quote, so one taken mid-chain carries an earlier@@ -1073,13 +1082,8 @@ export class MessageProjector {): (InboundMessageData & { edited?: true }) | null {const pending = this.inboundInFlight.get(`${id}:${message.id}`);if (pending?.revokeAnnounced) return null;- if (pending?.revoked || message.type === 'revoked') {- // Nothing of the content survives, as in REVOKED_ROW_PATCH: no edit held for it, and no mark.- // eslint-disable-next-line @typescript-eslint/no-unused-vars- const { media, quotedMessage, call, buttons, button, location, order, product, poll, mentionedIds, ...kept } =- message;- return { ...kept, body: '', type: 'revoked' };- }+ // No edit held for it, and no mark.+ if (pending?.revoked || message.type === 'revoked') return deletedPlaceholder(message);const edited = rowMarked || pending?.edited;// An edit the engine applied before the hand-over is in the body already.if (pending?.editedBody === undefined) return edited ? { ...message, edited: true as const } : message;@@ -1120,7 +1124,8 @@ export class MessageProjector {});// Update last active timestampvoid this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);- const messageData = { ...message };+ // As on the inbound path, one delivered deleted reaches the hooks with no content.+ const messageData = message.type === 'revoked' ? deletedPlaceholder(message) : { ...message };delete messageData.originalBody;const onFailure = (err: unknown): null => {