Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): list a blocked Baileys contact once

Yudhi Armyndharisab5fa9e43777
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.ts
index fbf609434..102e64be1 100644
--- a/src/engine/adapters/baileys-contacts.ts
+++ b/src/engine/adapters/baileys-contacts.ts
@@ -178,7 +178,8 @@ export class BaileysContacts {
* The read half of block/unblockContact. Bounded by our own clock: Baileys' `query()` swallows
* its timeout, and an unanswered blocklist query would otherwise surface as an EMPTY blocklist —
* a claim about the account sold in place of a transport failure. Wire items without a jid attr
- * are dropped rather than reported as "undefined".
+ * are dropped rather than reported as "undefined". A contact held under both its LID and its phone
+ * is two wire items that read as one id, which is listed once.
*/
async getBlockedContacts(budgetMs: number = this.queryBudgetMs): Promise<string[]> {
this.host.ensureReady();
@@ -187,7 +188,8 @@ export class BaileysContacts {
budgetMs,
'WhatsApp did not answer the blocklist query in time',
);
- return (jids ?? []).filter((jid): jid is string => Boolean(jid)).map(jid => this.host.toNeutralJid(jid));
+ const ids = (jids ?? []).filter((jid): jid is string => Boolean(jid)).map(jid => this.host.toNeutralJid(jid));
+ return [...new Set(ids)];
}
async setProfileName(name: string): Promise<void> {
diff --git a/src/engine/adapters/blocked-contacts.spec.ts b/src/engine/adapters/blocked-contacts.spec.ts
index 12a17c906..90f765882 100644
--- a/src/engine/adapters/blocked-contacts.spec.ts
+++ b/src/engine/adapters/blocked-contacts.spec.ts
@@ -65,13 +65,16 @@ describe('WwebjsContacts.getBlockedContacts', () => {
});
describe('BaileysContacts.getBlockedContacts', () => {
- function makeContacts(): { contacts: BaileysContacts; sock: { fetchBlocklist: jest.Mock } } {
+ function makeContacts(toNeutralJid = (jid: string): string => jid.replace('@s.whatsapp.net', '@c.us')): {
+ contacts: BaileysContacts;
+ sock: { fetchBlocklist: jest.Mock };
+ } {
const sock = { fetchBlocklist: jest.fn() };
const host = {
ensureReady: jest.fn(),
getSocket: () => sock as unknown as WASocket,
logger,
- toNeutralJid: (jid: string) => jid.replace('@s.whatsapp.net', '@c.us'),
+ toNeutralJid,
};
return { contacts: new BaileysContacts(host as never), sock };
}
@@ -90,6 +93,16 @@ describe('BaileysContacts.getBlockedContacts', () => {
await expect(contacts.getBlockedContacts()).resolves.toEqual(['[email protected]']);
});
+ it('lists an id once when the blocklist holds a contact under both its LID and its phone', async () => {
+ // The session knows the pair, so both wire jids fold to the contact's phone id.
+ const { contacts, sock } = makeContacts(jid =>
+ jid === '111222333@lid' ? '[email protected]' : jid.replace('@s.whatsapp.net', '@c.us'),
+ );
+ sock.fetchBlocklist.mockResolvedValue(['111222333@lid', '[email protected]', '[email protected]']);
+
+ await expect(contacts.getBlockedContacts()).resolves.toEqual(['[email protected]', '[email protected]']);
+ });
+
it('resolves [] for an empty blocklist', async () => {
const { contacts, sock } = makeContacts();
sock.fetchBlocklist.mockResolvedValue([]);