c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 1 | import { get, writable } from 'svelte/store'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 2 | import { getAccountData, messageMediaUrl, setAccountData, uploadMedia } from './api'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 3 | import { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 4 | DEFAULT_MESSAGE_SOUND, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 5 | SOUND_MAX_BYTES, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 6 | parseMessageSound, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 7 | type MessageSoundSettings |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 8 | } from './message-sound-model'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 9 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 10 | const EVENT_TYPE = 'dev.malice.message_sound'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 11 | const DEFAULT_URL = '/sounds/message.wav'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 12 | const CACHE_NAME = 'malice-sounds'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 13 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 14 | export const messageSound = writable<MessageSoundSettings>(DEFAULT_MESSAGE_SOUND); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 15 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 16 | const resolved = new Map<string, Promise<string>>(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 17 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 18 | export async function loadMessageSound(): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 19 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 20 | messageSound.set(parseMessageSound(await getAccountData(EVENT_TYPE))); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 21 | } catch { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 22 | messageSound.set(DEFAULT_MESSAGE_SOUND); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 23 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 24 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 25 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 26 | export async function saveMessageSound(settings: MessageSoundSettings): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 27 | await setAccountData(EVENT_TYPE, { mode: settings.mode, sound: settings.sound }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 28 | messageSound.set(settings); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 29 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 30 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 31 | export function resetMessageSound(): void { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 32 | messageSound.set(DEFAULT_MESSAGE_SOUND); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 33 | for (const url of resolved.values()) void url.then((u) => URL.revokeObjectURL(u), () => undefined); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 34 | resolved.clear(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 35 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 36 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 37 | const cacheKey = (mxc: string) => `/__sounds/${encodeURIComponent(mxc)}`; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 38 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 39 | async function remember(mxc: string, blob: Blob): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 40 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 41 | await (await caches.open(CACHE_NAME)).put(cacheKey(mxc), new Response(blob, { headers: { 'content-type': blob.type } })); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 42 | } catch {} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 43 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 44 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 45 | async function fetchSound(mxc: string): Promise<Blob> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 46 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 47 | const hit = await (await caches.open(CACHE_NAME)).match(cacheKey(mxc)); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 48 | if (hit) return await hit.blob(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 49 | } catch {} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 50 | const res = await fetch(messageMediaUrl(mxc, 'Full'), { credentials: 'same-origin' }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 51 | if (!res.ok) throw new Error('Sound unavailable'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 52 | const blob = await res.blob(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 53 | await remember(mxc, blob); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 54 | return blob; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 55 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 56 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 57 | function resolve(mxc: string | null): Promise<string> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 58 | if (!mxc) return Promise.resolve(DEFAULT_URL); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 59 | let url = resolved.get(mxc); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 60 | if (!url) { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 61 | url = fetchSound(mxc).then( |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 62 | (blob) => URL.createObjectURL(blob), |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 63 | () => DEFAULT_URL |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 64 | ); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 65 | resolved.set(mxc, url); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 66 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 67 | return url; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 68 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 69 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 70 | export async function playMessageSound(): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 71 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 72 | const audio = new Audio(await resolve(get(messageSound).sound)); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 73 | await audio.play(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 74 | } catch {} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 75 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 76 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 77 | function decodes(file: File): Promise<boolean> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 78 | return new Promise((done) => { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 79 | const url = URL.createObjectURL(file); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 80 | const audio = new Audio(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 81 | const finish = (ok: boolean) => { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 82 | clearTimeout(timer); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 83 | URL.revokeObjectURL(url); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 84 | done(ok); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 85 | }; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 86 | const timer = setTimeout(() => finish(false), 5000); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 87 | audio.oncanplaythrough = () => finish(true); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 88 | audio.onerror = () => finish(false); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 89 | audio.preload = 'auto'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 90 | audio.src = url; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 91 | }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 92 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 93 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 94 | export async function uploadMessageSound(file: File): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 95 | if (!file.type.startsWith('audio/')) throw new Error('Choose an audio file.'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 96 | if (file.size > SOUND_MAX_BYTES) throw new Error('Sounds must be smaller than 1 MB.'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 97 | if (!(await decodes(file))) throw new Error('That audio file could not be played.'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 98 | const { mxc } = await uploadMedia(file); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 99 | await remember(mxc, file); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 100 | await saveMessageSound({ ...get(messageSound), sound: mxc }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 101 | } |