Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(contacts): read ids via readWid and skip unreadable entries with a count
Deyvis17GYdf9388699c0f
diff --git a/src/engine/adapters/wwebjs-contacts.ts b/src/engine/adapters/wwebjs-contacts.tsindex eeaf80c74..eeef5566b 100644--- a/src/engine/adapters/wwebjs-contacts.ts+++ b/src/engine/adapters/wwebjs-contacts.ts@@ -5,6 +5,9 @@ import { userPart } from '../identity/wa-id';import { readWid, type SerializedWid } from '../types/whatsapp-web-js.types';import { type WwebjsEngineHost } from './wwebjs-host';+/** The raw whatsapp-web.js contact element type, kept local so the wwebjs `Contact` type never leaks. */+type RawWwebjsContact = Awaited<ReturnType<Client['getContacts']>>[number];+/*** Contact operations extracted from WhatsAppWebJsAdapter. The adapter keeps the public methods as* thin forwarders and injects the shared host surface (./wwebjs-host) via closures, so the delegate@@ -18,37 +21,65 @@ export class WwebjsContacts {return this.host.getClient();}+ /**+ * Map a raw whatsapp-web.js contact to the library-agnostic {@link Contact}. The id is read+ * through {@link readWid} so both the `_serialized` and post-rename `$1` shapes resolve; a+ * contact with no readable id returns null so callers can skip it instead of dereferencing+ * unguarded (the shape that lets a single bad entry reject the whole request).+ */+ private toContact(c: RawWwebjsContact): Contact | null {+ const id = readWid(c.id as unknown as SerializedWid);+ if (!id) return null;+ return {+ id,+ name: c.name || undefined,+ pushName: c.pushname || undefined,+ number: c.number,+ isMyContact: c.isMyContact,+ isBlocked: c.isBlocked,+ };+ }+async getContacts(): Promise<Contact[]> {this.host.ensureReady();++ let raw: RawWwebjsContact[];try {- const contacts = await this.client().getContacts();-- return contacts.map(c => ({- id: c.id._serialized,- name: c.name || undefined,- pushName: c.pushname || undefined,- number: c.number,- isMyContact: c.isMyContact,- isBlocked: c.isBlocked,- }));+ raw = await this.client().getContacts();} catch (error) {- this.host.reportIfPageTransportError(error, 'getContacts');+ // A dead page surfaces here as a raw Puppeteer error; convert it to the documented transport+ // failure the way wwebjs-chats.ts does, so a transport death answers 503 instead of a bare 500.+ if (this.host.isPageTransportError(error)) {+ this.host.reportIfPageTransportError(error, 'getContacts');+ throw new EngineTransportError('Transport died while reading contacts');+ }throw error;}++ // Read every id through readWid (so the `_serialized`->`$1` rename lands here too) and skip+ // entries with no readable id, counted and logged like the chats path, rather than dropping+ // the whole address book silently.+ const contacts: Contact[] = [];+ let skipped = 0;+ for (const c of raw) {+ const mapped = this.toContact(c);+ if (!mapped) {+ skipped++;+ continue;+ }+ contacts.push(mapped);+ }+ if (skipped > 0) {+ this.host.logger.warn(`Skipped ${skipped} contact(s) without a serialized id`);+ }+ return contacts;}async getContactById(contactId: string): Promise<Contact | null> {this.host.ensureReady();try {const contact = await this.client().getContactById(contactId);- return {- id: contact.id._serialized,- name: contact.name || undefined,- pushName: contact.pushname || undefined,- number: contact.number,- isMyContact: contact.isMyContact,- isBlocked: contact.isBlocked,- };+ return this.toContact(contact);} catch (error) {// Unlike the avatar lookup, a throw here can legitimately mean the contact is absent:// `window.WWebJS.getContact` has no try/catch and reads `contact.isBusiness` straight off