Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(wwebjs): key a self-revoked message's chatId off fromMe
Yudhi Armyndharis2862cd5ccef4
diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.tsindex b22877387..3c7ee8c13 100644--- a/src/engine/adapters/wwebjs-message-events.ts+++ b/src/engine/adapters/wwebjs-message-events.ts@@ -143,7 +143,6 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHoclient.on('message_revoke_everyone', (after, before) => {try {- const selfWid = host.getSelfWid();// Emit structured data only; the engine layer never produces a localized// display string. The dashboard renders the localized "message deleted" text.//@@ -162,7 +161,9 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHoconst payload: RevokedMessage = {id: afterId?._serialized ?? afterId?.$1 ?? '',revokedId: beforeId?._serialized ?? beforeId?.$1,- chatId: after.from === selfWid ? after.to : after.from,+ // Direction flag, as Message._getChatId: an own message in a LID chat carries the own LID as+ // `from`, which never equals the phone-dialect account wid.+ chatId: after.fromMe ? after.to : after.from,from: after.from,to: after.to,type: 'revoked',diff --git a/src/engine/adapters/wwebjs-revoke-chat-id.spec.ts b/src/engine/adapters/wwebjs-revoke-chat-id.spec.tsnew file mode 100644index 000000000..bfe9a1e3a--- /dev/null+++ b/src/engine/adapters/wwebjs-revoke-chat-id.spec.ts@@ -0,0 +1,49 @@+import { EventEmitter } from 'events';+import type { Client } from 'whatsapp-web.js';+import { createLogger } from '../../common/services/logger.service';+import { type WwebjsEngineHost } from './wwebjs-host';+import { registerWwebjsMessageEvents } from './wwebjs-message-events';++/**+ * whatsapp-web.js stamps an own message in a LID chat (and in a LID-addressing-mode group) with+ * `from` = the account's own LID, while the account wid is the phone dialect. The chat of a revoked+ * message therefore follows the direction flag, as `Message._getChatId` does, not a comparison of+ * `from` against the account wid.+ */+function wire(): { client: EventEmitter; onMessageRevoked: jest.Mock } {+ const client = new EventEmitter();+ const onMessageRevoked = jest.fn();+ const host = {+ logger: createLogger('wwebjs-revoke-chat-id.spec'),+ getSelfWid: () => '[email protected]',+ getCallbacks: () => ({ onMessageRevoked }),+ } as unknown as WwebjsEngineHost;+ registerWwebjsMessageEvents(client as unknown as Client, host);+ return { client, onMessageRevoked };+}++describe('wwebjs message_revoke_everyone chatId', () => {+ it('reports the peer when the account revokes its own message in a LID chat', () => {+ const { client, onMessageRevoked } = wire();+ client.emit('message_revoke_everyone', {+ id: { _serialized: 'R', fromMe: true },+ fromMe: true,+ from: '777000@lid',+ to: '99999@lid',+ timestamp: 1,+ });+ expect(onMessageRevoked).toHaveBeenCalledWith(expect.objectContaining({ chatId: '99999@lid' }));+ });++ it('reports the group when a member revokes a message there', () => {+ const { client, onMessageRevoked } = wire();+ client.emit('message_revoke_everyone', {+ id: { _serialized: 'R2', fromMe: false },+ fromMe: false,+ from: '[email protected]',+ to: '[email protected]',+ timestamp: 2,+ });+ expect(onMessageRevoked).toHaveBeenCalledWith(expect.objectContaining({ chatId: '[email protected]' }));+ });+});