1
13
14export type RoomKind = "page" | "database" | "comment" | "user";
15
16
17export interface ParsedRoom {
18 readonly kind: RoomKind;
19
20 readonly id: string;
21}
22
23const ROOM_KINDS: readonly RoomKind[] = ["page", "database", "comment", "user"];
24
25
30export function parseRoomKey(room: string): ParsedRoom | null {
31 if (typeof room !== "string" || room.length === 0) return null;
32 const idx = room.indexOf(":");
33 if (idx <= 0) return null;
34 const kind = room.slice(0, idx);
35 const id = room.slice(idx + 1);
36 if (!ROOM_KINDS.includes(kind as RoomKind)) return null;
37 if (id.length === 0 || id.includes(":")) return null;
38 if (id !== id.trim()) return null;
39 return { kind: kind as RoomKind, id };
40}
41
42
43export function roomKeyFor(kind: RoomKind, id: string): string {
44 return `${kind}:${id}`;
45}
46
47
51export function roomResource(
52 room: ParsedRoom,
53): { readonly kind: "page" | "database"; readonly id: string } | null {
54 if (room.kind === "user") return null;
55
56 if (room.kind === "comment") return null;
57 return { kind: room.kind, id: room.id };
58}
59