Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
refactor(engine): read wids in one place
Yudhi Armyndharis9cc08f2e87c4
diff --git a/src/engine/adapters/message-mapper.ts b/src/engine/adapters/message-mapper.tsindex db583ce37..1f658c645 100644--- a/src/engine/adapters/message-mapper.ts+++ b/src/engine/adapters/message-mapper.ts@@ -1,5 +1,5 @@import { EditedMessage, IncomingMessage, MessageContact, MessageType } from '../interfaces/whatsapp-engine.interface';-import type { SerializedWid } from '../types/whatsapp-web-js.types';+import { readWid, type SerializedWid } from '../types/whatsapp-web-js.types';import { chatKind } from '../identity/wa-id';/**@@ -106,7 +106,7 @@ export function buildIncomingMessageBase(msg: RawMessageFields): IncomingMessage// backport every arriving message otherwise carries `id: undefined`. The empty sentinel means// "received, id unreadable" and is normalized to NULL where it is persisted — never to `''`, which// the non-partial (sessionId, waMessageId) unique index would collide the second such message on.- id: msg.id._serialized ?? msg.id.$1 ?? '',+ id: readWid(msg.id) ?? '',from: msg.from,to: msg.to,chatId,diff --git a/src/engine/adapters/wwebjs-channels.ts b/src/engine/adapters/wwebjs-channels.tsindex ef9eeff17..54f65a55f 100644--- a/src/engine/adapters/wwebjs-channels.ts+++ b/src/engine/adapters/wwebjs-channels.ts@@ -1,6 +1,6 @@import { type Client } from 'whatsapp-web.js';import { Channel, ChannelMessage } from '../interfaces/whatsapp-engine.interface';-import { BusinessClient, WwjsChannelData } from '../types/whatsapp-web-js.types';+import { BusinessClient, WwjsChannelData, readWid } from '../types/whatsapp-web-js.types';import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';import { EngineRefusedError } from '../../common/errors/engine-refused.error';import { ChannelNotFoundError } from '../../common/errors/channel-not-found.error';@@ -35,7 +35,7 @@ export class WwebjsChannels {return channels.map((ch: WwjsChannelData) => ({// Read `$1` before giving up (#747: WA Web's minifier renamed the serialized property), and// never String() the object branch — that manufactures the literal "undefined" as an id.- id: (typeof ch.id === 'object' ? (ch.id._serialized ?? ch.id.$1) : String(ch.id)) || '',+ id: readWid(ch.id) || '',name: String(ch.name || ''),description: ch.description ? String(ch.description) : undefined,inviteCode: ch.inviteCode ? String(ch.inviteCode) : undefined,@@ -68,7 +68,7 @@ export class WwebjsChannels {// The nid crosses the puppeteer boundary as a raw page-context Wid, exactly the object class// WA Web's minifier rename hits (#747): read `$1` before giving up, and never String() an// absent id — a channel with the literal id "undefined" is unusable for every follow-up call.- const channelId = result.nid._serialized ?? result.nid.$1;+ const channelId = readWid(result.nid);if (!channelId) {throw new EngineRefusedError(`Channel '${name}' was created but its id was unreadable — refusing to return it`);}@@ -233,7 +233,7 @@ export class WwebjsChannels {// unreadable id into the literal "undefined" rather than the empty sentinel every other path// uses. Read-only endpoint — never persisted, never ack-matched — so `''` carries no collision// risk here; it just means "id unreadable".- id: (typeof msg.id === 'object' ? (msg.id?._serialized ?? msg.id?.$1) : msg.id) || '',+ id: readWid(msg.id) || '',body: String(msg.body || ''),timestamp: Number(msg.timestamp),hasMedia: Boolean(msg.hasMedia),diff --git a/src/engine/adapters/wwebjs-group-events.ts b/src/engine/adapters/wwebjs-group-events.tsindex 331140e55..8a84877e5 100644--- a/src/engine/adapters/wwebjs-group-events.ts+++ b/src/engine/adapters/wwebjs-group-events.ts@@ -1,7 +1,7 @@import { type Client, type GroupNotification } from 'whatsapp-web.js';import { type EngineEventCallbacks, GroupEvent } from '../interfaces/whatsapp-engine.interface';import { type createLogger } from '../../common/services/logger.service';-import { SerializedWid } from '../types/whatsapp-web-js.types';+import { SerializedWid, readWid } from '../types/whatsapp-web-js.types';/*** Interpret the on/off value a group settings notification carries in `body` ('on'/'true' → true,@@ -58,7 +58,7 @@ export function wwebjsGroupRecipientIds(notification: GroupNotification): string.map(entry => {if (typeof entry === 'string') return entry;const wid = entry as SerializedWid | undefined;- return wid?._serialized ?? wid?.$1 ?? '';+ return readWid(wid) ?? '';}).filter(id => id.length > 0);}diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.tsindex 6661350ee..a5605e901 100644--- a/src/engine/adapters/wwebjs-message-events.ts+++ b/src/engine/adapters/wwebjs-message-events.ts@@ -6,7 +6,7 @@ import {type ReactionEvent,type EditedMessage,} from '../interfaces/whatsapp-engine.interface';-import { type SerializedWid } from '../types/whatsapp-web-js.types';+import { readWid, type SerializedWid } from '../types/whatsapp-web-js.types';import { buildEditedMessage, buildIncomingMessageBase, mapContactFields } from './message-mapper';import { extractWwebjsCall, plannedInboundDownload, wwebjsAckToDeliveryStatus } from './wwebjs-messaging';import { admitInboundSafely, inboundMediaTimeoutMs, withInboundDownloadTimeout } from './inbound-media-cap';@@ -118,7 +118,7 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo// has a perfectly good id here, and dropping it strands the message at SENT — including the// `ack < 0` that is the only signal a send failed.const rawId = msg.id as unknown as SerializedWid | undefined;- const ackId = rawId?._serialized ?? rawId?.$1;+ const ackId = readWid(rawId);if (!ackId) {host.logger.warn('Dropping an ack whose message id could not be read', { ack });return;@@ -149,8 +149,8 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHoconst afterId = after.id as unknown as SerializedWid | undefined;const beforeId = before?.id as unknown as SerializedWid | undefined;const payload: RevokedMessage = {- id: afterId?._serialized ?? afterId?.$1 ?? '',- revokedId: beforeId?._serialized ?? beforeId?.$1,+ id: readWid(afterId) ?? '',+ revokedId: readWid(beforeId),// Direction flag, as Message._getChatId: an own message in a LID chat carries the own LID as// `from`, which never equals the phone-dialect account wid.chatId: after.fromMe ? after.to : after.from,@@ -179,7 +179,7 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo// emit another message's reactions. Empty string finds nothing and returns cleanly.const msgId = reaction.msgId as unknown as SerializedWid;const event: ReactionEvent = {- messageId: msgId?._serialized ?? msgId?.$1 ?? '',+ messageId: readWid(msgId) ?? '',chatId: reaction.id.remote,reaction: reaction.reaction,senderId: reaction.senderId,diff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.tsindex 48e9e6fe3..da9f5351a 100644--- a/src/engine/adapters/wwebjs-messaging.ts+++ b/src/engine/adapters/wwebjs-messaging.ts@@ -335,7 +335,7 @@ export function toMessageResult(msg: Message | undefined): MessageResult {);}const id = msg.id as unknown as SerializedWid | undefined;- return { id: id?._serialized ?? id?.$1 ?? '', timestamp: msg.timestamp };+ return { id: readWid(id) ?? '', timestamp: msg.timestamp };}/**diff --git a/src/engine/adapters/wwebjs-status.ts b/src/engine/adapters/wwebjs-status.tsindex e39723b0f..0501f2b05 100644--- a/src/engine/adapters/wwebjs-status.ts+++ b/src/engine/adapters/wwebjs-status.ts@@ -7,7 +7,7 @@ import {StatusPostOptions,StatusResult,} from '../interfaces/whatsapp-engine.interface';-import { SerializedWid } from '../types/whatsapp-web-js.types';+import { SerializedWid, readWid } from '../types/whatsapp-web-js.types';import { EngineRefusedError } from '../../common/errors/engine-refused.error';import { toMessageMedia } from './wwebjs-messaging';import { type WwebjsEngineHost, withPage, reportPageDeath } from './wwebjs-host';@@ -40,7 +40,7 @@ export function toStatusResult(msg: Message | undefined): StatusResult {const id = msg.id as unknown as SerializedWid | undefined;const ts = msg.timestamp ? new Date(msg.timestamp * 1000) : new Date();return {- statusId: id?._serialized ?? id?.$1 ?? '',+ statusId: readWid(id) ?? '',timestamp: ts,expiresAt: new Date(ts.getTime() + 24 * 3_600_000),};@@ -117,7 +117,7 @@ export class WwebjsStatus {statuses.push({// `deleteStatus` takes this id as its revoke handle, so losing it to the rename makes a// listed status unactionable (#747). The contact id above is a Wid and is unaffected.- id: ((msg.id as unknown as SerializedWid)?._serialized ?? (msg.id as unknown as SerializedWid)?.$1) || '',+ id: readWid(msg.id) || '',contact: contactSummary,type:msg.type === MessageTypes.IMAGE