Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): bound the read receipt, and leave the media send alone on purpose
Yudhi Armyndharis47c028e77ffb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e3efe6b11..bfa4bc5bb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- **Marking a chat read no longer answers a bare `500` when WhatsApp stays silent** — the read receipt reaches a library helper that destructures the query result, so an unanswered one threw a raw `TypeError` that nothing could classify; it now shares the same 30-second budget and answers `503`. The media send path fails the same way and is deliberately left alone: a send is not repeatable, and `503` is a status the SDKs retry, so making it one would trade an opaque failure for duplicate messages.- **Channel operations no longer answer a bare `500` when WhatsApp stays silent** — every one goes through a query helper that reports an absent answer as a malformed one, which the refusal classifier cannot place, so the raw error escaped with its message discarded; the channel lookup, invite lookup, subscribe, unsubscribe, delete and mute/unmute now share the same 30-second budget and answer `503`. Creating a channel is deliberately left unbounded for the same reason as creating a group — it is non-idempotent, and `503` is a status the Go SDK retries for `POST`.- **The three group-metadata reads no longer answer a bare `500` when WhatsApp stays silent** — `extractGroupMetadata` throws a Boom whose `data` is normalised to null, so the refusal classifier cannot place it and the raw error escaped with its message discarded; `GET /groups/{id}` and `GET /groups/join-info` now spend the same 30-second budget as the rest and answer `503`, and creating a group maps a genuine WhatsApp refusal to `403` instead of the same bare `500`. Group creation is deliberately left unbounded: it is the one non-idempotent call, and `503` is a status the Go SDK retries for `POST`, which could leave duplicate groups behind.- **A group list WhatsApp never answered is no longer served as "you are in no groups"** — `groupFetchAllParticipating` fills its result only behind an `if (groupsChild)`, so an unanswered query returns the same empty object an account with no groups produces, and `GET /sessions/{id}/groups` answered `200` with an empty array after a silent minute; there is nothing in the value to tell the two apart, so the read now shares the same 30-second budget as the group writes and answers `503` instead.diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.tsindex a86efa682..cb9b120bd 100644--- a/src/engine/adapters/baileys-contacts.ts+++ b/src/engine/adapters/baileys-contacts.ts@@ -139,7 +139,10 @@ export class BaileysContacts {if (!last) {return false; // nothing known to mark read}- await this.sock().readMessages([last.key]);+ // readMessages reaches fetchPrivacySettings, which destructures the query result and throws a+ // raw TypeError on an unanswered one — no Boom, so nothing downstream can classify it. Marking+ // a chat read is idempotent, so bounding it is safe: a repeat costs nothing.+ await this.confirmed(this.sock().readMessages([last.key]), 'the read receipt');return true;}diff --git a/src/engine/adapters/baileys-send-seen.spec.ts b/src/engine/adapters/baileys-send-seen.spec.tsnew file mode 100644index 000000000..992a4c658--- /dev/null+++ b/src/engine/adapters/baileys-send-seen.spec.ts@@ -0,0 +1,52 @@+import type { WASocket } from '@whiskeysockets/baileys';+import { BaileysContacts, BaileysContactsHost } from './baileys-contacts';+import { EngineTransportError } from '../../common/errors/engine-transport.error';++/**+ * `readMessages` reaches `fetchPrivacySettings`, whose body is+ * `const { content } = await query({...})`. On an unanswered query that destructure throws a raw+ * TypeError — no Boom, so nothing can classify it — and the caller gets a bare 500.+ *+ * Marking a chat read is idempotent in effect, so bounding it is safe: a repeat costs nothing.+ * The media send path is deliberately NOT bounded the same way — see the PR.+ */+const never = (): Promise<never> => new Promise<never>(() => undefined);++function contacts(sock: Record<string, jest.Mock>, budgetMs: number): BaileysContacts {+ const host = {+ ensureReady: () => undefined,+ getSocket: () => sock as unknown as WASocket,+ logger: { warn: jest.fn(), debug: jest.fn(), info: jest.fn(), error: jest.fn() },+ normalizedSelfJid: () => '[email protected]',+ listContacts: () => [],+ findContact: () => null,+ resolvePhone: () => null,+ listChats: () => [],+ lastMessage: () => ({ key: { id: 'M1', remoteJid: '[email protected]' }, timestamp: 1 }),+ toEngineJid: (j: string) => j,+ } as unknown as BaileysContactsHost;+ return new BaileysContacts(host, budgetMs);+}++describe('sendSeen', () => {+ it('reports an unanswered read receipt instead of a bare 500', async () => {+ await expect(contacts({ readMessages: jest.fn(never) }, 15).sendSeen('[email protected]')).rejects.toBeInstanceOf(+ EngineTransportError,+ );+ });++ it('still resolves when WhatsApp answers inside the budget', async () => {+ const readMessages = jest.fn().mockResolvedValue(undefined);+ await expect(contacts({ readMessages }, 500).sendSeen('[email protected]')).resolves.toBe(true);+ });++ it('still short-circuits when there is no last message to mark', async () => {+ const host = {+ ensureReady: () => undefined,+ getSocket: () => ({}) as unknown as WASocket,+ logger: { warn: jest.fn(), debug: jest.fn(), info: jest.fn(), error: jest.fn() },+ lastMessage: () => null,+ } as unknown as BaileysContactsHost;+ await expect(new BaileysContacts(host, 15).sendSeen('[email protected]')).resolves.toBe(false);+ });+});