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 { effectiveRooms } from './mute'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 4 | import { arrivalIn, isRoomWatched } from './notification-model'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 5 | import { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 6 | DEFAULT_MESSAGE_SOUND, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 7 | SOUND_MAX_BYTES, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 8 | parseMessageSound, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 9 | shouldPlaySound, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 10 | type MessageSoundSettings |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 11 | } from './message-sound-model'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 12 | import { selectedRoomId } from './store'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 13 | import type { RoomSummary } from './types'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 14 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 15 | const EVENT_TYPE = 'dev.malice.message_sound'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 16 | const DEFAULT_URL = '/sounds/message.wav'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 17 | const CACHE_NAME = 'malice-sounds'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 18 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 19 | export const messageSound = writable<MessageSoundSettings>(DEFAULT_MESSAGE_SOUND); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 20 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 21 | const resolved = new Map<string, Promise<string>>(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 22 | let lastPlayedAt = 0; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 23 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 24 | export async function loadMessageSound(): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 25 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 26 | messageSound.set(parseMessageSound(await getAccountData(EVENT_TYPE))); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 27 | } catch { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 28 | messageSound.set(DEFAULT_MESSAGE_SOUND); |
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 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 32 | export async function saveMessageSound(settings: MessageSoundSettings): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 33 | await setAccountData(EVENT_TYPE, { mode: settings.mode, sound: settings.sound }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 34 | messageSound.set(settings); |
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 | export function resetMessageSound(): void { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 38 | messageSound.set(DEFAULT_MESSAGE_SOUND); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 39 | for (const url of resolved.values()) void url.then((u) => URL.revokeObjectURL(u), () => undefined); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 40 | resolved.clear(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 41 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 42 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 43 | const cacheKey = (mxc: string) => `/__sounds/${encodeURIComponent(mxc)}`; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 44 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 45 | async function remember(mxc: string, blob: Blob): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 46 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 47 | 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 | 48 | } catch {} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 49 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 50 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 51 | async function fetchSound(mxc: string): Promise<Blob> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 52 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 53 | const hit = await (await caches.open(CACHE_NAME)).match(cacheKey(mxc)); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 54 | if (hit) return await hit.blob(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 55 | } catch {} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 56 | const res = await fetch(messageMediaUrl(mxc, 'Full'), { credentials: 'same-origin' }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 57 | if (!res.ok) throw new Error('Sound unavailable'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 58 | const blob = await res.blob(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 59 | await remember(mxc, blob); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 60 | return blob; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 61 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 62 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 63 | function resolve(mxc: string | null): Promise<string> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 64 | if (!mxc) return Promise.resolve(DEFAULT_URL); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 65 | let url = resolved.get(mxc); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 66 | if (!url) { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 67 | url = fetchSound(mxc).then( |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 68 | (blob) => URL.createObjectURL(blob), |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 69 | () => DEFAULT_URL |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 70 | ); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 71 | resolved.set(mxc, url); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 72 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 73 | return url; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 74 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 75 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 76 | export async function playMessageSound(): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 77 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 78 | const audio = new Audio(await resolve(get(messageSound).sound)); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 79 | await audio.play(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 80 | } catch {} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 81 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 82 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 83 | function decodes(file: File): Promise<boolean> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 84 | return new Promise((done) => { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 85 | const url = URL.createObjectURL(file); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 86 | const audio = new Audio(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 87 | const finish = (ok: boolean) => { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 88 | clearTimeout(timer); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 89 | URL.revokeObjectURL(url); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 90 | done(ok); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 91 | }; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 92 | const timer = setTimeout(() => finish(false), 5000); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 93 | audio.oncanplaythrough = () => finish(true); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 94 | audio.onerror = () => finish(false); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 95 | audio.preload = 'auto'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 96 | audio.src = url; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 97 | }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 98 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 99 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 100 | export async function uploadMessageSound(file: File): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 101 | if (!file.type.startsWith('audio/')) throw new Error('Choose an audio file.'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 102 | if (file.size > SOUND_MAX_BYTES) throw new Error('Sounds must be smaller than 1 MB.'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 103 | if (!(await decodes(file))) throw new Error('That audio file could not be played.'); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 104 | const { mxc } = await uploadMedia(file); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 105 | await remember(mxc, file); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 106 | await saveMessageSound({ ...get(messageSound), sound: mxc }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 107 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 108 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 109 | export function startMessageSound(): () => void { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 110 | let previous = new Map<string, RoomSummary>(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 111 | const unsubscribe = effectiveRooms.subscribe((list) => { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 112 | const prior = previous; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 113 | previous = new Map(list.map((room) => [room.room_id, room])); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 114 | const now = Date.now(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 115 | const mode = get(messageSound).mode; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 116 | for (const room of list) { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 117 | const arrival = arrivalIn(prior.get(room.room_id), room, now); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 118 | if (!arrival) continue; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 119 | if (isRoomWatched(room.room_id, get(selectedRoomId), document.visibilityState === 'visible', document.hasFocus())) continue; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 120 | if (!shouldPlaySound(mode, room, arrival, lastPlayedAt, now)) continue; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 121 | lastPlayedAt = now; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 122 | void playMessageSound(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 123 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 124 | }); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 125 | return unsubscribe; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 126 | } |