db4e552594cb | atagen | 2026-10-26 04:43 UTC | 1 | import { get, writable } from 'svelte/store'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 2 | import { mediaUrl } from './api'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 3 | import { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 4 | arrivalIn, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 5 | type Arrival, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 6 | badgeTotal, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 7 | isRoomWatched, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 8 | notifyingCount, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 9 | planNotification, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 10 | roomTag, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 11 | sourceFromSummary, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 12 | sourceFromTimeline, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 13 | type NotificationPlan |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 14 | } from './notification-model'; |
20fe182f3204 | atagen | 2026-10-26 05:50 UTC | 15 | import { effectiveRooms } from './mute'; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 16 | import { messageSound, playMessageSound } from './message-sound'; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 17 | import { shouldPlaySound } from './message-sound-model'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 18 | import { PUSH_CHANGED_MESSAGE } from './push-model'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 19 | import { disablePush, enablePush, pushActive, syncPush } from './push'; |
20fe182f3204 | atagen | 2026-10-26 05:50 UTC | 20 | import { selectedRoomId, sessionUser, timelines } from './store'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 21 | import type { RoomSummary } from './types'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 22 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 23 | const SETTLE_MS = 300; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 24 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 25 | export const OPEN_ROOM_MESSAGE = 'malice-open-room'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 26 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 27 | export type NotificationPermissionState = NotificationPermission | 'unsupported'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 28 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 29 | const supported = typeof Notification !== 'undefined'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 30 | const preferenceKey = (account: string) => `malice.notifications.${account}`; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 31 | |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 32 | function startMessageSound(): () => void { |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 33 | let previous = new Map<string, RoomSummary>(); |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 34 | let lastPlayedAt = 0; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 35 | const unsubscribe = effectiveRooms.subscribe((list) => { |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 36 | const prior = previous; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 37 | previous = new Map(list.map((room) => [room.room_id, room])); |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 38 | const now = Date.now(); |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 39 | const mode = get(messageSound).mode; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 40 | for (const room of list) { |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 41 | const arrival = arrivalIn(prior.get(room.room_id), room, now); |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 42 | if (!arrival) continue; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 43 | if (isRoomWatched(room.room_id, get(selectedRoomId), document.visibilityState === 'visible', document.hasFocus())) continue; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 44 | if (!shouldPlaySound(mode, room, arrival, lastPlayedAt, now)) continue; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 45 | lastPlayedAt = now; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 46 | void playMessageSound(); |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 47 | } |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 48 | }); |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 49 | return unsubscribe; |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 50 | } |
4ac384dcce6c | atagen | 2026-10-27 04:31 UTC | 51 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 52 | export const notificationsEnabled = writable(false); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 53 | export const notificationPermission = writable<NotificationPermissionState>(supported ? Notification.permission : 'unsupported'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 54 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 55 | function readPreference(account: string | null | undefined): boolean { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 56 | if (!account) return false; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 57 | try { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 58 | return localStorage.getItem(preferenceKey(account)) === '1'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 59 | } catch { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 60 | return false; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 61 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 62 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 63 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 64 | function writePreference(account: string, enabled: boolean): void { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 65 | try { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 66 | localStorage.setItem(preferenceKey(account), enabled ? '1' : '0'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 67 | } catch {} |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 68 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 69 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 70 | /** Must run inside a user gesture, since browsers refuse the permission prompt otherwise. */ |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 71 | export async function enableNotifications(): Promise<void> { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 72 | const account = get(sessionUser); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 73 | if (!supported || !account) return; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 74 | const result = Notification.permission === 'default' ? await Notification.requestPermission() : Notification.permission; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 75 | notificationPermission.set(result); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 76 | const on = result === 'granted'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 77 | writePreference(account, on); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 78 | notificationsEnabled.set(on); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 79 | if (on) await enablePush(account); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 80 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 81 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 82 | export function disableNotifications(): void { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 83 | const account = get(sessionUser); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 84 | if (account) { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 85 | writePreference(account, false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 86 | void disablePush(account); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 87 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 88 | notificationsEnabled.set(false); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 89 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 90 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 91 | function active(): boolean { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 92 | return supported && get(notificationsEnabled) && Notification.permission === 'granted'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 93 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 94 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 95 | async function registration(): Promise<ServiceWorkerRegistration | null> { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 96 | if (!('serviceWorker' in navigator) || !navigator.serviceWorker.controller) return null; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 97 | try { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 98 | return await navigator.serviceWorker.ready; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 99 | } catch { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 100 | return null; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 101 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 102 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 103 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 104 | async function show(plan: NotificationPlan, openRoom: (roomId: string) => void): Promise<void> { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 105 | const icon = mediaUrl(plan.iconMxc, 128) ?? undefined; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 106 | const options: NotificationOptions & { renotify?: boolean } = { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 107 | body: plan.body, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 108 | tag: plan.tag, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 109 | icon, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 110 | renotify: plan.renotify, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 111 | data: { roomId: plan.roomId } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 112 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 113 | const reg = await registration(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 114 | if (reg) { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 115 | await reg.showNotification(plan.title, options); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 116 | return; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 117 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 118 | const note = new Notification(plan.title, options); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 119 | note.onclick = () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 120 | window.focus(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 121 | openRoom(plan.roomId); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 122 | note.close(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 123 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 124 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 125 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 126 | async function closeRoom(roomId: string): Promise<void> { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 127 | const reg = await registration(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 128 | if (!reg) return; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 129 | for (const note of await reg.getNotifications({ tag: roomTag(roomId) })) note.close(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 130 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 131 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 132 | function setBadge(total: number): void { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 133 | const nav = navigator as Navigator & { setAppBadge?: (n: number) => Promise<void>; clearAppBadge?: () => Promise<void> }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 134 | if (total > 0) void nav.setAppBadge?.(total).catch(() => undefined); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 135 | else void nav.clearAppBadge?.().catch(() => undefined); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 136 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 137 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 138 | /** Watches room summaries for notifying unread increases, shows and clears notifications, and keeps the app badge current. */ |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 139 | export function startNotifications(openRoom: (roomId: string) => void): () => void { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 140 | let previous = new Map<string, RoomSummary>(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 141 | let notified = new Set<string>(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 142 | let badge = -1; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 143 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 144 | const unsubscribeSession = sessionUser.subscribe((account) => { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 145 | const enabled = readPreference(account); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 146 | notificationsEnabled.set(enabled); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 147 | if (account) void syncPush(account, enabled); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 148 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 149 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 150 | void registration().then(async (reg) => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 151 | for (const note of (await reg?.getNotifications()) ?? []) { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 152 | const roomId = (note.data as { roomId?: unknown } | null)?.roomId; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 153 | if (typeof roomId === 'string') notified.add(roomId); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 154 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 155 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 156 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 157 | const watched = (roomId: string) => |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 158 | isRoomWatched(roomId, get(selectedRoomId), document.visibilityState === 'visible', document.hasFocus()); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 159 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 160 | const clearRead = (list: RoomSummary[]) => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 161 | for (const room of list) { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 162 | if (notified.has(room.room_id) && (notifyingCount(room) === 0 || watched(room.room_id))) { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 163 | notified.delete(room.room_id); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 164 | void closeRoom(room.room_id); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 165 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 166 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 167 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 168 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 169 | const waiting = new Map<string, Arrival & { timer: ReturnType<typeof setTimeout> }>(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 170 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 171 | const queue = (roomId: string, mention: boolean) => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 172 | clearTimeout(waiting.get(roomId)?.timer); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 173 | const timer = setTimeout(() => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 174 | waiting.delete(roomId); |
20fe182f3204 | atagen | 2026-10-26 05:50 UTC | 175 | const room = get(effectiveRooms).find((r) => r.room_id === roomId); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 176 | if (!room || !active() || watched(roomId) || notifyingCount(room) === 0) return; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 177 | if (get(pushActive)) return; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 178 | const source = sourceFromTimeline(get(timelines)[roomId]) ?? sourceFromSummary(room); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 179 | notified.add(roomId); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 180 | void show(planNotification(room, source, { mention }), openRoom); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 181 | }, SETTLE_MS); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 182 | waiting.set(roomId, { mention, timer }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 183 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 184 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 185 | const onRooms = (list: RoomSummary[]) => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 186 | const next = new Map(list.map((room) => [room.room_id, room])); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 187 | const prior = previous; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 188 | previous = next; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 189 | if (!active()) { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 190 | if (badge !== 0) setBadge((badge = 0)); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 191 | return; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 192 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 193 | const total = badgeTotal(list); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 194 | if (total !== badge) setBadge((badge = total)); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 195 | const now = Date.now(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 196 | for (const room of list) { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 197 | const arrival = arrivalIn(prior.get(room.room_id), room, now); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 198 | if (!arrival || watched(room.room_id)) continue; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 199 | queue(room.room_id, arrival.mention || (waiting.get(room.room_id)?.mention ?? false)); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 200 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 201 | clearRead(list); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 202 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 203 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 204 | const unsubscribeSound = startMessageSound(); |
20fe182f3204 | atagen | 2026-10-26 05:50 UTC | 205 | const unsubscribeRooms = effectiveRooms.subscribe(onRooms); |
20fe182f3204 | atagen | 2026-10-26 05:50 UTC | 206 | const unsubscribeEnabled = notificationsEnabled.subscribe(() => onRooms(get(effectiveRooms))); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 207 | const onAttention = () => { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 208 | notificationPermission.set(supported ? Notification.permission : 'unsupported'); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 209 | if (supported && Notification.permission !== 'granted') pushActive.set(false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 210 | clearRead(get(effectiveRooms)); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 211 | const open = get(selectedRoomId); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 212 | if (open && get(pushActive) && watched(open)) void closeRoom(open); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 213 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 214 | const onMessage = (ev: MessageEvent) => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 215 | const data = ev.data as { type?: unknown; roomId?: unknown } | null; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 216 | const account = get(sessionUser); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 217 | if (data?.type === PUSH_CHANGED_MESSAGE && account) void syncPush(account, get(notificationsEnabled)); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 218 | if (data?.type === OPEN_ROOM_MESSAGE && typeof data.roomId === 'string') openRoom(data.roomId); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 219 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 220 | window.addEventListener('focus', onAttention); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 221 | document.addEventListener('visibilitychange', onAttention); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 222 | const unsubscribeSelected = selectedRoomId.subscribe(onAttention); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 223 | if ('serviceWorker' in navigator) navigator.serviceWorker.addEventListener('message', onMessage); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 224 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 225 | return () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 226 | unsubscribeSession(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 227 | unsubscribeRooms(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 228 | unsubscribeSound(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 229 | unsubscribeEnabled(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 230 | unsubscribeSelected(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 231 | for (const { timer } of waiting.values()) clearTimeout(timer); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 232 | window.removeEventListener('focus', onAttention); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 233 | document.removeEventListener('visibilitychange', onAttention); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 234 | if ('serviceWorker' in navigator) navigator.serviceWorker.removeEventListener('message', onMessage); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 235 | }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 236 | } |