Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(channel): answer 404 for a non-channel id on mute
Yudhi Armyndharis72b835b91ef9
diff --git a/src/modules/channel/channel.service.spec.ts b/src/modules/channel/channel.service.spec.tsindex ee5ec6942..ae5cd684f 100644--- a/src/modules/channel/channel.service.spec.ts+++ b/src/modules/channel/channel.service.spec.ts@@ -124,6 +124,21 @@ describe('ChannelService', () => {},);+ // Baileys has no adapter guard on mute, so without this the same id answered 404 on one engine+ // and 403 or 500 on the other.+ it('muteChannel refuses an id that is not a channel with 404 and never reaches the engine', () => {+ const muteChannel = jest.fn().mockResolvedValue(undefined);+ const svc = makeService({ muteChannel });+ expect(() => svc.muteChannel('s1', '[email protected]', true)).toThrow(ChannelNotFoundError);+ expect(muteChannel).not.toHaveBeenCalled();+ });++ it('muteChannel forwards a channel id', async () => {+ const muteChannel = jest.fn().mockResolvedValue(undefined);+ await makeService({ muteChannel }).muteChannel('s1', '120363000000000000@newsletter', false);+ expect(muteChannel).toHaveBeenCalledWith('120363000000000000@newsletter', false);+ });+it.each(['deleteChannel', 'unsubscribeFromChannel'] as const)('%s forwards a channel id', async method => {const engineCall = jest.fn().mockResolvedValue(undefined);await makeService({ [method]: engineCall })[method]('s1', '120363000000000000@newsletter');diff --git a/src/modules/channel/channel.service.ts b/src/modules/channel/channel.service.tsindex 196077ee9..12ac28746 100644--- a/src/modules/channel/channel.service.ts+++ b/src/modules/channel/channel.service.ts@@ -50,8 +50,9 @@ export class ChannelService {/*** A channel id, or 404. whatsapp-web.js resolves any other id to an ordinary chat, creating one if- * needed, before its channel delete or unsubscribe fails with a 500 (the adapter's mute guards the- * same way). Every channel id ends in `@newsletter` on both engines.+ * needed, before its channel delete or unsubscribe fails with a 500, and Baileys hands it to WhatsApp,+ * which refuses it with a 403 or an opaque failure. Every channel id ends in `@newsletter` on both+ * engines.*/private requireChannelId(channelId: string): string {if (!channelId.endsWith('@newsletter')) throw new ChannelNotFoundError(channelId);@@ -66,7 +67,8 @@ export class ChannelService {/** Mute or unmute a channel's notifications. Subscription is untouched either way. */muteChannel(sessionId: string, channelId: string, mute: boolean) {- return this.getEngine(sessionId).muteChannel(channelId, mute);+ const engine = this.getEngine(sessionId);+ return engine.muteChannel(this.requireChannelId(channelId), mute);}/**