Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): answer 409 when a Baileys status send loses its socket
Yudhi Armyndharis795942997d2b
diff --git a/src/engine/adapters/baileys-status.ts b/src/engine/adapters/baileys-status.tsindex a23741d92..7a81046dc 100644--- a/src/engine/adapters/baileys-status.ts+++ b/src/engine/adapters/baileys-status.ts@@ -3,6 +3,7 @@ import { MediaInput, StatusPostOptions, StatusResult } from '../interfaces/whatsimport { BadRequestException } from '@nestjs/common';import { resolveMediaBuffer } from './baileys-messaging';import { EngineRefusedError } from '../../common/errors/engine-refused.error';+import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';/** How long WhatsApp keeps a status up. */const STATUS_TTL_MS = 24 * 3_600_000;@@ -18,6 +19,8 @@ export interface BaileysStatusHost {ensureReady(): void;/** Post-ensureReady socket handle — call host.ensureReady() first. */getSocket(): WASocket;+ /** The live socket or null, read without a readiness check: null once a stop or logout tore it down. */+ getSocketOrNull(): WASocket | null;toEngineJid(jid: string): string;normalizedSelfJid(): string;/** Baileys timestamps are `number | Long`; normalize to unix seconds. */@@ -37,6 +40,23 @@ export class BaileysStatus {return this.host.getSocket();}+ /**+ * Send to `status@broadcast`. A send that fails after a stop or logout has torn its socket down is+ * not ready (409), as a chat send is; a failure on a socket still in place propagates.+ */+ private async sendStatus(+ content: AnyMessageContent,+ options: Parameters<WASocket['sendMessage']>[2],+ ): Promise<WAMessage | undefined> {+ const sock = this.sock();+ try {+ return await sock.sendMessage('status@broadcast', content, options);+ } catch (error) {+ if (this.host.getSocketOrNull() !== sock) throw new EngineNotReadyError();+ throw error;+ }+ }+postTextStatus(text: string, options: StatusPostOptions): Promise<StatusResult> {// `linkPreview: null` is Baileys' explicit "no preview": with the key absent it runs its own// generator (link-preview-js, unfixed SSRF advisory) on any URL in the status text.@@ -96,8 +116,7 @@ export class BaileysStatus {'unknown and the revoke cannot be addressed to them',);}- const sent = await this.sock().sendMessage(- 'status@broadcast',+ const sent = await this.sendStatus({delete: {remoteJid: 'status@broadcast',@@ -126,7 +145,7 @@ export class BaileysStatus {throw new BadRequestException('recipients is required to post a status on the Baileys engine');}const statusJidList = options.recipients.map(r => this.host.toEngineJid(r));- const sent = await this.sock().sendMessage('status@broadcast', content, {+ const sent = await this.sendStatus(content, {statusJidList,backgroundColor: options.backgroundColor,font: options.font,diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 4d2a18ded..c3834433d 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -6990,6 +6990,30 @@ describe('BaileysAdapter status posting', () => {);expect(fakeStore.getMessage).not.toHaveBeenCalled();});++ // A status send torn down by a stop or logout while the library is still writing it answers 409,+ // as a chat send does, rather than a raw Connection Closed 500 that the send breaker would count.+ it('a status post or revoke whose socket is torn down in flight reads as not ready', async () => {+ fakeSock.sendMessage.mockResolvedValueOnce({ key: { id: 'STATUS1' } });+ const adapter = await ready();+ await adapter.postTextStatus('hello', { recipients: ['[email protected]'] });+ fakeSock.sendMessage.mockImplementation(() => {+ (adapter as unknown as { sock: unknown }).sock = null;+ return Promise.reject(new Error('Connection Closed'));+ });+ await expect(adapter.deleteStatus('STATUS1')).rejects.toBeInstanceOf(EngineNotReadyError);+ (adapter as unknown as { sock: unknown }).sock = fakeSock;+ await expect(adapter.postTextStatus('hello', { recipients: ['[email protected]'] })).rejects.toBeInstanceOf(+ EngineNotReadyError,+ );+ });++ it('a status post that fails on a socket still in place rethrows the failure as is', async () => {+ const adapter = await ready();+ const failure = new Error('not-acceptable');+ fakeSock.sendMessage.mockRejectedValue(failure);+ await expect(adapter.postTextStatus('hello', { recipients: ['[email protected]'] })).rejects.toBe(failure);+ });});describe('BaileysAdapter proxy support', () => {