Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): map a server refusal on participant updates
Yudhi Armyndharis44a6c3d00c64
diff --git a/src/engine/adapters/baileys-groups.ts b/src/engine/adapters/baileys-groups.tsindex 6b2cca1ac..855620f01 100644--- a/src/engine/adapters/baileys-groups.ts+++ b/src/engine/adapters/baileys-groups.ts@@ -198,6 +198,13 @@ export class BaileysGroups {* every not-admin/not-registered/already-member refusal into a reported success. Map the entries* verbatim; THROW only when the operation failed for every requested participant (a refusal of* the operation itself → HTTP 403) or the server returned no outcome at all.+ *+ * The per-participant array is not the only refusal channel: WhatsApp can reject the IQ itself,+ * and `assertNodeErrorFree` then throws with the WA code on `data`. Every other write in this file+ * routes through {@link mapServerRefusal} for exactly that; this one did not, so a batch-level+ * refusal escaped as an unhandled error (HTTP 500) instead of the 403 its siblings give. The+ * deadline stays INSIDE the mapping so a timeout is still reported as a timeout — `refusedStatusCode`+ * only classifies a numeric `data`, so a transport Boom passes through untouched.*/private async runParticipantsUpdate(groupId: string,@@ -207,10 +214,12 @@ export class BaileysGroups {this.host.ensureReady();// An unanswered query yields [], which the empty-results guard below would report as a refusal// — a dead transport sold to the caller as a permissions problem.- const raw = await withQueryDeadline(- this.sock().groupParticipantsUpdate(groupId, this.toEngineParticipants(participants), action),- this.queryBudgetMs,- `WhatsApp did not answer the participant ${action} in time`,+ const raw = await mapServerRefusal(`The participant ${action}`, () =>+ withQueryDeadline(+ this.sock().groupParticipantsUpdate(groupId, this.toEngineParticipants(participants), action),+ this.queryBudgetMs,+ `WhatsApp did not answer the participant ${action} in time`,+ ),);const results: ParticipantOperationResult[] = (raw ?? []).map(entry => ({id: entry.jid ? this.host.toNeutralJid(entry.jid) : '',diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 6d43fb743..384dc7e95 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -3224,6 +3224,43 @@ describe('BaileysAdapter group management', () => {);});+ it.each([['addParticipants'], ['removeParticipants'], ['promoteParticipants'], ['demoteParticipants']])(+ '%s maps a batch-level server refusal to 403 rather than letting a raw Boom escape',+ async method => {+ // The per-participant array is the usual refusal channel, but WhatsApp can also reject the IQ+ // itself — assertNodeErrorFree then throws with the WA code on `data`, and without a mapping+ // that reaches the caller as an unhandled 500. Every other write in this adapter maps it.+ fakeSock.groupParticipantsUpdate.mockRejectedValueOnce(Object.assign(new Error('not-authorized'), { data: 403 }));+ const adapter = await ready();+ const err = await (adapter as unknown as Record<string, (g: string, p: string[]) => Promise<unknown>>)+ [method]('[email protected]', ['[email protected]'])+ .catch((e: unknown) => e);+ expect(err).toBeInstanceOf(EngineRefusedError);+ },+ );++ it('rethrows a transport failure on a participant update instead of calling it a refusal', async () => {+ // A dead socket carries a DisconnectReason-shaped statusCode but no numeric `data`; folding it+ // into a refusal would report "admin rights may be missing" for a connection that simply died.+ const connectionClosed = new Boom('Connection Closed', { statusCode: 428 });+ fakeSock.groupParticipantsUpdate.mockRejectedValueOnce(connectionClosed);+ const adapter = await ready();+ const err = await adapter.removeParticipants('[email protected]', ['[email protected]']).catch((e: unknown) => e);+ expect(err).toBe(connectionClosed);+ });++ it('keeps an unanswered participant update a 503, not a 403', async () => {+ // The query deadline sits INSIDE the refusal mapping, so the timeout's own error travels through+ // mapServerRefusal on its way out. It must arrive unchanged: an unanswered write is a transport+ // failure, and reporting it as "admin rights may be missing" sends operators to the wrong layer.+ const unanswered = new EngineTransportError('WhatsApp did not answer the participant remove in time');+ fakeSock.groupParticipantsUpdate.mockRejectedValueOnce(unanswered);+ const adapter = await ready();+ const err = await adapter.removeParticipants('[email protected]', ['[email protected]']).catch((e: unknown) => e);+ expect(err).toBe(unanswered);+ expect(err).not.toBeInstanceOf(EngineRefusedError);+ });+it.each([['setGroupMessagesAdminsOnly', true, 'announcement'],['setGroupMessagesAdminsOnly', false, 'not_announcement'],