Skip to content

Commit e09e94a

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(desktop): preserve live events and window order across channel refreshes (#1802)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 109800f commit e09e94a

8 files changed

Lines changed: 523 additions & 128 deletions

File tree

‎desktop/src/features/messages/hooks.ts‎

Lines changed: 16 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@ import { toast } from "sonner";
55
import {
66
channelMessagesKey,
77
channelWindowKey,
8-
dedupeMessagesById,
9-
normalizeTimelineMessages,
10-
sortMessages,
118
threadRepliesKey,
129
} from "@/features/messages/lib/messageQueryKeys";
1310
import {
1411
buildReplyTags,
15-
getChannelIdFromTags,
1612
getThreadReference,
1713
isBroadcastReply,
1814
normalizeMentionPubkeys,
1915
resolveReplyRootId,
2016
} from "@/features/messages/lib/threading";
17+
import {
18+
projectChannelWindowMessages,
19+
refreshChannelWindowMessages,
20+
} from "@/features/messages/lib/projectChannelWindow";
21+
import { reconcileChannelWindowMessages } from "@/features/messages/lib/channelWindowReconciliation";
22+
import {
23+
mergeMessages,
24+
mergeTimelineCacheMessages,
25+
} from "@/features/messages/lib/messageMerge";
26+
27+
export { mergeMessages, mergeTimelineCacheMessages };
2128
import { splitOutgoingTags } from "@/features/messages/lib/imetaMediaMarkdown";
2229
import {
2330
clearTimeoutState,
@@ -42,7 +49,6 @@ import type { Channel, Identity, RelayEvent } from "@/shared/api/types";
4249
import { applyEditTagOverlay } from "@/features/messages/lib/applyEditTagOverlay.mjs";
4350
import {
4451
emptyChannelWindowStore,
45-
flattenChannelWindowEvents,
4652
mapChannelWindowEvents,
4753
mergeLiveChannelWindowEvent,
4854
mergeLiveThreadSummary,
@@ -72,74 +78,6 @@ type MessageQueryContext = {
7278
const CHANNEL_TIMELINE_KINDS = new Set<number>(CHANNEL_TIMELINE_CONTENT_KINDS);
7379
const CHANNEL_AUX_KINDS = new Set<number>(CHANNEL_AUX_EVENT_KINDS);
7480

75-
function getLocalRenderKey(message: RelayEvent) {
76-
return message.localKey ?? message.id;
77-
}
78-
79-
function isMatchingPendingMessage(pending: RelayEvent, incoming: RelayEvent) {
80-
if (
81-
!pending.pending ||
82-
pending.content !== incoming.content ||
83-
pending.kind !== incoming.kind ||
84-
pending.pubkey.toLowerCase() !== incoming.pubkey.toLowerCase() ||
85-
getChannelIdFromTags(pending.tags) !== getChannelIdFromTags(incoming.tags)
86-
) {
87-
return false;
88-
}
89-
90-
const pendingThread = getThreadReference(pending.tags);
91-
const incomingThread = getThreadReference(incoming.tags);
92-
93-
return (
94-
pendingThread.parentId === incomingThread.parentId &&
95-
pendingThread.rootId === incomingThread.rootId
96-
);
97-
}
98-
99-
function mergeMessagesWithNormalizer(
100-
current: RelayEvent[],
101-
incoming: RelayEvent,
102-
normalize: (messages: RelayEvent[]) => RelayEvent[],
103-
): RelayEvent[] {
104-
const normalizedCurrent = dedupeMessagesById(current);
105-
const replacedPending = normalizedCurrent.find((message) =>
106-
isMatchingPendingMessage(message, incoming),
107-
);
108-
const incomingWithLocalKey = replacedPending
109-
? {
110-
...incoming,
111-
localKey: replacedPending.localKey ?? replacedPending.id,
112-
}
113-
: incoming;
114-
const incomingLocalKey = getLocalRenderKey(incomingWithLocalKey);
115-
const deduped = normalizedCurrent.filter(
116-
(message) =>
117-
message.id !== incoming.id &&
118-
getLocalRenderKey(message) !== incomingLocalKey &&
119-
!isMatchingPendingMessage(message, incoming),
120-
);
121-
122-
return normalize([...deduped, incomingWithLocalKey]);
123-
}
124-
125-
export function mergeMessages(
126-
current: RelayEvent[],
127-
incoming: RelayEvent,
128-
): RelayEvent[] {
129-
return mergeMessagesWithNormalizer(current, incoming, sortMessages);
130-
}
131-
132-
export function mergeTimelineCacheMessages(
133-
current: RelayEvent[],
134-
incoming: RelayEvent,
135-
): RelayEvent[] {
136-
return mergeMessagesWithNormalizer(
137-
current,
138-
incoming,
139-
normalizeTimelineMessages,
140-
);
141-
}
142-
14381
export function createOptimisticMessage(
14482
channelId: string,
14583
content: string,
@@ -255,25 +193,6 @@ export function resolveThreadReplyTarget(
255193
};
256194
}
257195

258-
function retainRefetchReconciliationEvents(events: RelayEvent[]) {
259-
return events.filter((event) => {
260-
if (!CHANNEL_TIMELINE_KINDS.has(event.kind)) return false;
261-
if (event.pending) return true;
262-
const thread = getThreadReference(event.tags);
263-
return thread.parentId !== null && !isBroadcastReply(event.tags);
264-
});
265-
}
266-
267-
function mergeRefetchReconciliationEvents(
268-
windowEvents: RelayEvent[],
269-
previousMessages: RelayEvent[],
270-
) {
271-
const authoritativeIds = new Set(windowEvents.map((event) => event.id));
272-
return retainRefetchReconciliationEvents(previousMessages)
273-
.filter((event) => !authoritativeIds.has(event.id))
274-
.reduce((current, reply) => mergeMessages(current, reply), windowEvents);
275-
}
276-
277196
export function useChannelWindowQuery(channel: Channel | null) {
278197
const queryClient = useQueryClient();
279198
const queryKey = channelWindowKey(channel?.id ?? "none");
@@ -305,9 +224,8 @@ export function useChannelMessagesQuery(channel: Channel | null) {
305224
queryClient.getQueryData<ChannelWindowStore>(windowKey) ??
306225
emptyChannelWindowStore();
307226
const next = replaceNewestChannelWindow(current, page);
308-
const windowEvents = flattenChannelWindowEvents(next);
309227
queryClient.setQueryData(windowKey, next);
310-
return mergeRefetchReconciliationEvents(windowEvents, previousMessages);
228+
return reconcileChannelWindowMessages(next, previousMessages);
311229
},
312230
staleTime: 5 * 60 * 1_000,
313231
gcTime: 60 * 60 * 1_000,
@@ -320,11 +238,7 @@ export function useChannelSubscription(channel: Channel | null) {
320238
const channelType = channel?.channelType ?? null;
321239
const refreshNewestWindow = useEffectEvent(async () => {
322240
if (!channelId) return;
323-
await queryClient.invalidateQueries({
324-
queryKey: channelMessagesKey(channelId),
325-
exact: true,
326-
refetchType: "active",
327-
});
241+
await refreshChannelWindowMessages(queryClient, channelId);
328242
});
329243

330244
const appendMessage = useEffectEvent((event: RelayEvent) => {
@@ -371,10 +285,7 @@ export function useChannelSubscription(channel: Channel | null) {
371285
const next = mergeLiveChannelWindowEvent(current, event, isTimelineRow);
372286
if (next !== current) {
373287
queryClient.setQueryData(windowKey, next);
374-
queryClient.setQueryData<RelayEvent[]>(
375-
channelMessagesKey(channelId),
376-
flattenChannelWindowEvents(next),
377-
);
288+
projectChannelWindowMessages(queryClient, channelId);
378289
}
379290

380291
if (event.kind === KIND_SYSTEM_MESSAGE) {
@@ -629,10 +540,7 @@ export function useSendMessageMutation(
629540
optimisticMessage,
630541
);
631542
queryClient.setQueryData(windowKey, nextWindow);
632-
queryClient.setQueryData<RelayEvent[]>(
633-
queryKey,
634-
flattenChannelWindowEvents(nextWindow),
635-
);
543+
projectChannelWindowMessages(queryClient, effectiveChannel.id);
636544

637545
return {
638546
optimisticId: optimisticMessage.id,
@@ -680,10 +588,7 @@ export function useSendMessageMutation(
680588
localKey: context.optimisticId,
681589
});
682590
queryClient.setQueryData(windowKey, next);
683-
queryClient.setQueryData<RelayEvent[]>(
684-
context.queryKey,
685-
flattenChannelWindowEvents(next),
686-
);
591+
projectChannelWindowMessages(queryClient, context.channelId);
687592
},
688593
});
689594
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import type { RelayEvent } from "@/shared/api/types";
2+
import { CHANNEL_TIMELINE_CONTENT_KINDS } from "@/shared/constants/kinds";
3+
import {
4+
compareRelayOrder,
5+
flattenChannelWindowEvents,
6+
type ChannelWindowStore,
7+
} from "./channelWindowStore";
8+
import { reconcileIncomingMessage } from "./messageMerge";
9+
import { getThreadReference, isBroadcastReply } from "./threading";
10+
11+
const CHANNEL_TIMELINE_KINDS = new Set<number>(CHANNEL_TIMELINE_CONTENT_KINDS);
12+
13+
function retainRefetchReconciliationEvents(events: RelayEvent[]) {
14+
return events.filter((event) => {
15+
if (!CHANNEL_TIMELINE_KINDS.has(event.kind)) return false;
16+
if (event.pending) return true;
17+
const thread = getThreadReference(event.tags);
18+
return thread.parentId !== null && !isBroadcastReply(event.tags);
19+
});
20+
}
21+
22+
/**
23+
* Project the timeline from the authoritative window while retaining local
24+
* pending sends and non-broadcast thread replies the window does not contain.
25+
*/
26+
export function reconcileChannelWindowMessages(
27+
window: ChannelWindowStore,
28+
messages: RelayEvent[],
29+
) {
30+
const windowEvents = flattenChannelWindowEvents(window);
31+
const authoritativeIds = new Set(windowEvents.map((event) => event.id));
32+
const retained = retainRefetchReconciliationEvents(messages).filter(
33+
(event) => !authoritativeIds.has(event.id),
34+
);
35+
36+
// Reconcile acknowledgements against cache-only rows without changing the
37+
// authoritative window's order. The render key moves from an optimistic row
38+
// to its relay acknowledgement while the relay row remains in its original
39+
// cursor position.
40+
let cacheOnly = retained;
41+
const authoritative = windowEvents.map((event) => {
42+
const reconciled = reconcileIncomingMessage(cacheOnly, event);
43+
const incoming = reconciled.at(-1);
44+
cacheOnly = reconciled.slice(0, -1);
45+
return incoming ?? event;
46+
});
47+
48+
return mergeChronologicalMessages(cacheOnly, authoritative);
49+
}
50+
51+
function mergeChronologicalMessages(
52+
cacheOnly: RelayEvent[],
53+
authoritative: RelayEvent[],
54+
) {
55+
const retained = [...cacheOnly].sort((left, right) =>
56+
compareRelayOrder(right, left),
57+
);
58+
const merged: RelayEvent[] = [];
59+
let retainedIndex = 0;
60+
let authoritativeIndex = 0;
61+
62+
while (
63+
retainedIndex < retained.length &&
64+
authoritativeIndex < authoritative.length
65+
) {
66+
const retainedEvent = retained[retainedIndex];
67+
const authoritativeEvent = authoritative[authoritativeIndex];
68+
if (compareRelayOrder(retainedEvent, authoritativeEvent) > 0) {
69+
merged.push(retainedEvent);
70+
retainedIndex += 1;
71+
} else {
72+
merged.push(authoritativeEvent);
73+
authoritativeIndex += 1;
74+
}
75+
}
76+
77+
return merged.concat(
78+
retained.slice(retainedIndex),
79+
authoritative.slice(authoritativeIndex),
80+
);
81+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function cursorsEqual(
5757
}
5858

5959
/** Relay order: newest timestamp first, then ascending id within a second. */
60-
function compareRelayOrder(left: RelayEvent, right: RelayEvent) {
60+
export function compareRelayOrder(left: RelayEvent, right: RelayEvent) {
6161
return left.created_at !== right.created_at
6262
? right.created_at - left.created_at
6363
: left.id < right.id
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type { RelayEvent } from "@/shared/api/types";
2+
import {
3+
dedupeMessagesById,
4+
normalizeTimelineMessages,
5+
sortMessages,
6+
} from "./messageQueryKeys";
7+
import { getChannelIdFromTags, getThreadReference } from "./threading";
8+
9+
function getLocalRenderKey(message: RelayEvent) {
10+
return message.localKey ?? message.id;
11+
}
12+
13+
function isMatchingPendingMessage(pending: RelayEvent, incoming: RelayEvent) {
14+
if (
15+
!pending.pending ||
16+
incoming.pending ||
17+
pending.content !== incoming.content ||
18+
pending.kind !== incoming.kind ||
19+
pending.pubkey.toLowerCase() !== incoming.pubkey.toLowerCase() ||
20+
getChannelIdFromTags(pending.tags) !== getChannelIdFromTags(incoming.tags)
21+
) {
22+
return false;
23+
}
24+
25+
const pendingThread = getThreadReference(pending.tags);
26+
const incomingThread = getThreadReference(incoming.tags);
27+
28+
return (
29+
pendingThread.parentId === incomingThread.parentId &&
30+
pendingThread.rootId === incomingThread.rootId
31+
);
32+
}
33+
34+
export function reconcileIncomingMessage(
35+
current: RelayEvent[],
36+
incoming: RelayEvent,
37+
): RelayEvent[] {
38+
const normalizedCurrent = dedupeMessagesById(current);
39+
const replacedPending = normalizedCurrent.find((message) =>
40+
isMatchingPendingMessage(message, incoming),
41+
);
42+
const incomingWithLocalKey = replacedPending
43+
? {
44+
...incoming,
45+
localKey: replacedPending.localKey ?? replacedPending.id,
46+
}
47+
: incoming;
48+
const incomingLocalKey = getLocalRenderKey(incomingWithLocalKey);
49+
const deduped = normalizedCurrent.filter(
50+
(message) =>
51+
message.id !== incoming.id &&
52+
getLocalRenderKey(message) !== incomingLocalKey,
53+
);
54+
55+
return [...deduped, incomingWithLocalKey];
56+
}
57+
58+
function mergeMessagesWithNormalizer(
59+
current: RelayEvent[],
60+
incoming: RelayEvent,
61+
normalize: (messages: RelayEvent[]) => RelayEvent[],
62+
): RelayEvent[] {
63+
return normalize(reconcileIncomingMessage(current, incoming));
64+
}
65+
66+
export function mergeMessages(
67+
current: RelayEvent[],
68+
incoming: RelayEvent,
69+
): RelayEvent[] {
70+
return mergeMessagesWithNormalizer(current, incoming, sortMessages);
71+
}
72+
73+
export function mergeTimelineCacheMessages(
74+
current: RelayEvent[],
75+
incoming: RelayEvent,
76+
): RelayEvent[] {
77+
return mergeMessagesWithNormalizer(
78+
current,
79+
incoming,
80+
normalizeTimelineMessages,
81+
);
82+
}

0 commit comments

Comments
 (0)