Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): key presence.update on the participants that changed

Yudhi Armyndharisde2fac6f0c71
diff --git a/src/modules/webhook/utils/idempotency.util.spec.ts b/src/modules/webhook/utils/idempotency.util.spec.ts
index 41b1f7def..e3533c9fe 100644
--- a/src/modules/webhook/utils/idempotency.util.spec.ts
+++ b/src/modules/webhook/utils/idempotency.util.spec.ts
@@ -276,6 +276,18 @@ describe('Idempotency Utils', () => {
);
});
+ it('keys presence.update on WHAT changed, salted per occurrence', () => {
+ const at = '2026-07-20T00:00:00.000Z';
+ const data = { sessionId: 'A', chatId: '[email protected]', participants: [{ id: 'u1@lid', state: 'composing' }] };
+ const a = generateIdempotencyKey('presence.update', data, at);
+ expect(a).toMatch(/^pres_A_123@g\.us_[a-f0-9]{12}_2026-07-20T00:00:00\.000Z$/);
+ expect(generateIdempotencyKey('presence.update', data, at)).toBe(a);
+ expect(generateIdempotencyKey('presence.update', data, '2026-07-20T00:05:00.000Z')).not.toBe(a);
+ // Two participants changing in the same millisecond are two events, not one.
+ const other = { ...data, participants: [{ id: 'u2@lid', state: 'available' }] };
+ expect(generateIdempotencyKey('presence.update', other, at)).not.toBe(a);
+ });
+
it('keys call.received on the session + call id (unique per call, no occurrence salt needed)', () => {
const at = '2026-07-20T00:00:00.000Z';
const data = { sessionId: 'A', callId: 'CALL1', from: '[email protected]', timestamp: 1782000000 };
diff --git a/src/modules/webhook/utils/idempotency.util.ts b/src/modules/webhook/utils/idempotency.util.ts
index d3771c933..82c3e4ddf 100644
--- a/src/modules/webhook/utils/idempotency.util.ts
+++ b/src/modules/webhook/utils/idempotency.util.ts
@@ -113,8 +113,10 @@ export function generateIdempotencyKey(event: string, data: Record<string, unkno
// Keyed on the chat and salted per occurrence. Only genuine state CHANGES are dispatched, and
// a contact who types, stops, and types again produces the same payload each time — content
// hashing would collapse that back into one delivery and hide the very transitions the event
- // exists to report.
- return `pres_${toStr(data.sessionId)}_${toStr(data.chatId)}${occurrence}`;
+ // exists to report. The participants are hashed in as well: two group members changing state
+ // in the same millisecond are two events and must not share a key. Pre-stringified for the
+ // same reason as group.update below.
+ return `pres_${toStr(data.sessionId)}_${toStr(data.chatId)}_${hashData({ participants: JSON.stringify(data.participants ?? null) })}${occurrence}`;
case 'group.join':
// A membership change carries no unique id and repeats with identical content (the same user