From 047782e58594739b81af28206898349d040bed86 Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Fri, 17 Apr 2026 22:37:20 +0800 Subject: [PATCH] fix(room): handle reaction updates in onRoomMessage WS handler Root cause: publish_reaction_event sends a RoomMessageEvent (not reaction_added) with reactions field set. The onRoomMessage handler previously returned prev immediately when a duplicate ID was found, skipping the reaction update entirely. Fix: - Add reactions field to RoomMessagePayload so TypeScript knows it's there - When a duplicate message ID is found AND payload carries reactions, update the existing message's reactions field instead of ignoring - ReactionItem and ReactionGroup have identical shapes, so assignment works --- src/contexts/room-context.tsx | 16 ++++++++++++++-- src/lib/ws-protocol.ts | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/contexts/room-context.tsx b/src/contexts/room-context.tsx index a87c8ad..428155b 100644 --- a/src/contexts/room-context.tsx +++ b/src/contexts/room-context.tsx @@ -424,8 +424,20 @@ export function RoomProvider({ // Use ref to get current activeRoomId to avoid stale closure if (payload.room_id === activeRoomIdRef.current) { setMessages((prev) => { - // Deduplicate by both ID (for normal) and seq (for optimistic replacement) - if (prev.some((m) => m.id === payload.id)) { + // Check if this is a reaction-update event (same ID, different reactions). + // publish_reaction_event sends RoomMessageEvent with reactions field set. + const existingIdx = prev.findIndex((m) => m.id === payload.id); + if (existingIdx !== -1) { + // Message already exists — update reactions if provided. + // Reaction events have empty content/sender_type. + if (payload.reactions !== undefined) { + const updated = [...prev]; + updated[existingIdx] = { ...updated[existingIdx], reactions: payload.reactions }; + const msg = updated[existingIdx]; + saveMessage(msg).catch(() => {}); + return updated; + } + // Duplicate of a real message — ignore return prev; } // Also check if there's an optimistic message with the same seq that should be replaced diff --git a/src/lib/ws-protocol.ts b/src/lib/ws-protocol.ts index 89b6543..f04e533 100644 --- a/src/lib/ws-protocol.ts +++ b/src/lib/ws-protocol.ts @@ -166,6 +166,8 @@ export interface RoomMessagePayload { send_at: string; seq: number; display_name?: string; + /** Present when this event carries reaction updates for the message */ + reactions?: ReactionItem[]; } export interface ProjectEventPayload {