Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): qualify a bare participant number before the engine fold
Yudhi Armyndharis84aecc7ef98b
diff --git a/src/engine/adapters/baileys-groups.ts b/src/engine/adapters/baileys-groups.tsindex 855620f01..b770648ca 100644--- a/src/engine/adapters/baileys-groups.ts+++ b/src/engine/adapters/baileys-groups.ts@@ -17,6 +17,7 @@ import { EngineTransportError } from '../../common/errors/engine-transport.errorimport { InvalidInviteCodeError } from '../../common/errors/invalid-invite-code.error';import { type createLogger } from '../../common/services/logger.service';import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';+import { toParticipantWid } from '../identity/wa-id';/*** Group-domain operations extracted from BaileysAdapter. The adapter keeps the public@@ -89,9 +90,15 @@ export async function mapServerRefusal<T>(/*** Fold neutral `<phone>@c.us` participant ids back to the engine wire dialect (`@s.whatsapp.net`) before* a group write. `@lid` (a first-class addressing mode) and the group id itself are left untouched.+ *+ * A BARE number is qualified first. `toEngineJid` folds only an already-domained user id, so a bare+ * number used to travel verbatim into the participant node — and Baileys' encoder writes an+ * un-domained string as a packed nibble rather than a JID_PAIR, so WhatsApp received an attribute+ * that was not a JID and the write did nothing. The bare form is the documented convenience input on+ * these routes and the service guard accepts it, so it has to be addressable by the time it lands here.*/export function toEngineParticipants(participants: string[], toEngineJid: (jid: string) => string): string[] {- return participants.map(toEngineJid);+ return participants.map(p => toEngineJid(toParticipantWid(p)));}/** The neutral method tokens — Baileys' wire tokens are already this vocabulary. */diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 384dc7e95..a2930a19b 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -3027,6 +3027,23 @@ describe('BaileysAdapter group management', () => {expect(fakeSock.groupParticipantsUpdate).toHaveBeenCalledWith('[email protected]', ['[email protected]'], action);});+ // A bare number is the documented convenience form on these routes, and the guard accepts it. It+ // must be qualified BEFORE the engine fold: `toEngineJid` only folds an already-domained user id,+ // so a bare number went out verbatim and Baileys' encoder wrote it as a packed nibble STRING+ // rather than a JID_PAIR — WhatsApp received an attribute that was not a JID at all.+ it.each([+ ['addParticipants', 'add'],+ ['removeParticipants', 'remove'],+ ['promoteParticipants', 'promote'],+ ['demoteParticipants', 'demote'],+ ])('%s qualifies a bare number before folding to the engine dialect', async (method, action) => {+ const adapter = await ready();+ await (adapter as unknown as Record<string, (g: string, p: string[]) => Promise<void>>)[method]('[email protected]', [+ '628111',+ ]);+ expect(fakeSock.groupParticipantsUpdate).toHaveBeenCalledWith('[email protected]', ['[email protected]'], action);+ });+it('participant ops pass @lid ids through unchanged (lid addressing mode)', async () => {const adapter = await ready();await adapter.addParticipants('[email protected]', ['111@lid']);@@ -3036,8 +3053,14 @@ describe('BaileysAdapter group management', () => {it('createGroup folds neutral @c.us participants to the engine dialect, keeping @lid raw', async () => {fakeSock.groupCreate.mockResolvedValue(META);const adapter = await ready();- await adapter.createGroup('G', ['[email protected]', '222@lid']);- expect(fakeSock.groupCreate).toHaveBeenCalledWith('G', ['[email protected]', '222@lid']);+ // The bare number belongs here too: it is the documented convenience form, and unqualified it+ // reaches the socket as a non-JID string.+ await adapter.createGroup('G', ['[email protected]', '222@lid', '628333']);+ expect(fakeSock.groupCreate).toHaveBeenCalledWith('G', [+ '[email protected]',+ '222@lid',+ '[email protected]',+ ]);});it('leaveGroup / setGroupSubject / setGroupDescription delegate to the socket', async () => {