ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 1 | import { isRecord } from './guards'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 2 | import { parseTimelineItem } from './types'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 3 | import type { TimelineEvent } from './types'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 4 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 5 | export type SearchScope = { kind: 'room'; room_id: string } | { kind: 'rooms'; room_ids: string[] } | { kind: 'all' }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 6 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 7 | export interface ScopeOption { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 8 | label: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 9 | scope: SearchScope; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 10 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 11 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 12 | export interface SearchHit { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 13 | room_id: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 14 | event_id: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 15 | timestamp_ms: number; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 16 | event: TimelineEvent; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 17 | ranges: [number, number][]; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 18 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 19 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 20 | export interface SearchCoverage { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 21 | room_id: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 22 | oldest_indexed_ms: number | null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 23 | reached_start: boolean; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 24 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 25 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 26 | export interface SearchPage { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 27 | hits: SearchHit[]; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 28 | next: string | null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 29 | coverage: SearchCoverage[]; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 30 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 31 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 32 | /** The wire form of a scope, as macltok's externally tagged enum. */ |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 33 | export function scopeWire(scope: SearchScope): unknown { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 34 | if (scope.kind === 'room') return { Room: scope.room_id }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 35 | if (scope.kind === 'rooms') return { Rooms: scope.room_ids }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 36 | return 'All'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 37 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 38 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 39 | function parseRanges(raw: unknown): [number, number][] { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 40 | if (!Array.isArray(raw)) return []; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 41 | return raw.flatMap((range) => |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 42 | Array.isArray(range) && typeof range[0] === 'number' && typeof range[1] === 'number' ? [[range[0], range[1]] as [number, number]] : [] |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 43 | ); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 44 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 45 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 46 | function parseHit(raw: unknown): SearchHit | null { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 47 | if (!isRecord(raw) || typeof raw['room_id'] !== 'string' || typeof raw['event_id'] !== 'string') return null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 48 | const item = parseTimelineItem(raw['preview'] ?? raw['event']); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 49 | if (item?.kind !== 'event') return null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 50 | return { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 51 | room_id: raw['room_id'], |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 52 | event_id: raw['event_id'], |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 53 | timestamp_ms: typeof raw['timestamp_ms'] === 'number' ? raw['timestamp_ms'] : item.event.timestamp_ms, |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 54 | event: item.event, |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 55 | ranges: parseRanges(raw['ranges']) |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 56 | }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 57 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 58 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 59 | function parseCoverage(raw: unknown): SearchCoverage | null { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 60 | if (!isRecord(raw) || typeof raw['room_id'] !== 'string') return null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 61 | return { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 62 | room_id: raw['room_id'], |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 63 | oldest_indexed_ms: typeof raw['oldest_indexed_ms'] === 'number' ? raw['oldest_indexed_ms'] : null, |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 64 | reached_start: raw['reached_start'] === true |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 65 | }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 66 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 67 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 68 | export function parseSearchPage(raw: unknown): SearchPage | null { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 69 | if (!isRecord(raw) || !Array.isArray(raw['hits'])) return null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 70 | return { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 71 | hits: raw['hits'].flatMap((hit) => parseHit(hit) ?? []), |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 72 | next: typeof raw['next'] === 'string' ? raw['next'] : null, |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 73 | coverage: Array.isArray(raw['coverage']) ? raw['coverage'].flatMap((c) => parseCoverage(c) ?? []) : [] |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 74 | }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 75 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 76 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 77 | /** Hits grouped by room, keeping the order in which each room first appears. */ |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 78 | export function groupByRoom(hits: SearchHit[]): { room_id: string; hits: SearchHit[] }[] { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 79 | const groups = new Map<string, SearchHit[]>(); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 80 | for (const hit of hits) { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 81 | const list = groups.get(hit.room_id); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 82 | if (list) list.push(hit); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 83 | else groups.set(hit.room_id, [hit]); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 84 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 85 | return [...groups].map(([room_id, list]) => ({ room_id, hits: list })); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 86 | } |