Skip to content

Commit 04014d1

Browse files
author
npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta
committed
Hydrate reactions for rendered messages
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
1 parent 40070a5 commit 04014d1

12 files changed

Lines changed: 909 additions & 26 deletions

‎desktop/src/features/channels/ui/ChannelPane.tsx‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ import {
5050
import type { ChannelAgentSessionAgent } from "@/features/channels/ui/useChannelAgentSessions";
5151
import { Button } from "@/shared/ui/button";
5252
import type { useChannelFind } from "@/features/search/useChannelFind";
53-
import type { MainTimelineEntry } from "@/features/messages/lib/threadPanel";
53+
import {
54+
buildMainTimelineEntries,
55+
type MainTimelineEntry,
56+
} from "@/features/messages/lib/threadPanel";
57+
import { useRenderScopedReactionHydration } from "@/features/messages/lib/useRenderScopedReactionHydration";
5458
import type { TimelineMessage } from "@/features/messages/types";
5559
import type { UserProfileLookup } from "@/features/profile/lib/identity";
5660
import { isWelcomeChannel } from "@/features/onboarding/welcome";
@@ -563,6 +567,16 @@ export const ChannelPane = React.memo(function ChannelPane({
563567

564568
return messages.filter((message) => !isWelcomeSetupSystemMessage(message));
565569
}, [activeChannel, messages]);
570+
const mainTimelineEntries = React.useMemo(
571+
() => buildMainTimelineEntries(visibleMessages),
572+
[visibleMessages],
573+
);
574+
useRenderScopedReactionHydration({
575+
activeChannel,
576+
mainTimelineEntries,
577+
threadHeadMessage,
578+
threadMessages,
579+
});
566580
const videoReviewCommentsByRootId = React.useMemo(
567581
() => buildVideoReviewCommentsByRootId(messages),
568582
[messages],
@@ -677,6 +691,7 @@ export const ChannelPane = React.memo(function ChannelPane({
677691
: "No channel selected"
678692
}
679693
isLoading={isTimelineLoading}
694+
mainEntries={mainTimelineEntries}
680695
messages={visibleMessages}
681696
firstUnreadMessageId={firstUnreadMessageId}
682697
unreadCount={unreadCount}

‎desktop/src/features/messages/lib/auxBackfill.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
sortMessages,
66
} from "@/features/messages/lib/messageQueryKeys";
77
import { relayClient } from "@/shared/api/relayClient";
8+
import { buildChannelStructuralAuxFilter } from "@/shared/api/relayChannelFilters";
89
import type { RelayEvent } from "@/shared/api/types";
910
import {
1011
CHANNEL_TIMELINE_CONTENT_KINDS,
@@ -65,16 +66,16 @@ export async function mergeAuxEventsWithDeletionBackfill(input: {
6566
}
6667

6768
/**
68-
* After a content-kinds-only history fetch, pull the auxiliary events
69-
* (reactions, edits, deletions) that reference the loaded messages — keyed by
70-
* `#e` over their ids, not by a time window — and merge them into the same
71-
* channel cache.
69+
* After a content-kinds-only history fetch, pull structural auxiliary events
70+
* (edits/deletions) that reference the loaded messages — keyed by `#e` over
71+
* their ids, not by a time window — and merge them into the same channel cache.
72+
* Reactions are hydrated separately for the rows the GUI renders.
7273
*
7374
* History fetches request content kinds only so the `limit` budget buys
74-
* visible message depth (a reaction-heavy 200-event window was only ~136
75-
* messages). The cost is that an edit/deletion for a visible message can fall
76-
* outside any fetched time window — so aux must be pulled by reference, or a
77-
* visible message renders stale (un-edited / not-deleted).
75+
* visible message depth. The cost is that an edit/deletion for a visible
76+
* message can fall outside any fetched time window — so structural aux must be
77+
* pulled by reference, or a visible message renders stale (un-edited /
78+
* not-deleted).
7879
*
7980
* Best-effort: failures are logged but never reject, so a flaky overlay fetch
8081
* can't blank the freshly-loaded messages.
@@ -92,9 +93,10 @@ export async function backfillAuxForMessages(
9293
try {
9394
const cacheKey = channelMessagesKey(channelId);
9495
const cachedEvents = queryClient.getQueryData<RelayEvent[]>(cacheKey) ?? [];
95-
const auxEvents = await relayClient.fetchAuxEventsForMessages(
96+
const auxEvents = await relayClient.fetchAuxEventsByReference(
9697
channelId,
9798
messageIds,
99+
buildChannelStructuralAuxFilter,
98100
);
99101
const mergedAuxEvents = await mergeAuxEventsWithDeletionBackfill({
100102
channelId,
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import {
5+
claimUnhydratedRenderScopedReactionIds,
6+
collectRenderScopedReactionMessageIds,
7+
hydrateRenderScopedReactions,
8+
releaseRenderScopedReactionIds,
9+
resetRenderScopedReactionHydration,
10+
} from "./renderScopedReactions.ts";
11+
import { formatTimelineMessages } from "./formatTimelineMessages.ts";
12+
import { channelMessagesKey } from "./messageQueryKeys.ts";
13+
14+
const CHANNEL_ID = "36411e44-0e2d-4cfe-bd6e-567eb169db9f";
15+
16+
function hex(char) {
17+
return char.repeat(64);
18+
}
19+
20+
function event(id, kind, overrides = {}) {
21+
return {
22+
id,
23+
pubkey: hex("a"),
24+
kind,
25+
created_at: 1_700_000_000,
26+
content: "",
27+
tags: [["h", CHANNEL_ID]],
28+
sig: "sig",
29+
...overrides,
30+
};
31+
}
32+
33+
function entry(message) {
34+
return { message, summary: null };
35+
}
36+
37+
function makeQueryClientStub(initialEvents = []) {
38+
const store = new Map([
39+
[JSON.stringify(channelMessagesKey(CHANNEL_ID)), initialEvents],
40+
]);
41+
return {
42+
getQueryData(key) {
43+
return store.get(JSON.stringify(key));
44+
},
45+
setQueryData(key, updater) {
46+
const k = JSON.stringify(key);
47+
const next =
48+
typeof updater === "function" ? updater(store.get(k) ?? []) : updater;
49+
store.set(k, next);
50+
return next;
51+
},
52+
};
53+
}
54+
55+
test.afterEach(() => {
56+
resetRenderScopedReactionHydration();
57+
});
58+
59+
test("collects main timeline and open thread messages without hidden replies", () => {
60+
const main = event(hex("1"), 9);
61+
const hiddenCollapsedReply = event(hex("2"), 9, {
62+
tags: [["e", main.id]],
63+
});
64+
const threadHead = event(hex("3"), 9);
65+
const visibleThreadReply = event(hex("4"), 9, {
66+
tags: [["e", threadHead.id]],
67+
});
68+
69+
assert.deepEqual(
70+
collectRenderScopedReactionMessageIds({
71+
mainEntries: [entry(main), entry(threadHead)],
72+
threadHeadMessage: threadHead,
73+
threadEntries: [entry(visibleThreadReply)],
74+
}),
75+
[main.id, threadHead.id, visibleThreadReply.id],
76+
);
77+
78+
assert.ok(
79+
!collectRenderScopedReactionMessageIds({
80+
mainEntries: [entry(main), entry(threadHead)],
81+
threadHeadMessage: threadHead,
82+
threadEntries: [entry(visibleThreadReply)],
83+
}).includes(hiddenCollapsedReply.id),
84+
);
85+
});
86+
87+
test("claims each rendered message id once per channel and can retry released ids", () => {
88+
assert.deepEqual(
89+
claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [
90+
hex("1"),
91+
hex("2"),
92+
hex("1"),
93+
]),
94+
[hex("1"), hex("2")],
95+
);
96+
assert.deepEqual(
97+
claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [hex("1"), hex("2")]),
98+
[],
99+
);
100+
assert.deepEqual(
101+
claimUnhydratedRenderScopedReactionIds("other-channel", [hex("1")]),
102+
[hex("1")],
103+
);
104+
105+
releaseRenderScopedReactionIds(CHANNEL_ID, [hex("2")]);
106+
assert.deepEqual(
107+
claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [hex("1"), hex("2")]),
108+
[hex("2")],
109+
);
110+
});
111+
112+
test("hydrates visible reactions into the channel timeline cache", async () => {
113+
const messageId = hex("1");
114+
const reactionId = hex("2");
115+
const currentUser = hex("c");
116+
const message = event(messageId, 9, {
117+
pubkey: hex("a"),
118+
content: "ship it?",
119+
});
120+
const reaction = event(reactionId, 7, {
121+
pubkey: currentUser,
122+
content: "✅",
123+
tags: [["e", messageId]],
124+
});
125+
const queryClient = makeQueryClientStub([message]);
126+
const calls = [];
127+
128+
await hydrateRenderScopedReactions({
129+
channelId: CHANNEL_ID,
130+
messageIds: [messageId],
131+
queryClient,
132+
deps: {
133+
fetchReactionEventsForMessages: async (channelId, messageIds) => {
134+
calls.push({ channelId, messageIds });
135+
return [reaction];
136+
},
137+
},
138+
});
139+
140+
assert.deepEqual(calls, [{ channelId: CHANNEL_ID, messageIds: [messageId] }]);
141+
const cached = queryClient.getQueryData(channelMessagesKey(CHANNEL_ID));
142+
assert.ok(cached.some((e) => e.id === reactionId));
143+
144+
const timeline = formatTimelineMessages(cached, null, currentUser, null);
145+
assert.deepEqual(
146+
timeline
147+
.find((m) => m.id === messageId)
148+
?.reactions?.map((r) => ({
149+
count: r.count,
150+
emoji: r.emoji,
151+
mine: r.reactedByCurrentUser,
152+
})),
153+
[{ count: 1, emoji: "✅", mine: true }],
154+
);
155+
});
156+
157+
test("failed hydration releases ids so the next render can retry", async () => {
158+
const messageId = hex("1");
159+
const queryClient = makeQueryClientStub([event(messageId, 9)]);
160+
161+
await hydrateRenderScopedReactions({
162+
channelId: CHANNEL_ID,
163+
messageIds: [messageId],
164+
queryClient,
165+
deps: {
166+
fetchReactionEventsForMessages: async () => {
167+
throw new Error("relay timeout");
168+
},
169+
},
170+
});
171+
172+
assert.deepEqual(
173+
claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [messageId]),
174+
[messageId],
175+
);
176+
});

0 commit comments

Comments
 (0)