MCPcopy Create free account
hub / github.com/block/buzz / fetchRouteTargetEvents

Function fetchRouteTargetEvents

desktop/src/app/routes/ChannelRouteScreen.tsx:44–94  ·  view source on GitHub ↗
(
  eventIds: string[],
  targetMessageId: string | null,
  targetThreadRootId: string | null,
)

Source from the content-addressed store, hash-verified

42}
43
44async function fetchRouteTargetEvents(
45 eventIds: string[],
46 targetMessageId: string | null,
47 targetThreadRootId: string | null,
48): Promise<RelayEvent[]> {
49 const eventsById = new Map<string, RelayEvent>();
50 const addEvent = (event: RelayEvent | null) => {
51 if (event) {
52 eventsById.set(event.id, event);
53 }
54 };
55
56 const uniqueEventIds = [...new Set(eventIds)];
57 const initialEvents = await Promise.all(uniqueEventIds.map(fetchRouteEvent));
58 for (const event of initialEvents) {
59 addEvent(event);
60 }
61
62 const targetEvent = targetMessageId
63 ? (eventsById.get(targetMessageId) ?? null)
64 : null;
65 if (!targetEvent) {
66 return [...eventsById.values()];
67 }
68
69 const targetThreadRef = getThreadReference(targetEvent.tags);
70 const threadRootId = targetThreadRootId ?? targetThreadRef.rootId ?? null;
71 if (threadRootId && !eventsById.has(threadRootId)) {
72 addEvent(await fetchRouteEvent(threadRootId));
73 }
74
75 let parentId = getReplyParentId(targetEvent);
76 let guard = 0;
77 while (
78 parentId &&
79 parentId !== threadRootId &&
80 guard < MAX_ROUTE_ANCESTOR_HOPS
81 ) {
82 const parentEvent =
83 eventsById.get(parentId) ?? (await fetchRouteEvent(parentId));
84 if (!parentEvent) {
85 break;
86 }
87
88 eventsById.set(parentEvent.id, parentEvent);
89 parentId = getReplyParentId(parentEvent);
90 guard += 1;
91 }
92
93 return [...eventsById.values()];
94}
95
96export function ChannelRouteScreen({
97 channelId,

Callers 1

ChannelRouteScreenFunction · 0.85

Calls 7

getThreadReferenceFunction · 0.90
addEventFunction · 0.85
fetchRouteEventFunction · 0.85
getReplyParentIdFunction · 0.85
hasMethod · 0.80
setMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected