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; |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 20 | let refusalLogged = false; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 21 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 22 | export function requestLinkPreview(roomId: string, url: string): void { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 23 | if (inFlight.has(url) || url in get(linkPreviews) || Date.now() < backoffUntil) return; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 24 | inFlight.add(url); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 25 | getUrlPreview(roomId, url) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 26 | .then((raw) => linkPreviews.update((all) => ({ ...all, [url]: parseLinkPreview(raw) }))) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 27 | .catch((e) => { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 28 | if (e instanceof ApiError && e.status === 429) backoffUntil = Date.now() + BACKOFF_MS; |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 29 | else if (e instanceof ApiError && e.status === 403) { |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 30 | linkPreviews.update((all) => ({ ...all, [url]: null })); |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 31 | if (!refusalLogged) console.warn('The homeserver refused a link preview', e.message); |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 32 | refusalLogged = true; |
719f7cdd974e | atagen | 2026-10-26 10:15 UTC | 33 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 34 | }) |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 35 | .finally(() => inFlight.delete(url)); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 36 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 37 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 38 | function parseSettings(raw: unknown): UrlPreviewSettings { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 39 | if (typeof raw !== 'object' || raw === null) return DEFAULT_SETTINGS; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 40 | return { encrypted_rooms: (raw as Record<string, unknown>)['encrypted_rooms'] === true }; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 41 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 42 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 43 | export async function loadUrlPreviewSettings(): Promise<void> { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 44 | try { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 45 | urlPreviewSettings.set(parseSettings(await getAccountData(EVENT_TYPE))); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 46 | } catch { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 47 | urlPreviewSettings.set(DEFAULT_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 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 51 | export async function saveUrlPreviewSettings(settings: UrlPreviewSettings): Promise<void> { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 52 | await setAccountData(EVENT_TYPE, { encrypted_rooms: settings.encrypted_rooms }); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 53 | urlPreviewSettings.set(settings); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 54 | } |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 55 | |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 56 | export function resetLinkPreviews(): void { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 57 | linkPreviews.set({}); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 58 | urlPreviewSettings.set(DEFAULT_SETTINGS); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 59 | inFlight.clear(); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 60 | backoffUntil = 0; |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 61 | } |