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