Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): carry an absent reaction snapshot through the socket layer

Yudhi Armyndharis309215418ae6
diff --git a/dashboard/src/hooks/useWebSocket.ts b/dashboard/src/hooks/useWebSocket.ts
index baca6afae..e2813c090 100644
--- a/dashboard/src/hooks/useWebSocket.ts
+++ b/dashboard/src/hooks/useWebSocket.ts
@@ -37,7 +37,13 @@ interface MessageReactionEvent {
chatId: string;
reaction: string;
senderId: string;
- reactions: Record<string, string>;
+ /**
+ * The post-apply snapshot of every reaction on the message. ABSENT when the gateway holds no
+ * stored copy of the message to compute it from, which it says by omitting the key rather than
+ * sending an empty object — absent means "unknown", `{}` means "there are none left". Keep it
+ * optional: normalising the absence away here is what makes a consumer's own fallback dead code.
+ */
+ reactions?: Record<string, string>;
timestamp: string;
}
@@ -299,7 +305,9 @@ export function useWebSocket(events: WebSocketEvents = {}) {
chatId: String(data.chatId),
reaction: String(data.reaction),
senderId: String(data.senderId),
- reactions: (data.reactions as Record<string, string>) || {},
+ // Carried through as-is, including absent: `|| {}` here would tell every consumer that
+ // the message has no reactions left, which is a different claim from "we do not know".
+ reactions: data.reactions as Record<string, string> | undefined,
timestamp: msg.timestamp,
});
break;
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index 9408a6d16..0d0c75782 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -23,6 +23,7 @@ import {
import {
applyMessageEdit,
mergeDeliveryStatus,
+ mergeReactionSnapshot,
findRevokedIndex,
getMediaSrc,
type ChatMessageView,
@@ -423,11 +424,8 @@ export function Chats() {
// so we must read the prior message and deep-merge — `updateMessage`'s shallow merge would clobber
// the rest of metadata.
//
- // `reactions` is absent when the gateway holds no stored copy of the message to snapshot from (an
- // ephemeral message, or one that predates the session going live). Absent means "unknown", not
- // "none", so the known map has to survive — overwriting it would drop the optimistic reaction the
- // user just added under the local `me` key. Folding in the single reported change instead would
- // double-count that same reaction under its real sender id, so we keep what we have.
+ // The absent-vs-empty distinction on `reactions` is mergeReactionSnapshot's job; it is a named
+ // function so the behaviour is covered by a test, because nothing here is.
const caches = queryClient.getQueriesData<ChatMessageView[]>({
queryKey: ['messages', event.sessionId],
});
@@ -439,7 +437,10 @@ export function Chats() {
const next = list.slice();
next[idx] = {
...target,
- metadata: { ...(target.metadata || {}), reactions: event.reactions ?? target.metadata?.reactions },
+ metadata: {
+ ...(target.metadata || {}),
+ reactions: mergeReactionSnapshot(target.metadata?.reactions, event.reactions),
+ },
};
queryClient.setQueryData(key, next);
}
diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.ts
index 49b541bc9..d7ddaa60c 100644
--- a/dashboard/src/utils/chatMessages.test.ts
+++ b/dashboard/src/utils/chatMessages.test.ts
@@ -1,6 +1,11 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { mapEngineHistoryMessage, mergeChatMessages, type EngineHistoryMessage } from './chatMessages.ts';
+import {
+ mapEngineHistoryMessage,
+ mergeChatMessages,
+ mergeReactionSnapshot,
+ type EngineHistoryMessage,
+} from './chatMessages.ts';
import type { ChatMessage } from '../services/api';
const hist = (over: Partial<EngineHistoryMessage> = {}): EngineHistoryMessage => ({
@@ -387,3 +392,26 @@ test('mergeChatMessages enforces the payload cap on the initial load', () => {
assert.equal(merged[1].metadata?.media?.omitted, true);
assert.equal(merged[2].metadata?.media?.data, 'DB_2'); // newest MEDIA_PAYLOAD_CACHE_LIMIT survive
});
+
+// A `message.reaction` frame omits `reactions` when the gateway holds no stored copy of the message
+// to snapshot from. Absent means "unknown", not "there are none" — and the difference is visible:
+// the local user's own optimistic reaction lives under the `me` key in exactly that map.
+test('mergeReactionSnapshot keeps the known map when the event carries no snapshot', () => {
+ assert.deepEqual(mergeReactionSnapshot({ me: '👍' }, undefined), { me: '👍' });
+});
+
+test('mergeReactionSnapshot takes the snapshot when the event carries one', () => {
+ assert.deepEqual(mergeReactionSnapshot({ me: '👍' }, { '[email protected]': '❤️' }), { '[email protected]': '❤️' });
+});
+
+test('mergeReactionSnapshot treats an EMPTY snapshot as an answer, not as absence', () => {
+ // The last reaction being withdrawn is a real state the gateway reports as `{}`, and it must clear
+ // the badge rather than fall back to the stale map. Note `{}` is truthy, so `||` and `??` agree
+ // here — the absent-vs-empty distinction is destroyed one layer up if the socket mapper coerces
+ // an absent key with `|| {}`, which is exactly the defect this function was extracted to expose.
+ assert.deepEqual(mergeReactionSnapshot({ me: '👍' }, {}), {});
+});
+
+test('mergeReactionSnapshot stays undefined when neither side knows anything', () => {
+ assert.equal(mergeReactionSnapshot(undefined, undefined), undefined);
+});
diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.ts
index 7127271c9..2e9e1d0a3 100644
--- a/dashboard/src/utils/chatMessages.ts
+++ b/dashboard/src/utils/chatMessages.ts
@@ -148,6 +148,24 @@ export function mergeDeliveryStatus(
return DELIVERY_RANK[incoming] >= DELIVERY_RANK[current] ? incoming : current;
}
+/**
+ * The reaction map to store after a `message.reaction` event.
+ *
+ * The gateway OMITS `reactions` when it holds no stored copy of the message to snapshot from — an
+ * ephemeral message, or one that predates the session going live. Absent means "unknown", so the map
+ * already on screen survives, including the optimistic reaction the local user just added under the
+ * `me` key. An empty object is a different claim: every reaction was withdrawn, and that must clear
+ * the badge. `??` draws that line where `||` would not, which is the whole reason this is a named
+ * function rather than an inline expression — the socket layer carries the absence through
+ * deliberately (useWebSocket.ts) and flattening it anywhere in between makes this dead code.
+ */
+export function mergeReactionSnapshot(
+ existing: Record<string, string> | undefined,
+ incoming: Record<string, string> | undefined,
+): Record<string, string> | undefined {
+ return incoming ?? existing;
+}
+
/**
* Merge two metadata bags field-by-field. The incoming copy wins per field only when it actually
* carries a value — a live `message.sent` echo is built as `{media, quotedMessage, call}` with