ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 1 | import { get, writable } from 'svelte/store'; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 2 | import { ApiError, getAccountData, getUrlPreview, setAccountData } from './api'; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 3 | import { parseLinkPreview } from './types'; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 4 | import type { LinkPreview } from './types'; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 5 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 6 | const EVENT_TYPE = 'dev.malice.url_previews'; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 7 | const BACKOFF_MS = 30_000; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 8 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 9 | export interface UrlPreviewSettings { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 10 | encrypted_rooms: boolean; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 11 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 12 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 13 | const DEFAULT_SETTINGS: UrlPreviewSettings = { encrypted_rooms: false }; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 14 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 15 | export const linkPreviews = writable<Record<string, LinkPreview | null>>({}); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 16 | export const urlPreviewSettings = writable<UrlPreviewSettings>(DEFAULT_SETTINGS); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 17 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 18 | const inFlight = new Set<string>(); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 19 | let backoffUntil = 0; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 20 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 21 | export function requestLinkPreview(roomId: string, url: string): void { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 22 | if (inFlight.has(url) || url in get(linkPreviews) || Date.now() < backoffUntil) return; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 23 | inFlight.add(url); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 24 | getUrlPreview(roomId, url) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 25 | .then((raw) => linkPreviews.update((all) => ({ ...all, [url]: parseLinkPreview(raw) }))) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 26 | .catch((e) => { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 27 | if (e instanceof ApiError && e.status === 429) backoffUntil = Date.now() + BACKOFF_MS; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 28 | }) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 29 | .finally(() => inFlight.delete(url)); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 30 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 31 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 32 | function parseSettings(raw: unknown): UrlPreviewSettings { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 33 | if (typeof raw !== 'object' || raw === null) return DEFAULT_SETTINGS; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 34 | return { encrypted_rooms: (raw as Record<string, unknown>)['encrypted_rooms'] === true }; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 35 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 36 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 37 | export async function loadUrlPreviewSettings(): Promise<void> { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 38 | try { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 39 | urlPreviewSettings.set(parseSettings(await getAccountData(EVENT_TYPE))); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 40 | } catch { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 41 | urlPreviewSettings.set(DEFAULT_SETTINGS); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 42 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 43 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 44 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 45 | export async function saveUrlPreviewSettings(settings: UrlPreviewSettings): Promise<void> { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 46 | await setAccountData(EVENT_TYPE, { encrypted_rooms: settings.encrypted_rooms }); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 47 | urlPreviewSettings.set(settings); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 48 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 49 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 50 | export function resetLinkPreviews(): void { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 51 | linkPreviews.set({}); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 52 | urlPreviewSettings.set(DEFAULT_SETTINGS); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 53 | inFlight.clear(); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 54 | backoffUntil = 0; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 55 | } |