Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): answer a contact lookup with the saved twin, not the bare one
Yudhi Armyndharis55845960e50e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3a84d1d68..ab66df764 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- A whatsapp-web.js pairing-code retry cancels the attempt it abandoned before starting the next one. The per-attempt timeout stops waiting but does not stop the library's in-page linking flow, which keeps re-requesting a code on its own interval, so retries used to stack competing flows over one device slot.- A Baileys session whose credentials carry no LID of their own no longer reports a `group.join` for a group it created itself: every LID comparison answered false, so the creator was never recognised. The session's own LID mapping settles it instead.- Starting a session that turns out to be linked already no longer opens a QR modal over it, which then polled for a code that could never arrive. The decision now reads the list the dashboard re-reads after the start, not the state it held before it.+- `GET /api/sessions/:sessionId/contacts/:contactId` answers with the saved contact when the same person occupies two store entries, one keyed by LID and one by phone number. Only one of the two carries the saved name, and the lookup returned whichever it reached first, so a saved contact could answer with no name and `isMyContact: false`.- A QR code that arrives while the dashboard is asking the server about a disconnect no longer has its modal closed underneath it. The handler blanks the displayed code, re-reads the session list, and closed the modal when the answer said no engine was left; a reconnect completing inside that window had already pushed a fresh, scannable code.- Escape dismisses the Chats emoji picker instead of closing the whole conversation behind it.- A click on a WhatsApp Business prompt quotes the prompt in the stored reply, so the dashboard renders the question above the answer instead of an empty quote box.diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.tsindex 7b4574cf1..37923e83b 100644--- a/src/engine/adapters/baileys-session-store.spec.ts+++ b/src/engine/adapters/baileys-session-store.spec.ts@@ -54,6 +54,16 @@ describe('BaileysSessionStore', () => {expect(store.findContact('[email protected]')).toMatchObject({ name: 'Alice', pushName: 'Al' });});+ it('prefers the named twin when one person occupies both a lid and a phone entry', () => {+ // History sync and app state can key the same person twice. Only one entry carries the saved+ // name, and answering with the other reported a saved contact as unknown.+ store.upsertContacts([{ id: '111@lid', notify: 'Al' }]);+ store.upsertContacts([{ id: '[email protected]', lid: '111@lid', name: 'Alice' }]);++ expect(store.findContact('111@lid')).toMatchObject({ name: 'Alice', isMyContact: true });+ expect(store.findContact('[email protected]')).toMatchObject({ name: 'Alice', isMyContact: true });+ });+it('accepts a contact keyed only by lid (id omitted) and finds it by phone', () => {store.upsertContacts([{ lid: '111@lid', phoneNumber: '[email protected]', name: 'Ada' }]);expect(store.findContact('111@lid')?.name).toBe('Ada');diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.tsindex cdfbc3b81..105df3e25 100644--- a/src/engine/adapters/baileys-session-store.ts+++ b/src/engine/adapters/baileys-session-store.ts@@ -380,15 +380,22 @@ export class BaileysSessionStore {if (parsed.kind === 'user') {keys.push(`${parsed.userPart}@s.whatsapp.net`, `${parsed.userPart}@c.us`);}+ // One person can occupy two entries, one keyed by `@lid` and one by the phone dialect, and only+ // one of them carries the saved name. Prefer the named one: the nameless twin answers+ // `isMyContact: false` and no display name for somebody the account has saved.+ let unnamed: BaileysContact | undefined;for (const key of keys) {const direct = this.contacts.get(key);- if (direct) {- return this.toNeutralContact(direct);- }+ if (!direct) continue;+ if (direct.name) return this.toNeutralContact(direct);+ unnamed ??= direct;}if (parsed.kind !== 'user' && parsed.kind !== 'lid') {- return null;+ return unnamed ? this.toNeutralContact(unnamed) : null;}+ // The twin is keyed under the OTHER dialect, so a direct hit cannot reach it; the scan below+ // can, through `lid`/`phoneNumber`. Run it even when a direct hit was found, as long as that hit+ // was nameless, and keep the nameless one only if the scan turns up nothing better.const want = parsed.userPart;for (const c of this.contacts.values()) {const phone = c.phoneNumber@@ -397,11 +404,11 @@ export class BaileysSessionStore {? userPart(c.id): '';const lid = c.lid ? userPart(c.lid) : c.id.endsWith('@lid') ? userPart(c.id) : '';- if (phone === want || lid === want) {- return this.toNeutralContact(c);- }+ if (phone !== want && lid !== want) continue;+ if (c.name) return this.toNeutralContact(c);+ unnamed ??= c;}- return null;+ return unnamed ? this.toNeutralContact(unnamed) : null;}listChats(): ChatSummary[] {