Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
refactor(session): lift the large engine callbacks out of initializeEngine
Yudhi Armyndharis0ead8b238b59
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex fb49b58cc..1c65ee22d 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -45,6 +45,7 @@ import {IncomingMessage,ReactionEvent,EditedMessage,+ RevokedMessage,GroupEvent,IncomingCallEvent,} from '../../engine/interfaces/whatsapp-engine.interface';@@ -1136,236 +1137,8 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicatvoid this.updateStatus(id, SessionStatus.QR_READY);},- onReady: (phone: string, pushName: string): void => {- if (!this.isLiveEngine(id, engine)) return;- this.logger.log(`Session ready: ${phone}`, {- sessionId: id,- phone,- pushName,- action: 'ready',- });-- void this.webhookService.dispatch(id, 'session.authenticated', { sessionId: id, phone, pushName });- this.eventsGateway.emitSessionAuthenticated(id, { phone, pushName });-- // Execute hook for ready event- void this.hookManager.execute(- 'session:ready',- { phone, pushName },- {- sessionId: id,- source: 'Engine',- },- );-- // Reset reconnect attempts and clear any stale failure reason on success- const reconnectState = this.reconnectStates.get(id);- if (reconnectState) {- reconnectState.attempts = 0;- }- // A fresh READY stretch starts the watchdog's failure budget clean too.- this.watchdog.clear(id);- this.sessionErrors.delete(id);- // READY proves any in-flight stuck-auth recovery succeeded (or none was needed), so the- // one-shot recovery budget is re-armed for a future episode.- this.stuckAuthRecoveryUsed.delete(id);-- void this.sessionRepository- .update(id, {- status: SessionStatus.READY,- phone,- pushName,- connectedAt: new Date(),- lastActiveAt: new Date(),- })- .catch(err =>- this.logger.warn('Failed to persist session ready state', {- sessionId: id,- error: err instanceof Error ? err.message : String(err),- }),- );-- // Best-effort snapshot of the account's own contacts' currently-active statuses. Live status- // posts arrive through onMessage below; this just backfills what was already up before we- // connected. Not awaited — onReady must not block on it.- void this.seedStatuses(id, engine);- },- onMessage: (message): void => {- if (!this.isLiveEngine(id, engine)) return;- // Status/Story posts arrive via the inbound path for some engines; ingest them into the- // status store instead of the message pipeline. Mirrors the isStatusBroadcast guard in- // onMessageCreate below: an own-send echo (fromMe) stays a plain drop — never ingested,- // never dispatched — so a status you posted never re-appears in your own webhooks/API as- // if a contact had posted it.- if (message.isStatusBroadcast) {- if (message.fromMe) return;- const status = buildIncomingStatus(message);- if (status) {- void this.statusStore- .ingest(id, status)- // Dispatch only on a fresh insert: ingest also resolves with the pre-existing row- // for a duplicate delivery (or the winner's row after a lost insert race), and the- // webhook must fire once per status, not once per (re)delivery.- .then(({ row, created }) => {- // The ingest awaited; a stop()/delete() can retire this engine mid-flight — don't- // dispatch for a session that no longer exists (mirrors message.received's re-check).- if (!this.isLiveEngine(id, engine)) return;- if (created) this.dispatchStatusReceived(id, row);- })- .catch(err =>- this.logger.warn('Status ingest failed', {- sessionId: id,- error: err instanceof Error ? err.message : String(err),- }),- );- }- return;- }- // Ephemeral/disappearing messages: skip persist + dispatch when the operator opted out.- // A message is ephemeral when its chat has a disappearing-messages timer (ephemeralDuration > 0).- if (- !resolveFeatureFlags(this.configService).storeEphemeralMessages &&- message.ephemeralDuration &&- message.ephemeralDuration > 0- ) {- this.logger.debug('Skipping ephemeral message', {- sessionId: id,- messageId: message.id,- chatId: message.chatId,- ephemeralDuration: message.ephemeralDuration,- });- return;- }- this.logger.debug(`Message received from ${message.from}`, {- sessionId: id,- messageId: message.id,- from: message.from,- action: 'message_received',- });- // Update last active timestamp- void this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);- // Convert IncomingMessage to plain object for dispatch- const messageData = { ...message };-- // Execute hook for message received - plugins can modify or stop processing- void this.hookManager- .execute('message:received', messageData, {- sessionId: id,- source: 'Engine',- })- .then(async ({ data: finalMessage }) => {- // `continue: false` is deliberately NOT read here. It means "stop the handler chain", which- // HookManager has already done — the plugins after the one that returned it never ran. It- // does not mean "this message never happened".- //- // Honouring it here used to skip everything below: the message was never written to the- // messages table, never dispatched to webhooks, and never emitted over the websocket. An- // auto-reply plugin returning `false` for its ordinary purpose — keeping other bots from- // answering the same message — silently erased the customer's message from the operator's- // own history, leaving a thread of bot replies answering nothing. Nothing in the hook- // contract (`HookResult.continue`, docs/19) or the webhook contract (`message.received`- // fires when "an inbound message arrives", docs/06) hinted at that, and a sandboxed- // marketplace plugin could swallow a session's entire inbound traffic with no audit trail.- //- // The message has already arrived at WhatsApp. A plugin can stop other plugins from acting- // on it; it cannot make the gateway forget it. Pre-action hooks are where a veto belongs —- // `message:sending` blocks a send that has not happened yet (core/hooks/sending-gate.ts).-- // Persist the incoming message so the dashboard chats view can render history.- const incoming: IncomingMessage = finalMessage;-- // Inline @lid -> phone resolution (#263), opt-in via RESOLVE_LID_TO_PHONE. Best-effort:- // attaches senderPhone (digits or null) before persist/dispatch so webhook/ws consumers- // get it in a single pass. Only for privacy-id senders, so no lookup for normal numbers.- if (resolveFeatureFlags(this.configService).resolveLidToPhone && incoming.isLidSender && !incoming.fromMe) {- incoming.senderPhone = await this.lidResolver.resolveSenderPhone(id, incoming.author ?? incoming.from);- }-- const metadata = buildMessageMetadata(incoming);-- const chatName = incoming.contact?.pushName ?? incoming.contact?.name ?? undefined;-- const dbMessage = this.messageRepository.create({- sessionId: id,- waMessageId: storableWaMessageId(incoming.id),- chatId: incoming.chatId,- chatName,- // Group poster (participant JID) — `from` is the group JID, so this is the stable- // sender identity the chat view keys attribution runs/colors on. Undefined for 1:1.- author: incoming.author,- from: incoming.from,- to: incoming.to,- body: incoming.body,- type: incoming.type,- direction: incoming.fromMe ? MessageDirection.OUTGOING : MessageDirection.INCOMING,- timestamp: incoming.timestamp,- status: MessageStatus.SENT,- metadata,- });-- // The hook chain above is async; a delete()/teardown can retire this engine while it- // awaits. Re-check liveness so a late continuation can't persist an orphan messages row- // (the row has no FK, so a session-delete cleanup would never reap it) or dispatch for a- // session that no longer exists. Mirrors the synchronous isLiveEngine gate at entry.- if (!this.isLiveEngine(id, engine)) return;-- // De-duplicate at the source: the engine can re-fire `message` for one inbound message- // (#464). UNIQUE(sessionId, waMessageId) makes the insert the atomic dedup oracle — a- // near-simultaneous re-fire loses the race and is skipped here, so persist + webhook + WS- // happen exactly once. Fail-open: a non-conflict DB error still dispatches, so a real- // message is never dropped by a transient DB failure.- let isNewMessage = true;- let persisted = false;- try {- // `insert()` (not `save()`) is load-bearing: the UNIQUE(sessionId, waMessageId) constraint- // makes a duplicate insert throw, which is the atomic dedup oracle for #464 re-fires.- // Unlike `save()`, `insert()` does NOT merge DB-generated columns (@PrimaryGeneratedColumn,- // @CreateDateColumn) back onto the entity instance — so merge them explicitly here, before- // the `message:persisted` emit. `identifiers[0]` always carries the PK on both SQLite and- // Postgres; `generatedMaps[0]` adds createdAt where the driver returns it (Postgres yes;- // SQLite historically does not — acceptable; the PK is the load-bearing field for plugins).- const result = await this.messageRepository.insert(- dbMessage as unknown as QueryDeepPartialEntity<Message>,- );- Object.assign(dbMessage, result.identifiers[0] ?? {}, result.generatedMaps?.[0] ?? {});- persisted = true;- } catch (err) {- if (isUniqueConstraintError(err)) {- isNewMessage = false;- } else {- this.logger.error(`Failed to save incoming message ${incoming.id} to database`, String(err));- }- }- if (!isNewMessage) {- return; // duplicate re-fire — the original already persisted and dispatched- }-- // Fire-and-forget: a plugin handler must never break the receive path. Both engine adapters- // (wwjs `message` and Baileys `upsert`) converge on this persist, so one emit covers inbound.- // The built-in FTS search provider is DB-synced and does NOT consume this; it exists for- // plugin providers (Spec 2) + general use.- // Gate ONLY the hook on `persisted`: on a non-unique insert error (transient SQLITE_BUSY /- // lock-timeout / connection drop) the row was never stored and `dbMessage.id` is undefined,- // so emitting `message:persisted` would hand plugins an id-less payload for a row that isn't- // in the DB. The webhook/WS dispatch below stays fail-open — a real inbound message must- // never be dropped on a transient DB failure; only the hook requires a durable row.- if (persisted) {- void this.hookManager- .execute(- 'message:persisted',- { sessionId: id, message: dbMessage },- { sessionId: id, source: 'SessionService' },- )- .catch(() => undefined);- }-- // Dispatch to webhooks with potentially modified message- void this.webhookService.dispatch(id, 'message.received', finalMessage);- // Emit real-time event to WebSocket clients- this.eventsGateway.emitMessage(id, finalMessage);- })- .catch(err => this.logger.error(`onMessage handler failed for ${id}`, String(err)));- },+ onReady: (phone, pushName): void => this.handleEngineReady(id, engine, phone, pushName),+ onMessage: (message): void => this.handleInboundMessage(id, engine, message),onHistoryMessages: (messages): void => {if (!this.isLiveEngine(id, engine)) return;// Persist for the chat view only; no dispatch (these predate the live session).@@ -1373,226 +1146,9 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicatthis.logger.error(`Failed to persist history messages for ${id}`, String(err)),);},- onMessageCreate: (message): void => {- if (!this.isLiveEngine(id, engine)) return;- // `message_create` fires for every message the account creates, including sends composed on a- // linked phone — which the `message`/`onMessage` event never delivers. Incoming messages are- // already handled by `onMessage`, so only outgoing (`fromMe`) ones produce `message.sent` here.- if (!message.fromMe) {- return;- }-- // Status/Story posts are account-created but not real conversations; don't emit `message.sent`- // for them. The adapter flags these (the engine-specific pseudo-JID stays out of this layer).- if (message.isStatusBroadcast) {- return;- }-- this.logger.debug(`Message sent to ${message.to}`, {- sessionId: id,- messageId: message.id,- to: message.to,- action: 'message_sent',- });- // Update last active timestamp- void this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);- const messageData = { ...message };-- // Execute hook for message sent - plugins can modify or stop processing- void this.hookManager- .execute('message:sent', messageData, {- sessionId: id,- source: 'Engine',- })- .then(async ({ data: finalMessage }) => {- // `continue: false` is not read here, for the same reason as the message:received path- // above: the send has already happened, so a plugin can stop the handler chain but cannot- // un-send it. Skipping the persist below dropped the operator's own outgoing message from- // history and from `message.sent` webhooks.-- // Persist the outgoing message so local history reflects sends composed on a linked phone- // (message_create is the ONLY event those produce). It also fires for API-originated sends,- // which the REST send path persists itself — the UNIQUE(sessionId, waMessageId) index is- // the atomic dedup oracle between the two writers: the loser skips its insert, and- // persistSentState additionally drops its redundant PENDING row when the echo won. The- // webhook/WS dispatch below is identical whether the insert won, lost, or failed — the- // message.sent contract is unchanged.- const outgoing: IncomingMessage = finalMessage;- const metadata = buildMessageMetadata(outgoing, true);-- // The ephemeral opt-out gates STORAGE only (mirrors onMessage); the live dispatch below- // is today's contract and stays.- const mayPersist =- resolveFeatureFlags(this.configService).storeEphemeralMessages ||- !(outgoing.ephemeralDuration && outgoing.ephemeralDuration > 0);-- if (mayPersist) {- const dbMessage = this.messageRepository.create({- sessionId: id,- waMessageId: storableWaMessageId(outgoing.id),- chatId: outgoing.chatId,- from: outgoing.from,- to: outgoing.to,- body: outgoing.body,- type: outgoing.type,- direction: MessageDirection.OUTGOING,- timestamp: outgoing.timestamp,- status: MessageStatus.SENT,- metadata,- });- // The hook chain above is async; a delete()/teardown can retire this engine while it- // awaits. Re-check liveness so a late continuation can't persist an orphan row- // (mirrors onMessage).- if (!this.isLiveEngine(id, engine)) return;- let persisted = false;- try {- const result = await this.messageRepository.insert(- dbMessage as unknown as QueryDeepPartialEntity<Message>,- );- Object.assign(dbMessage, result.identifiers[0] ?? {}, result.generatedMaps?.[0] ?? {});- persisted = true;- } catch (err) {- // Unique violation = the REST send path already persisted this API-originated send —- // the dedup oracle working as intended, not an error. Anything else is a real DB- // failure; fail open so a real send is never dropped on a transient DB fault.- if (!isUniqueConstraintError(err)) {- this.logger.error(`Failed to save outgoing message ${outgoing.id} to database`, String(err));- }- }- if (persisted) {- // Fire-and-forget, mirroring onMessage: plugin providers (search etc.) see phone-- // composed sends exactly like API sends.- void this.hookManager- .execute(- 'message:persisted',- { sessionId: id, message: dbMessage },- { sessionId: id, source: 'SessionService' },- )- .catch(() => undefined);- }- }-- void this.webhookService.dispatch(id, 'message.sent', finalMessage);- // Emit real-time event to WebSocket clients (as message.sent, not message.received)- this.eventsGateway.emitMessageSent(id, finalMessage);- })- .catch(err => this.logger.error(`onMessageCreate handler failed for ${id}`, String(err)));- },- onMessageAck: (messageId, status: DeliveryStatus): void => {- if (!this.isLiveEngine(id, engine)) return;- this.logger.debug(`Message ack: ${messageId} -> ${status}`, {- sessionId: id,- messageId,- status,- action: 'message_ack',- });-- // Reflect real delivery state on the stored message (#220): delivered/read/failed advance the- // stored status; pending/sent carry no upgrade (it's already SENT — visibly "not delivered").- // The UPDATE is guarded to the allowed prior statuses so delivery state only ADVANCES: an- // out-of-order/late ack cannot downgrade a higher status, which also makes these- // fire-and-forget writes race-safe at the DB level.- const messageStatus = deliveryStatusToMessageStatus(status);- if (messageStatus) {- // Scope by sessionId: waMessageId is unique per account/chat, not global — an ack on one- // session must never advance a same-id row in another session. The In() guard makes the- // UPDATE forward-only (a late/out-of-order ack can't downgrade) and idempotent on retry.- const advanceAck = (): Promise<number> =>- this.messageRepository- .update(- { sessionId: id, waMessageId: messageId, status: In(ackStatusTransitionFrom(messageStatus)) },- { status: messageStatus },- )- .then(result => result.affected ?? 0);-- const logNoop = (): void =>- this.logger.debug(`Message ack ${messageId}: no status row advanced to ${messageStatus} (${status})`, {- sessionId: id,- messageId,- status,- action: 'message_ack_noop',- });-- const onAckError = (err: unknown): void =>- this.logger.error(`Failed to advance ack for ${messageId}`, String(err));-- void advanceAck()- .then(affected => {- if (affected > 0) return;- // affected:0 — most likely the send's 2nd save (which writes waMessageId) hasn't committed- // yet, so the row isn't matchable. Each ack is one-shot (WhatsApp won't necessarily resend),- // so retry ONCE after a short delay to close that race rather than leave it stuck at SENT.- const timer = setTimeout(() => {- void advanceAck()- .then(retried => {- if (retried === 0) logNoop();- })- .catch(onAckError);- }, ACK_RECONCILE_DELAY_MS);- timer.unref?.();- })- .catch(onAckError);- }-- // One ack payload, emitted identically over the socket and the webhook so a client coded- // against either channel sees the same shape. `id` mirrors the field every other message.*- // event carries (and the idempotency-key resolver reads). `ack` is a deprecated legacy field- // kept for backward compatibility — new consumers should read the neutral `status`.- const ackPayload = { id: messageId, messageId, status, ack: deliveryStatusToAck(status) };-- // Push the live delivery/read tick to the dashboard over the websocket.- this.eventsGateway.emitMessageAck(id, ackPayload);-- // Dispatch the delivery/read receipt to webhooks (#155). Outgoing `message.sent` is handled- // solely by `onMessageCreate`, so the ack path deliberately does NOT emit `message.sent`.- void this.webhookService.dispatch(id, 'message.ack', ackPayload);-- // Surface delivery failures actively so consumers don't have to poll for them (#220). Use a- // distinct object (not the shared ackPayload) so this separate event can't be perturbed by an- // in-place payload mutation in the concurrent message.ack dispatch's webhook:before hook.- if (status === 'failed') {- void this.webhookService.dispatch(id, 'message.failed', { ...ackPayload });- }-- // Notify plugins of the delivery/read receipt. The `message:ack` hook event was declared in- // the HookEvent union but never emitted, so any plugin registered for it silently never fired.- // Fire-and-forget: an ack is a notification with nothing downstream to cancel, so the hook's- // `continue` flag is moot. Delivery failures surface here as status `failed` — `message:failed`- // stays reserved for send-time send failures, which carry a distinct `{ error, input }` payload.- void this.hookManager.execute(- 'message:ack',- { messageId, status, ack: deliveryStatusToAck(status) },- { sessionId: id, source: 'Engine' },- );- },- onMessageRevoked: (message): void => {- if (!this.isLiveEngine(id, engine)) return;- this.logger.debug(`Message revoked: ${message.id}`, {- sessionId: id,- messageId: message.id,- action: 'message_revoked',- });-- // Flag the stored message as revoked (best-effort; the message may not be in the- // DB). The dashboard renders the localized "message deleted" text, so no display- // string is persisted here.- //- // Match on `revokedId` (the ORIGINAL deleted message's id) when present: on wwebjs- // `message.id` is the revocation notification, which never matches a stored row.- // `revokedId` falls back to `id` (Baileys, where the two are the same).- const revokedWaMessageId = message.revokedId ?? message.id;- void this.messageRepository- .update({ sessionId: id, waMessageId: revokedWaMessageId }, { body: '', type: 'revoked' })- .catch(err => {- this.logger.error(`Failed to update revoked message: ${revokedWaMessageId}`, String(err));- });-- // Notify consumers regardless of whether the row existed: webhook (message.revoked- // is a declared event) + the real-time dashboard stream.- const revokedPayload = message as unknown as Record<string, unknown>;- void this.webhookService.dispatch(id, 'message.revoked', revokedPayload);- this.eventsGateway.emitMessageRevoked(id, revokedPayload);- },+ onMessageCreate: (message): void => this.handleOwnSendEcho(id, engine, message),+ onMessageAck: (messageId, status): void => this.handleMessageAck(id, engine, messageId, status),+ onMessageRevoked: (message): void => this.handleMessageRevoked(id, engine, message),onMessageReaction: (event): void => {if (!this.isLiveEngine(id, engine)) return;if (!event.messageId) {@@ -1831,6 +1387,462 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat* column). Invoked through the per-message serialization chain in onMessageReaction, so concurrent* reactions on the same message run sequentially and don't clobber each other.*/+ /** Engine callback body, lifted out of initializeEngine so the wiring table stays readable. */+ private handleInboundMessage(id: string, engine: IWhatsAppEngine, message: IncomingMessage): void {+ if (!this.isLiveEngine(id, engine)) return;+ // Status/Story posts arrive via the inbound path for some engines; ingest them into the+ // status store instead of the message pipeline. Mirrors the isStatusBroadcast guard in+ // onMessageCreate below: an own-send echo (fromMe) stays a plain drop — never ingested,+ // never dispatched — so a status you posted never re-appears in your own webhooks/API as+ // if a contact had posted it.+ if (message.isStatusBroadcast) {+ if (message.fromMe) return;+ const status = buildIncomingStatus(message);+ if (status) {+ void this.statusStore+ .ingest(id, status)+ // Dispatch only on a fresh insert: ingest also resolves with the pre-existing row+ // for a duplicate delivery (or the winner's row after a lost insert race), and the+ // webhook must fire once per status, not once per (re)delivery.+ .then(({ row, created }) => {+ // The ingest awaited; a stop()/delete() can retire this engine mid-flight — don't+ // dispatch for a session that no longer exists (mirrors message.received's re-check).+ if (!this.isLiveEngine(id, engine)) return;+ if (created) this.dispatchStatusReceived(id, row);+ })+ .catch(err =>+ this.logger.warn('Status ingest failed', {+ sessionId: id,+ error: err instanceof Error ? err.message : String(err),+ }),+ );+ }+ return;+ }+ // Ephemeral/disappearing messages: skip persist + dispatch when the operator opted out.+ // A message is ephemeral when its chat has a disappearing-messages timer (ephemeralDuration > 0).+ if (+ !resolveFeatureFlags(this.configService).storeEphemeralMessages &&+ message.ephemeralDuration &&+ message.ephemeralDuration > 0+ ) {+ this.logger.debug('Skipping ephemeral message', {+ sessionId: id,+ messageId: message.id,+ chatId: message.chatId,+ ephemeralDuration: message.ephemeralDuration,+ });+ return;+ }+ this.logger.debug(`Message received from ${message.from}`, {+ sessionId: id,+ messageId: message.id,+ from: message.from,+ action: 'message_received',+ });+ // Update last active timestamp+ void this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);+ // Convert IncomingMessage to plain object for dispatch+ const messageData = { ...message };++ // Execute hook for message received - plugins can modify or stop processing+ void this.hookManager+ .execute('message:received', messageData, {+ sessionId: id,+ source: 'Engine',+ })+ .then(async ({ data: finalMessage }) => {+ // `continue: false` is deliberately NOT read here. It means "stop the handler chain", which+ // HookManager has already done — the plugins after the one that returned it never ran. It+ // does not mean "this message never happened".+ //+ // Honouring it here used to skip everything below: the message was never written to the+ // messages table, never dispatched to webhooks, and never emitted over the websocket. An+ // auto-reply plugin returning `false` for its ordinary purpose — keeping other bots from+ // answering the same message — silently erased the customer's message from the operator's+ // own history, leaving a thread of bot replies answering nothing. Nothing in the hook+ // contract (`HookResult.continue`, docs/19) or the webhook contract (`message.received`+ // fires when "an inbound message arrives", docs/06) hinted at that, and a sandboxed+ // marketplace plugin could swallow a session's entire inbound traffic with no audit trail.+ //+ // The message has already arrived at WhatsApp. A plugin can stop other plugins from acting+ // on it; it cannot make the gateway forget it. Pre-action hooks are where a veto belongs —+ // `message:sending` blocks a send that has not happened yet (core/hooks/sending-gate.ts).++ // Persist the incoming message so the dashboard chats view can render history.+ const incoming: IncomingMessage = finalMessage;++ // Inline @lid -> phone resolution (#263), opt-in via RESOLVE_LID_TO_PHONE. Best-effort:+ // attaches senderPhone (digits or null) before persist/dispatch so webhook/ws consumers+ // get it in a single pass. Only for privacy-id senders, so no lookup for normal numbers.+ if (resolveFeatureFlags(this.configService).resolveLidToPhone && incoming.isLidSender && !incoming.fromMe) {+ incoming.senderPhone = await this.lidResolver.resolveSenderPhone(id, incoming.author ?? incoming.from);+ }++ const metadata = buildMessageMetadata(incoming);++ const chatName = incoming.contact?.pushName ?? incoming.contact?.name ?? undefined;++ const dbMessage = this.messageRepository.create({+ sessionId: id,+ waMessageId: storableWaMessageId(incoming.id),+ chatId: incoming.chatId,+ chatName,+ // Group poster (participant JID) — `from` is the group JID, so this is the stable+ // sender identity the chat view keys attribution runs/colors on. Undefined for 1:1.+ author: incoming.author,+ from: incoming.from,+ to: incoming.to,+ body: incoming.body,+ type: incoming.type,+ direction: incoming.fromMe ? MessageDirection.OUTGOING : MessageDirection.INCOMING,+ timestamp: incoming.timestamp,+ status: MessageStatus.SENT,+ metadata,+ });++ // The hook chain above is async; a delete()/teardown can retire this engine while it+ // awaits. Re-check liveness so a late continuation can't persist an orphan messages row+ // (the row has no FK, so a session-delete cleanup would never reap it) or dispatch for a+ // session that no longer exists. Mirrors the synchronous isLiveEngine gate at entry.+ if (!this.isLiveEngine(id, engine)) return;++ // De-duplicate at the source: the engine can re-fire `message` for one inbound message+ // (#464). UNIQUE(sessionId, waMessageId) makes the insert the atomic dedup oracle — a+ // near-simultaneous re-fire loses the race and is skipped here, so persist + webhook + WS+ // happen exactly once. Fail-open: a non-conflict DB error still dispatches, so a real+ // message is never dropped by a transient DB failure.+ let isNewMessage = true;+ let persisted = false;+ try {+ // `insert()` (not `save()`) is load-bearing: the UNIQUE(sessionId, waMessageId) constraint+ // makes a duplicate insert throw, which is the atomic dedup oracle for #464 re-fires.+ // Unlike `save()`, `insert()` does NOT merge DB-generated columns (@PrimaryGeneratedColumn,+ // @CreateDateColumn) back onto the entity instance — so merge them explicitly here, before+ // the `message:persisted` emit. `identifiers[0]` always carries the PK on both SQLite and+ // Postgres; `generatedMaps[0]` adds createdAt where the driver returns it (Postgres yes;+ // SQLite historically does not — acceptable; the PK is the load-bearing field for plugins).+ const result = await this.messageRepository.insert(dbMessage as unknown as QueryDeepPartialEntity<Message>);+ Object.assign(dbMessage, result.identifiers[0] ?? {}, result.generatedMaps?.[0] ?? {});+ persisted = true;+ } catch (err) {+ if (isUniqueConstraintError(err)) {+ isNewMessage = false;+ } else {+ this.logger.error(`Failed to save incoming message ${incoming.id} to database`, String(err));+ }+ }+ if (!isNewMessage) {+ return; // duplicate re-fire — the original already persisted and dispatched+ }++ // Fire-and-forget: a plugin handler must never break the receive path. Both engine adapters+ // (wwjs `message` and Baileys `upsert`) converge on this persist, so one emit covers inbound.+ // The built-in FTS search provider is DB-synced and does NOT consume this; it exists for+ // plugin providers (Spec 2) + general use.+ // Gate ONLY the hook on `persisted`: on a non-unique insert error (transient SQLITE_BUSY /+ // lock-timeout / connection drop) the row was never stored and `dbMessage.id` is undefined,+ // so emitting `message:persisted` would hand plugins an id-less payload for a row that isn't+ // in the DB. The webhook/WS dispatch below stays fail-open — a real inbound message must+ // never be dropped on a transient DB failure; only the hook requires a durable row.+ if (persisted) {+ void this.hookManager+ .execute(+ 'message:persisted',+ { sessionId: id, message: dbMessage },+ { sessionId: id, source: 'SessionService' },+ )+ .catch(() => undefined);+ }++ // Dispatch to webhooks with potentially modified message+ void this.webhookService.dispatch(id, 'message.received', finalMessage);+ // Emit real-time event to WebSocket clients+ this.eventsGateway.emitMessage(id, finalMessage);+ })+ .catch(err => this.logger.error(`onMessage handler failed for ${id}`, String(err)));+ }++ /** Engine callback body, lifted out of initializeEngine so the wiring table stays readable. */+ private handleOwnSendEcho(id: string, engine: IWhatsAppEngine, message: IncomingMessage): void {+ if (!this.isLiveEngine(id, engine)) return;+ // `message_create` fires for every message the account creates, including sends composed on a+ // linked phone — which the `message`/`onMessage` event never delivers. Incoming messages are+ // already handled by `onMessage`, so only outgoing (`fromMe`) ones produce `message.sent` here.+ if (!message.fromMe) {+ return;+ }++ // Status/Story posts are account-created but not real conversations; don't emit `message.sent`+ // for them. The adapter flags these (the engine-specific pseudo-JID stays out of this layer).+ if (message.isStatusBroadcast) {+ return;+ }++ this.logger.debug(`Message sent to ${message.to}`, {+ sessionId: id,+ messageId: message.id,+ to: message.to,+ action: 'message_sent',+ });+ // Update last active timestamp+ void this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);+ const messageData = { ...message };++ // Execute hook for message sent - plugins can modify or stop processing+ void this.hookManager+ .execute('message:sent', messageData, {+ sessionId: id,+ source: 'Engine',+ })+ .then(async ({ data: finalMessage }) => {+ // `continue: false` is not read here, for the same reason as the message:received path+ // above: the send has already happened, so a plugin can stop the handler chain but cannot+ // un-send it. Skipping the persist below dropped the operator's own outgoing message from+ // history and from `message.sent` webhooks.++ // Persist the outgoing message so local history reflects sends composed on a linked phone+ // (message_create is the ONLY event those produce). It also fires for API-originated sends,+ // which the REST send path persists itself — the UNIQUE(sessionId, waMessageId) index is+ // the atomic dedup oracle between the two writers: the loser skips its insert, and+ // persistSentState additionally drops its redundant PENDING row when the echo won. The+ // webhook/WS dispatch below is identical whether the insert won, lost, or failed — the+ // message.sent contract is unchanged.+ const outgoing: IncomingMessage = finalMessage;+ const metadata = buildMessageMetadata(outgoing, true);++ // The ephemeral opt-out gates STORAGE only (mirrors onMessage); the live dispatch below+ // is today's contract and stays.+ const mayPersist =+ resolveFeatureFlags(this.configService).storeEphemeralMessages ||+ !(outgoing.ephemeralDuration && outgoing.ephemeralDuration > 0);++ if (mayPersist) {+ const dbMessage = this.messageRepository.create({+ sessionId: id,+ waMessageId: storableWaMessageId(outgoing.id),+ chatId: outgoing.chatId,+ from: outgoing.from,+ to: outgoing.to,+ body: outgoing.body,+ type: outgoing.type,+ direction: MessageDirection.OUTGOING,+ timestamp: outgoing.timestamp,+ status: MessageStatus.SENT,+ metadata,+ });+ // The hook chain above is async; a delete()/teardown can retire this engine while it+ // awaits. Re-check liveness so a late continuation can't persist an orphan row+ // (mirrors onMessage).+ if (!this.isLiveEngine(id, engine)) return;+ let persisted = false;+ try {+ const result = await this.messageRepository.insert(dbMessage as unknown as QueryDeepPartialEntity<Message>);+ Object.assign(dbMessage, result.identifiers[0] ?? {}, result.generatedMaps?.[0] ?? {});+ persisted = true;+ } catch (err) {+ // Unique violation = the REST send path already persisted this API-originated send —+ // the dedup oracle working as intended, not an error. Anything else is a real DB+ // failure; fail open so a real send is never dropped on a transient DB fault.+ if (!isUniqueConstraintError(err)) {+ this.logger.error(`Failed to save outgoing message ${outgoing.id} to database`, String(err));+ }+ }+ if (persisted) {+ // Fire-and-forget, mirroring onMessage: plugin providers (search etc.) see phone-+ // composed sends exactly like API sends.+ void this.hookManager+ .execute(+ 'message:persisted',+ { sessionId: id, message: dbMessage },+ { sessionId: id, source: 'SessionService' },+ )+ .catch(() => undefined);+ }+ }++ void this.webhookService.dispatch(id, 'message.sent', finalMessage);+ // Emit real-time event to WebSocket clients (as message.sent, not message.received)+ this.eventsGateway.emitMessageSent(id, finalMessage);+ })+ .catch(err => this.logger.error(`onMessageCreate handler failed for ${id}`, String(err)));+ }++ /** Engine callback body, lifted out of initializeEngine so the wiring table stays readable. */+ private handleMessageAck(id: string, engine: IWhatsAppEngine, messageId: string, status: DeliveryStatus): void {+ if (!this.isLiveEngine(id, engine)) return;+ this.logger.debug(`Message ack: ${messageId} -> ${status}`, {+ sessionId: id,+ messageId,+ status,+ action: 'message_ack',+ });++ // Reflect real delivery state on the stored message (#220): delivered/read/failed advance the+ // stored status; pending/sent carry no upgrade (it's already SENT — visibly "not delivered").+ // The UPDATE is guarded to the allowed prior statuses so delivery state only ADVANCES: an+ // out-of-order/late ack cannot downgrade a higher status, which also makes these+ // fire-and-forget writes race-safe at the DB level.+ const messageStatus = deliveryStatusToMessageStatus(status);+ if (messageStatus) {+ // Scope by sessionId: waMessageId is unique per account/chat, not global — an ack on one+ // session must never advance a same-id row in another session. The In() guard makes the+ // UPDATE forward-only (a late/out-of-order ack can't downgrade) and idempotent on retry.+ const advanceAck = (): Promise<number> =>+ this.messageRepository+ .update(+ { sessionId: id, waMessageId: messageId, status: In(ackStatusTransitionFrom(messageStatus)) },+ { status: messageStatus },+ )+ .then(result => result.affected ?? 0);++ const logNoop = (): void =>+ this.logger.debug(`Message ack ${messageId}: no status row advanced to ${messageStatus} (${status})`, {+ sessionId: id,+ messageId,+ status,+ action: 'message_ack_noop',+ });++ const onAckError = (err: unknown): void =>+ this.logger.error(`Failed to advance ack for ${messageId}`, String(err));++ void advanceAck()+ .then(affected => {+ if (affected > 0) return;+ // affected:0 — most likely the send's 2nd save (which writes waMessageId) hasn't committed+ // yet, so the row isn't matchable. Each ack is one-shot (WhatsApp won't necessarily resend),+ // so retry ONCE after a short delay to close that race rather than leave it stuck at SENT.+ const timer = setTimeout(() => {+ void advanceAck()+ .then(retried => {+ if (retried === 0) logNoop();+ })+ .catch(onAckError);+ }, ACK_RECONCILE_DELAY_MS);+ timer.unref?.();+ })+ .catch(onAckError);+ }++ // One ack payload, emitted identically over the socket and the webhook so a client coded+ // against either channel sees the same shape. `id` mirrors the field every other message.*+ // event carries (and the idempotency-key resolver reads). `ack` is a deprecated legacy field+ // kept for backward compatibility — new consumers should read the neutral `status`.+ const ackPayload = { id: messageId, messageId, status, ack: deliveryStatusToAck(status) };++ // Push the live delivery/read tick to the dashboard over the websocket.+ this.eventsGateway.emitMessageAck(id, ackPayload);++ // Dispatch the delivery/read receipt to webhooks (#155). Outgoing `message.sent` is handled+ // solely by `onMessageCreate`, so the ack path deliberately does NOT emit `message.sent`.+ void this.webhookService.dispatch(id, 'message.ack', ackPayload);++ // Surface delivery failures actively so consumers don't have to poll for them (#220). Use a+ // distinct object (not the shared ackPayload) so this separate event can't be perturbed by an+ // in-place payload mutation in the concurrent message.ack dispatch's webhook:before hook.+ if (status === 'failed') {+ void this.webhookService.dispatch(id, 'message.failed', { ...ackPayload });+ }++ // Notify plugins of the delivery/read receipt. The `message:ack` hook event was declared in+ // the HookEvent union but never emitted, so any plugin registered for it silently never fired.+ // Fire-and-forget: an ack is a notification with nothing downstream to cancel, so the hook's+ // `continue` flag is moot. Delivery failures surface here as status `failed` — `message:failed`+ // stays reserved for send-time send failures, which carry a distinct `{ error, input }` payload.+ void this.hookManager.execute(+ 'message:ack',+ { messageId, status, ack: deliveryStatusToAck(status) },+ { sessionId: id, source: 'Engine' },+ );+ }++ /** Engine callback body, lifted out of initializeEngine so the wiring table stays readable. */+ private handleEngineReady(id: string, engine: IWhatsAppEngine, phone: string, pushName: string): void {+ if (!this.isLiveEngine(id, engine)) return;+ this.logger.log(`Session ready: ${phone}`, {+ sessionId: id,+ phone,+ pushName,+ action: 'ready',+ });++ void this.webhookService.dispatch(id, 'session.authenticated', { sessionId: id, phone, pushName });+ this.eventsGateway.emitSessionAuthenticated(id, { phone, pushName });++ // Execute hook for ready event+ void this.hookManager.execute(+ 'session:ready',+ { phone, pushName },+ {+ sessionId: id,+ source: 'Engine',+ },+ );++ // Reset reconnect attempts and clear any stale failure reason on success+ const reconnectState = this.reconnectStates.get(id);+ if (reconnectState) {+ reconnectState.attempts = 0;+ }+ // A fresh READY stretch starts the watchdog's failure budget clean too.+ this.watchdog.clear(id);+ this.sessionErrors.delete(id);+ // READY proves any in-flight stuck-auth recovery succeeded (or none was needed), so the+ // one-shot recovery budget is re-armed for a future episode.+ this.stuckAuthRecoveryUsed.delete(id);++ void this.sessionRepository+ .update(id, {+ status: SessionStatus.READY,+ phone,+ pushName,+ connectedAt: new Date(),+ lastActiveAt: new Date(),+ })+ .catch(err =>+ this.logger.warn('Failed to persist session ready state', {+ sessionId: id,+ error: err instanceof Error ? err.message : String(err),+ }),+ );++ // Best-effort snapshot of the account's own contacts' currently-active statuses. Live status+ // posts arrive through onMessage below; this just backfills what was already up before we+ // connected. Not awaited — onReady must not block on it.+ void this.seedStatuses(id, engine);+ }++ /** Engine callback body, lifted out of initializeEngine so the wiring table stays readable. */+ private handleMessageRevoked(id: string, engine: IWhatsAppEngine, message: RevokedMessage): void {+ if (!this.isLiveEngine(id, engine)) return;+ this.logger.debug(`Message revoked: ${message.id}`, {+ sessionId: id,+ messageId: message.id,+ action: 'message_revoked',+ });++ // Flag the stored message as revoked (best-effort; the message may not be in the+ // DB). The dashboard renders the localized "message deleted" text, so no display+ // string is persisted here.+ //+ // Match on `revokedId` (the ORIGINAL deleted message's id) when present: on wwebjs+ // `message.id` is the revocation notification, which never matches a stored row.+ // `revokedId` falls back to `id` (Baileys, where the two are the same).+ const revokedWaMessageId = message.revokedId ?? message.id;+ void this.messageRepository+ .update({ sessionId: id, waMessageId: revokedWaMessageId }, { body: '', type: 'revoked' })+ .catch(err => {+ this.logger.error(`Failed to update revoked message: ${revokedWaMessageId}`, String(err));+ });++ // Notify consumers regardless of whether the row existed: webhook (message.revoked+ // is a declared event) + the real-time dashboard stream.+ const revokedPayload = message as unknown as Record<string, unknown>;+ void this.webhookService.dispatch(id, 'message.revoked', revokedPayload);+ this.eventsGateway.emitMessageRevoked(id, revokedPayload);+ }+private async applyReaction(id: string, event: ReactionEvent): Promise<void> {try {// Guard the lookup key before it reaches TypeORM: `findOne` DROPS an undefined condition from