Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): give a contact reached through its LID its phone number

Yudhi Armyndharis61da6765af89
diff --git a/.env.example b/.env.example
index 9cd30b50d..a4d706163 100644
--- a/.env.example
+++ b/.env.example
@@ -264,8 +264,10 @@ OPENWA_PIDS_LIMIT=2048
# LID_MAPPING_CACHE_MAX=5000
# Per-map cap on the Baileys session store's in-memory maps (contacts, chats, lastMessages, lidToPn,
-# ephemeralByChat — all fed by peer traffic). A miss falls back to the persisted lid table, a
+# ephemeralByChat, all fed by peer traffic). A miss falls back to the persisted lid table, a
# re-resolution, or a defined empty result, so eviction never loses data. 0 disables the cap. Default 5000.
+# In the contacts map it bounds the PEER half only: a contact carrying a name you saved is exempt, so
+# that map is bounded by the account's own address book and lowering this knob will not shrink it.
# BAILEYS_SESSION_STORE_MAX_ENTRIES=5000
# Global cap on the in-memory Baileys chat mute/archive/pin mirror, shared by all sessions. An evicted
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 96d592744..46b5e12b3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A `socks5://` or `socks4://` proxy at an IPv6 literal connects: the brackets `URL` keeps were going on the wire as part of a hostname.
- 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`.
+- `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 contact reached through its LID also carries its `number` now, read from the resolved id rather than from the LID key, and `GET .../contacts` lists such a person once instead of twice when both entries carry the saved name.
- 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.ts
index 37923e83b..b9b096dc9 100644
--- a/src/engine/adapters/baileys-session-store.spec.ts
+++ b/src/engine/adapters/baileys-session-store.spec.ts
@@ -64,6 +64,27 @@ describe('BaileysSessionStore', () => {
expect(store.findContact('[email protected]')).toMatchObject({ name: 'Alice', isMyContact: true });
});
+ it('answers a lid lookup with the phone number when both twins carry a saved name', () => {
+ // The preference above is keyed on `name` alone, so when BOTH entries are named the queried
+ // dialect wins. For a lid query that is the lid-keyed entry, whose `number` is empty unless it
+ // is derived from the resolved id: a saved contact answered with no phone number at all.
+ store.upsertContacts([{ id: '111@lid', name: 'Alice' }]);
+ store.upsertContacts([{ id: '[email protected]', lid: '111@lid', name: 'Alice' }]);
+
+ expect(store.findContact('111@lid')).toMatchObject({ id: '[email protected]', name: 'Alice', number: '628111' });
+ });
+
+ it('lists a person once when both of their entries carry a saved name', () => {
+ // Both project to the same neutral id once the lid resolves, so listing both puts two rows
+ // sharing one id into GET /contacts.
+ store.upsertContacts([{ id: '111@lid', name: 'Alice' }]);
+ store.upsertContacts([{ id: '[email protected]', lid: '111@lid', name: 'Alice' }]);
+
+ expect(store.listContacts()).toEqual([
+ expect.objectContaining({ id: '[email protected]', name: 'Alice', number: '628111' }),
+ ]);
+ });
+
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.ts
index 105df3e25..bd3f2e555 100644
--- a/src/engine/adapters/baileys-session-store.ts
+++ b/src/engine/adapters/baileys-session-store.ts
@@ -368,7 +368,21 @@ export class BaileysSessionStore {
listContacts(): Contact[] {
// GET /contacts is the address book, not "everyone this session has ever seen". Baileys
// documents `name` as the one YOU saved; `notify` is only the pushname they set themselves.
- return [...this.contacts.values()].filter(c => c.name).map(c => this.toNeutralContact(c));
+ //
+ // Deduplicated by neutral id: one person can occupy two entries, one keyed by `@lid` and one by
+ // the phone dialect, and when both carry a saved name they project to the SAME id once the lid
+ // resolves. Listing both put two rows sharing one id into the answer. The richer projection
+ // wins, so a twin that resolved to a number is not displaced by one that did not.
+ const byId = new Map<string, Contact>();
+ for (const c of this.contacts.values()) {
+ if (!c.name) continue;
+ const contact = this.toNeutralContact(c);
+ const existing = byId.get(contact.id);
+ if (!existing || (!existing.number && contact.number)) {
+ byId.set(contact.id, contact);
+ }
+ }
+ return [...byId.values()];
}
findContact(id: string): Contact | null {
@@ -491,9 +505,14 @@ export class BaileysSessionStore {
}
private toNeutralContact(c: BaileysContact): Contact {
- const number = c.phoneNumber ? userPart(c.phoneNumber) : c.id.endsWith('@s.whatsapp.net') ? userPart(c.id) : '';
+ // The number is read off the NEUTRAL id, which has already done the lid resolution: a lid-keyed
+ // entry whose mapping is known projects to `<phone>@c.us` and carries its number, where reading
+ // the raw `@lid` key answered an empty string for somebody the account has saved. An unresolved
+ // lid still answers '', which is the honest answer there.
+ const id = this.toNeutralJid(c.id);
+ const number = c.phoneNumber ? userPart(c.phoneNumber) : id.endsWith('@c.us') ? userPart(id) : '';
return {
- id: this.toNeutralJid(c.id),
+ id,
name: c.name ?? c.verifiedName,
pushName: c.notify,
number,