Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): read edits, revokes and reactions from unwrapped content
Yudhi Armyndharis2a4f9600f612
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.tsindex ae38b0e02..c32404fab 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -252,14 +252,15 @@ export class BaileysEvents {// A live disappearing message (also viewOnce / documentWithCaption / edited) arrives wrapped, so the// raw `getContentType` returns the OUTER wrapper key (e.g. 'ephemeralMessage') and downstream type/// body/media/location detection would miss the real inner content. Normalize ONCE so the true inner- // type drives routing here AND mapMessage. normalizeMessageContent leaves protocolMessage and- // reactionMessage untouched, so the early-return branches below still match.+ // type drives routing here AND mapMessage. The protocol and reaction branches below read the+ // normalized content too: a client wraps an edit in `editedMessage`, and `ephemeralMessage` can hold+ // a revoke or a reaction just as well, so the raw root does not always carry them.const normalizedRoot = b.normalizeMessageContent(msg.message ?? undefined) ?? msg.message ?? undefined;const contentType = b.getContentType(normalizedRoot);// --- protocolMessage REVOKE: don't emit onMessage ---if (contentType === 'protocolMessage') {- const pm = msg.message?.protocolMessage;+ const pm = normalizedRoot?.protocolMessage;if (pm?.type === b.proto.Message.ProtocolMessage.Type.REVOKE) {const from = msg.key.fromMe === true ? this.host.normalizedSelfJid() : remoteJid;const to = msg.key.fromMe === true ? remoteJid : this.host.normalizedSelfJid();@@ -327,7 +328,7 @@ export class BaileysEvents {// --- reactionMessage: don't emit onMessage ---if (contentType === 'reactionMessage') {- const rm = msg.message?.reactionMessage;+ const rm = normalizedRoot?.reactionMessage;const event: ReactionEvent = {messageId: rm?.key?.id ?? '',chatId: this.host.toNeutralJid(remoteJid),diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex d9959a02b..f6034cf2a 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -3124,6 +3124,54 @@ describe('BaileysAdapter inbound fan-out', () => {);});+ it('reads an edit, a revoke and a reaction that arrive inside a wrapper from the unwrapped content', async () => {+ baileys.getContentType.mockImplementation(realGetContentType);+ baileys.normalizeMessageContent.mockImplementation(+ (m?: { editedMessage?: { message?: unknown }; ephemeralMessage?: { message?: unknown } }) =>+ m?.editedMessage?.message ?? m?.ephemeralMessage?.message ?? m,+ );+ const onMessageEdited = jest.fn();+ const onMessageRevoked = jest.fn();+ const onMessageReaction = jest.fn();+ const adapter = newAdapter();+ await adapter.initialize({ onMessageEdited, onMessageRevoked, onMessageReaction });+ const key = (id: string) => ({ remoteJid: '[email protected]', fromMe: false, id });+ fakeSock.fire('messages.upsert', {+ type: 'notify',+ messages: [+ {+ key: key('WRAPPED_EDIT'),+ message: {+ editedMessage: {+ message: {+ protocolMessage: { key: { id: 'EDITED_ID' }, type: 14, editedMessage: { conversation: 'fixed' } },+ },+ },+ },+ messageTimestamp: 1700000050,+ },+ {+ key: key('WRAPPED_REVOKE'),+ message: { ephemeralMessage: { message: { protocolMessage: { key: { id: 'REVOKED_ID' }, type: 0 } } } },+ messageTimestamp: 1700000051,+ },+ {+ key: key('WRAPPED_REACTION'),+ message: { ephemeralMessage: { message: { reactionMessage: { key: { id: 'REACTED_ID' }, text: 'ok' } } } },+ messageTimestamp: 1700000052,+ },+ ],+ });+ await new Promise(r => setImmediate(r));+ await new Promise(r => setImmediate(r));++ expect(firstEditedMessage(onMessageEdited)).toMatchObject({ messageId: 'EDITED_ID', body: 'fixed' });+ expect(onMessageRevoked).toHaveBeenCalledWith(expect.objectContaining({ id: 'REVOKED_ID' }));+ expect(onMessageReaction).toHaveBeenCalledWith(+ expect.objectContaining({ messageId: 'REACTED_ID', reaction: 'ok' }),+ );+ });+it('reactionMessage: fires onMessageReaction and NOT onMessage', async () => {// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertionconst baileys = jest.requireMock('@whiskeysockets/baileys') as { getContentType: jest.Mock };