( eventIds: string[], targetMessageId: string | null, targetThreadRootId: string | null, )
| 42 | } |
| 43 | |
| 44 | async 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 | |
| 96 | export function ChannelRouteScreen({ |
| 97 | channelId, |
no test coverage detected