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; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 8 | const BATCH_MS = 150; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 9 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 10 | export interface UrlPreviewSettings { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 11 | encrypted_rooms: boolean; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 12 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 13 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 14 | const DEFAULT_SETTINGS: UrlPreviewSettings = { encrypted_rooms: false }; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 15 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 16 | export const linkPreviews = writable<Record<string, LinkPreview | null>>({}); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 17 | export const urlPreviewSettings = writable<UrlPreviewSettings>(DEFAULT_SETTINGS); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 18 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 19 | const inFlight = new Set<string>(); |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 20 | const landed: Record<string, LinkPreview | null> = {}; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 21 | let flushTimer: ReturnType<typeof setTimeout> | null = null; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 22 | |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 23 | /** Previews resolve one by one after a room opens, and each one landing on its own shifted the timeline again. */ |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 24 | function land(url: string, preview: LinkPreview | null): void { |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 25 | landed[url] = preview; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 26 | flushTimer ??= setTimeout(() => { |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 27 | flushTimer = null; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 28 | const batch = { ...landed }; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 29 | for (const key of Object.keys(landed)) delete landed[key]; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 30 | linkPreviews.update((all) => ({ ...all, ...batch })); |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 31 | }, BATCH_MS); |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 32 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 33 | let backoffUntil = 0; |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 34 | let refusalLogged = false; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 35 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 36 | export function requestLinkPreview(roomId: string, url: string): void { |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 37 | if (inFlight.has(url) || url in landed || url in get(linkPreviews) || Date.now() < backoffUntil) return; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 38 | inFlight.add(url); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 39 | getUrlPreview(roomId, url) |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 40 | .then((raw) => land(url, parseLinkPreview(raw))) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 41 | .catch((e) => { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 42 | if (e instanceof ApiError && e.status === 429) backoffUntil = Date.now() + BACKOFF_MS; |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 43 | else if (e instanceof ApiError && e.status === 403) { |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 44 | land(url, null); |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 45 | if (!refusalLogged) console.warn('The homeserver refused a link preview', e.message); |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 46 | refusalLogged = true; |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 47 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 48 | }) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 49 | .finally(() => inFlight.delete(url)); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 50 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 51 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 52 | function parseSettings(raw: unknown): UrlPreviewSettings { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 53 | if (typeof raw !== 'object' || raw === null) return DEFAULT_SETTINGS; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 54 | return { encrypted_rooms: (raw as Record<string, unknown>)['encrypted_rooms'] === true }; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 55 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 56 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 57 | export async function loadUrlPreviewSettings(): Promise<void> { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 58 | try { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 59 | urlPreviewSettings.set(parseSettings(await getAccountData(EVENT_TYPE))); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 60 | } catch { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 61 | urlPreviewSettings.set(DEFAULT_SETTINGS); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 62 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 63 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 64 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 65 | export async function saveUrlPreviewSettings(settings: UrlPreviewSettings): Promise<void> { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 66 | await setAccountData(EVENT_TYPE, { encrypted_rooms: settings.encrypted_rooms }); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 67 | urlPreviewSettings.set(settings); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 68 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 69 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 70 | export function resetLinkPreviews(): void { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 71 | linkPreviews.set({}); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 72 | urlPreviewSettings.set(DEFAULT_SETTINGS); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 73 | inFlight.clear(); |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 74 | if (flushTimer) clearTimeout(flushTimer); |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 75 | flushTimer = null; |
85334efe0e3a | atagen | 2026-10-27 05:55 UTC | 76 | for (const key of Object.keys(landed)) delete landed[key]; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 77 | backoffUntil = 0; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 78 | } |