|
| 1 | +import { HDSModel } from './HDSModel.ts'; |
| 2 | + |
| 3 | +export interface AppStreamDef { |
| 4 | + key: string; |
| 5 | + suffix: string; |
| 6 | + eventType: string; |
| 7 | + label: { [lang: string]: string }; |
| 8 | + description: { [lang: string]: string }; |
| 9 | + display: string; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * AppStreams - Extension of HDSModel |
| 14 | + * |
| 15 | + * Manages app-contextual stream definitions. |
| 16 | + * Each app/bridge access declares `clientData.appStreamId` (e.g. "bridge-mira-app"). |
| 17 | + * AppStreams definitions declare sub-streams by suffix (e.g. suffix "notes" → "bridge-mira-app-notes"). |
| 18 | + */ |
| 19 | +export class HDSModelAppStreams { |
| 20 | + #model: HDSModel; |
| 21 | + #defs: AppStreamDef[] | null; |
| 22 | + |
| 23 | + constructor (model: HDSModel) { |
| 24 | + this.#model = model; |
| 25 | + this.#defs = null; |
| 26 | + } |
| 27 | + |
| 28 | + /** Get all app stream definitions */ |
| 29 | + getAll (): AppStreamDef[] { |
| 30 | + if (!this.#defs) { |
| 31 | + this.#defs = []; |
| 32 | + const raw = this.#model.modelData.appStreams || {}; |
| 33 | + for (const [key, def] of Object.entries(raw)) { |
| 34 | + const d = def as any; |
| 35 | + this.#defs.push({ |
| 36 | + key, |
| 37 | + suffix: d.suffix, |
| 38 | + eventType: d.eventType, |
| 39 | + label: d.label || {}, |
| 40 | + description: d.description || {}, |
| 41 | + display: d.display || 'diary', |
| 42 | + }); |
| 43 | + } |
| 44 | + } |
| 45 | + return this.#defs; |
| 46 | + } |
| 47 | + |
| 48 | + /** Get an app stream definition by key (e.g. "notes", "chat") */ |
| 49 | + forKey (key: string): AppStreamDef | null { |
| 50 | + return this.getAll().find(d => d.key === key) || null; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Resolve the full stream ID for an app stream definition given an appStreamId. |
| 55 | + * E.g. resolveStreamId("bridge-mira-app", "notes") → "bridge-mira-app-notes" |
| 56 | + */ |
| 57 | + resolveStreamId (appStreamId: string, key: string): string | null { |
| 58 | + const def = this.forKey(key); |
| 59 | + if (!def) return null; |
| 60 | + return `${appStreamId}-${def.suffix}`; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Resolve all app sub-stream IDs for a given appStreamId. |
| 65 | + * Returns a map of key → full stream ID. |
| 66 | + * E.g. resolveAll("bridge-mira-app") → { notes: "bridge-mira-app-notes", chat: "bridge-mira-app-chat" } |
| 67 | + */ |
| 68 | + resolveAll (appStreamId: string): Record<string, string> { |
| 69 | + const result: Record<string, string> = {}; |
| 70 | + for (const def of this.getAll()) { |
| 71 | + result[def.key] = `${appStreamId}-${def.suffix}`; |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Check if a streamId belongs to an app stream. |
| 78 | + * Returns the app stream definition key if matched, null otherwise. |
| 79 | + * E.g. matchStream("bridge-mira-app-notes", "bridge-mira-app") → "notes" |
| 80 | + */ |
| 81 | + matchStream (streamId: string, appStreamId: string): string | null { |
| 82 | + for (const def of this.getAll()) { |
| 83 | + if (streamId === `${appStreamId}-${def.suffix}`) return def.key; |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Extract appStreamId from an access's clientData. |
| 90 | + * Returns null if not set. |
| 91 | + */ |
| 92 | + static getAppStreamId (access: { clientData?: Record<string, any> }): string | null { |
| 93 | + return access?.clientData?.appStreamId || null; |
| 94 | + } |
| 95 | +} |
0 commit comments