From 705bd177e8c829a71fc474a0a01752ba84a07425 Mon Sep 17 00:00:00 2001 From: maaz519 Date: Wed, 27 May 2026 17:03:47 +0530 Subject: [PATCH] fix(worker): reactorJid null guard, document reaction removal, restore comments --- apps/worker/src/whatsapp/normalizer.test.ts | 17 +++++++++++++++++ apps/worker/src/whatsapp/normalizer.ts | 11 ++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/apps/worker/src/whatsapp/normalizer.test.ts b/apps/worker/src/whatsapp/normalizer.test.ts index fc221d5..cd195ab 100644 --- a/apps/worker/src/whatsapp/normalizer.test.ts +++ b/apps/worker/src/whatsapp/normalizer.test.ts @@ -140,4 +140,21 @@ describe('normalizeReaction', () => { } as proto.IWebMessageInfo; expect(normalizeReaction(msg, 'acc_1')).toBeNull(); }); + + it('returns null when key is missing', () => { + const result = normalizeReaction({ message: { reactionMessage: { key: { id: 'T' }, text: '⭐' } } } as proto.IWebMessageInfo, 'acc_1'); + expect(result).toBeNull(); + }); + + it('returns null when participant (reactorJid) is missing', () => { + const msg = { + key: { + remoteJid: '120363043312345678@g.us', + fromMe: false, + id: 'ID', + }, + message: { reactionMessage: { key: { id: 'TARGET' }, text: '⭐' } }, + } as proto.IWebMessageInfo; + expect(normalizeReaction(msg, 'acc_1')).toBeNull(); + }); }); diff --git a/apps/worker/src/whatsapp/normalizer.ts b/apps/worker/src/whatsapp/normalizer.ts index 3f10206..902923d 100644 --- a/apps/worker/src/whatsapp/normalizer.ts +++ b/apps/worker/src/whatsapp/normalizer.ts @@ -22,7 +22,9 @@ export function normalizeMessage( if (!key) return null; const remoteJid = key.remoteJid ?? ''; + // only group messages (group JIDs end with @g.us) if (!remoteJid.endsWith('@g.us')) return null; + // skip our own outgoing messages if (key.fromMe) return null; if (!msg.message) return null; @@ -49,7 +51,9 @@ export function normalizeReaction( if (!key) return null; const remoteJid = key.remoteJid ?? ''; + // only group messages (group JIDs end with @g.us) if (!remoteJid.endsWith('@g.us')) return null; + // skip our own outgoing messages if (key.fromMe) return null; const reaction = msg.message?.reactionMessage; @@ -58,10 +62,15 @@ export function normalizeReaction( const targetMsgId = reaction.key?.id; if (!targetMsgId) return null; + // Ensure reactorJid is not empty (group message must have a participant) + const reactorJid = key.participant; + if (!reactorJid) return null; + return { - reactorJid: key.participant ?? '', + reactorJid, targetMsgId, sourceGroupJid: remoteJid, + // emoji is empty string ('') when reaction was removed (retraction) emoji: reaction.text ?? '', accountId, };