Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): announce a message delivered deleted as its placeholder
Yudhi Armyndharis5242b13b0989
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 5790a8f7c..06e04dc76 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -7290,7 +7290,7 @@ 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`. 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'`) never carries the field. The field is absent otherwise.+> **`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.> **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.diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.tsindex fca383600..528b52df8 100644--- a/src/engine/adapters/wwebjs-message-changes.spec.ts+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts@@ -768,9 +768,11 @@ describe.each([expect(events('message.sent')).toEqual([expect.objectContaining({ body: 'e0', type: 'text', edited: true }),- expect.objectContaining({ type: 'revoked' }),+ expect.objectContaining({ body: '', type: 'revoked' }),]);expect(events('message.sent')[1]).not.toHaveProperty('edited');+ // The message:sent hook of the delivery that came deleted is given no text either.+ expect(hooked[hooked.length - 1]).toBe('');});it('announces an own message delivered again after its delete and an API edit of it without the mark', async () => {diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 82479c7a6..faa3d1764 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -491,7 +491,8 @@ export class MessageProjector {return turn.hookGate.promise.then(() => {if (!this.engines.isLive(id, engine)) return null;if (entry.revoked) return messageData;- if (entry.editedBody !== undefined) messageData.body = entry.editedBody;+ // A delivery that came deleted has no text for the edit to replace.+ if (entry.editedBody !== undefined && messageData.type !== 'revoked') messageData.body = entry.editedBody;return run();});}@@ -1043,10 +1044,10 @@ export class MessageProjector {* The message as a revoke or edit that arrived while it was in flight left it, for its announcement:* 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), plus `edited: true` when the row is marked for it or already- * was (`rowMarked`): a consumer that held no copy of the message could not apply that event. A message- * announced deleted carries no mark. The stored row takes the same changes in {@link applyChangesMadeInFlight}.+ * engine revoke, and the message still exists for its sender) or when it was delivered deleted, the edited+ * body when edited (message.edited already went out with it), plus `edited: true` when the row is marked+ * for it or already was (`rowMarked`): 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,@@ -1055,15 +1056,14 @@ export class MessageProjector {): (InboundMessageData & { edited?: true }) | null {const pending = this.inboundInFlight.get(`${id}:${message.id}`);if (pending?.revokeAnnounced) return null;- if (pending?.revoked) {- // Nothing of the content survives, as in REVOKED_ROW_PATCH.+ 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-varsconst { media, quotedMessage, call, buttons, button, location, order, product, poll, mentionedIds, ...kept } =message;return { ...kept, body: '', type: 'revoked' };}- // The mark described content a message delivered deleted no longer has.- const edited = (rowMarked || pending?.edited) && message.type !== 'revoked';+ 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;return { ...message, body: pending.editedBody, ...(edited ? { edited: true as const } : {}) };