Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): report receipts only for group and status chats
Yudhi Armyndharis9879701d1d70
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.tsindex c938bd6bf..58bcedcf9 100644--- a/src/engine/adapters/baileys-events.spec.ts+++ b/src/engine/adapters/baileys-events.spec.ts@@ -704,6 +704,38 @@ describe('BaileysEvents.handleMessageReceiptUpdate', () => {expect(onMessageReceipt).not.toHaveBeenCalled();});+ it('reports the receipts of a status post as well as a group message', () => {+ const { events, onMessageReceipt } = setup();+ events.handleMessageReceiptUpdate([+ {+ key: { id: 'ST1', remoteJid: 'status@broadcast', fromMe: true },+ receipt: { userJid: member, readTimestamp: 5 },+ },+ ]);+ expect(onMessageReceipt.mock.calls).toEqual([+ [{ messageId: 'ST1', chatId: 'status@broadcast', participant: member, status: 'read', timestamp: 5 }],+ ]);+ });++ // message.receipt is documented for group and status messages only, which is the split Baileys itself makes+ // (1:1 receipts arrive as messages.update). A list the phone filled in reaches this handler through a+ // message's own userReceipt, whatever the chat, so the handler holds the line.+ it.each([+ ['a 1:1 chat', '[email protected]'],+ ['a 1:1 chat in the lid dialect', '55555@lid'],+ ['a broadcast list', '1700000000@broadcast'],+ ['a channel', '120363000000000002@newsletter'],+ ])('does not report a receipt for %s', (_kind, remoteJid) => {+ const { events, onMessageReceipt } = setup();+ events.handleMessageReceiptUpdate([+ {+ key: { id: 'OUT1', remoteJid, fromMe: true },+ receipt: { userJid: member, receiptTimestamp: 1, readTimestamp: 2 },+ },+ ]);+ expect(onMessageReceipt).not.toHaveBeenCalled();+ });+it('neutralises the chat and participant ids', () => {const { events, onMessageReceipt } = setup({ toNeutralJid: (jid: string) => `n:${jid}` });events.handleMessageReceiptUpdate([diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.tsindex 9479e5db8..a934ed7a0 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -56,7 +56,7 @@ import type { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter'import { type createLogger } from '../../common/services/logger.service';import { createSilentLogger } from './baileys-logger';import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';-import { parseWaId, userPart } from '../identity/wa-id';+import { chatKind, parseWaId, userPart, type ChatKind } from '../identity/wa-id';/*** Inbound event handling extracted from BaileysAdapter: the socket event handlers@@ -96,6 +96,12 @@ const PRESENCE_STATES: ReadonlySet<PresenceState> = new Set<PresenceState>(['paused',]);+/**+ * The chats a per-recipient receipt is reported for: the two Baileys raises message-receipt.update for, and the+ * only ones message.receipt is documented for. A 1:1 chat reports through messages.update instead.+ */+const RECEIPT_CHAT_KINDS: ReadonlySet<ChatKind> = new Set<ChatKind>(['group', 'status']);+/*** Top-level Message keys that carry no user content. A live message made only of these is dropped;* messageContextInfo rides along on real content too, so on its own it is not enough to drop.@@ -1394,10 +1400,11 @@ export class BaileysEvents {/*** Baileys `message-receipt.update`: per-participant receipts, which Baileys raises only for group* and status messages (1:1 receipts arrive as `messages.update` instead). Only receipts for- * messages this account sent are reported, and receipts from the account's own linked devices are- * dropped. The event buffer can merge several receipts of one participant into one entry, so each- * timestamp the entry carries becomes its own event. rc14 files a `played` receipt under- * `readTimestamp`, so on that version a voice note that was played reports `read`.+ * messages this account sent are reported, and only for those two chat kinds whichever way the list+ * reached this handler; receipts from the account's own linked devices are dropped. The event+ * buffer can merge several receipts of one participant into one entry, so each timestamp the entry+ * carries becomes its own event. rc14 files a `played` receipt under `readTimestamp`, so on that+ * version a voice note that was played reports `read`.*/handleMessageReceiptUpdate(updates: Array<{@@ -1416,7 +1423,14 @@ export class BaileysEvents {}const isSelf = this.selfMatcher();for (const { key, receipt } of Array.isArray(updates) ? updates : []) {- if (!key?.id || !key.remoteJid || key.fromMe !== true || !receipt?.userJid || isSelf(receipt.userJid)) {+ if (+ !key?.id ||+ !key.remoteJid ||+ key.fromMe !== true ||+ !RECEIPT_CHAT_KINDS.has(chatKind(key.remoteJid)) ||+ !receipt?.userJid ||+ isSelf(receipt.userJid)+ ) {continue;}const stamps: Array<[MessageReceiptStatus, typeof receipt.receiptTimestamp]> = [