| 1 | import { isRecord } from './guards'; |
| 2 | import { parseTimelineEvent } 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 type SearchBackfill = 'Waiting' | 'Running' | 'Paused' | 'Complete' | 'Disabled' | { Failed: string }; |
| 21 | |
| 22 | export interface RoomSearchCoverage { |
| 23 | room_id: string; |
| 24 | oldest_indexed_ms: number | null; |
| 25 | start_reached: boolean; |
| 26 | backfill: SearchBackfill; |
| 27 | } |
| 28 | |
| 29 | export interface SearchCoverage { |
| 30 | rooms: RoomSearchCoverage[]; |
| 31 | persistent: boolean; |
| 32 | undecrypted: number; |
| 33 | } |
| 34 | |
| 35 | export interface SearchPage { |
| 36 | hits: SearchHit[]; |
| 37 | next: string | null; |
| 38 | coverage: SearchCoverage; |
| 39 | } |
| 40 | |
| 41 | export interface SearchSession { |
| 42 | search_id: string; |
| 43 | page: SearchPage; |
| 44 | } |
| 45 | |
| 46 | /** The wire form of a scope, as macltok's externally tagged enum. */ |
| 47 | export function scopeWire(scope: SearchScope): unknown { |
| 48 | if (scope.kind === 'room') return { Room: scope.room_id }; |
| 49 | if (scope.kind === 'rooms') return { Rooms: scope.room_ids }; |
| 50 | return 'All'; |
| 51 | } |
| 52 | |
| 53 | /** macltok's matches are utf-8 byte ranges, so they're mapped back to string offsets against the body. */ |
| 54 | function parseRanges(raw: unknown, body: string): [number, number][] { |
| 55 | if (!Array.isArray(raw)) return []; |
| 56 | const bytes = new TextEncoder().encode(body); |
| 57 | const decoder = new TextDecoder(); |
| 58 | const offset = (byte: number) => decoder.decode(bytes.subarray(0, byte)).length; |
| 59 | return raw.flatMap((range) => |
| 60 | isRecord(range) && typeof range['start'] === 'number' && typeof range['end'] === 'number' |
| 61 | ? [[offset(range['start']), offset(range['end'])] as [number, number]] |
| 62 | : [] |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | function hitBody(event: TimelineEvent): string { |
| 67 | const content = event.content; |
| 68 | if (content.kind === 'Message') return content.message.body; |
| 69 | return ''; |
| 70 | } |
| 71 | |
| 72 | function parseHit(raw: unknown): SearchHit | null { |
| 73 | if (!isRecord(raw) || typeof raw['room_id'] !== 'string' || typeof raw['event_id'] !== 'string' || !isRecord(raw['event'])) return null; |
| 74 | const event = parseTimelineEvent(raw['event']); |
| 75 | return { |
| 76 | room_id: raw['room_id'], |
| 77 | event_id: raw['event_id'], |
| 78 | timestamp_ms: typeof raw['timestamp_ms'] === 'number' ? raw['timestamp_ms'] : event.timestamp_ms, |
| 79 | event, |
| 80 | ranges: parseRanges(raw['matches'], hitBody(event)) |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | function parseBackfill(raw: unknown): SearchBackfill { |
| 85 | if (raw === 'Waiting' || raw === 'Running' || raw === 'Paused' || raw === 'Complete' || raw === 'Disabled') return raw; |
| 86 | if (isRecord(raw) && isRecord(raw['Failed']) && typeof raw['Failed']['message'] === 'string') return { Failed: raw['Failed']['message'] }; |
| 87 | return 'Waiting'; |
| 88 | } |
| 89 | |
| 90 | function parseRoomCoverage(raw: unknown): RoomSearchCoverage | null { |
| 91 | if (!isRecord(raw) || typeof raw['room_id'] !== 'string') return null; |
| 92 | return { |
| 93 | room_id: raw['room_id'], |
| 94 | oldest_indexed_ms: typeof raw['oldest_indexed_ms'] === 'number' ? raw['oldest_indexed_ms'] : null, |
| 95 | start_reached: raw['start_reached'] === true, |
| 96 | backfill: parseBackfill(raw['backfill']) |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | function parseCoverage(raw: unknown): SearchCoverage { |
| 101 | if (!isRecord(raw)) return { rooms: [], persistent: false, undecrypted: 0 }; |
| 102 | return { |
| 103 | rooms: Array.isArray(raw['rooms']) ? raw['rooms'].flatMap((room) => parseRoomCoverage(room) ?? []) : [], |
| 104 | persistent: raw['persistent'] === true, |
| 105 | undecrypted: typeof raw['undecrypted'] === 'number' ? raw['undecrypted'] : 0 |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | export function parseSearchPage(raw: unknown): SearchPage | null { |
| 110 | if (!isRecord(raw) || !Array.isArray(raw['hits'])) return null; |
| 111 | return { |
| 112 | hits: raw['hits'].flatMap((hit) => parseHit(hit) ?? []), |
| 113 | next: typeof raw['next_cursor'] === 'string' ? raw['next_cursor'] : null, |
| 114 | coverage: parseCoverage(raw['coverage']) |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | export function parseSearchSession(raw: unknown): SearchSession | null { |
| 119 | if (!isRecord(raw) || typeof raw['search_id'] !== 'string') return null; |
| 120 | const page = parseSearchPage(raw['page']); |
| 121 | return page ? { search_id: raw['search_id'], page } : null; |
| 122 | } |
| 123 | |
| 124 | /** Hits grouped by room, keeping the order in which each room first appears. */ |
| 125 | export function groupByRoom(hits: SearchHit[]): { room_id: string; hits: SearchHit[] }[] { |
| 126 | const groups = new Map<string, SearchHit[]>(); |
| 127 | for (const hit of hits) { |
| 128 | const list = groups.get(hit.room_id); |
| 129 | if (list) list.push(hit); |
| 130 | else groups.set(hit.room_id, [hit]); |
| 131 | } |
| 132 | return [...groups].map(([room_id, list]) => ({ room_id, hits: list })); |
| 133 | } |