235ce394f90d | atagen | 2026-10-26 06:54 UTC | 1 | import { writable } from 'svelte/store'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 2 | import { runCommand } from './api'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 3 | import { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 4 | DEVICE_PUSH_APP_ID, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 5 | devicePusherRequest, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 6 | parseStoredPush, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 7 | stalePushers, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 8 | subscriptionKeys, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 9 | syncAction, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 10 | urlBase64ToBytes, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 11 | type PushSubscriptionKeys, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 12 | type StoredPush |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 13 | } from './push-model'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 14 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 15 | /** True while this browser holds a subscription the server knows about, so in-app notifications stand down. */ |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 16 | export const pushActive = writable(false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 17 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 18 | const supported = typeof window !== 'undefined' && 'serviceWorker' in navigator && 'PushManager' in window; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 19 | const storageKey = (account: string) => `malice.push.${account}`; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 20 | const browserKey = 'malice.push.browser'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 21 | const vapidPath = __MALICE_DEVICE__ ? '/push/vapid' : '/api/push/vapid'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 22 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 23 | let queue: Promise<unknown> = Promise.resolve(); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 24 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 25 | /** Enable, disable and launch checks all touch one subscription, so they run one at a time. */ |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 26 | function serial<T>(task: () => Promise<T>): Promise<T> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 27 | const run = queue.then(task, task); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 28 | queue = run.catch(() => undefined); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 29 | return run; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 30 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 31 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 32 | function browserId(): string { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 33 | try { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 34 | const existing = localStorage.getItem(browserKey); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 35 | if (existing) return existing; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 36 | const id = crypto.randomUUID(); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 37 | localStorage.setItem(browserKey, id); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 38 | return id; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 39 | } catch { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 40 | return 'unknown'; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 41 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 42 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 43 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 44 | function readStored(account: string): StoredPush | null { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 45 | try { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 46 | return parseStoredPush(localStorage.getItem(storageKey(account))); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 47 | } catch { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 48 | return null; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 49 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 50 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 51 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 52 | function writeStored(account: string, stored: StoredPush | null): void { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 53 | try { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 54 | if (stored) localStorage.setItem(storageKey(account), JSON.stringify(stored)); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 55 | else localStorage.removeItem(storageKey(account)); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 56 | } catch {} |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 57 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 58 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 59 | async function registration(): Promise<ServiceWorkerRegistration | null> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 60 | if (!supported) return null; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 61 | return (await navigator.serviceWorker.getRegistration()) ?? null; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 62 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 63 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 64 | async function applicationServerKey(): Promise<Uint8Array<ArrayBuffer>> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 65 | const res = await fetch(vapidPath, { credentials: 'same-origin' }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 66 | if (!res.ok) throw new Error('push key unavailable'); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 67 | const body: unknown = await res.json().catch(() => null); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 68 | const key = typeof body === 'string' ? body : (body as { public_key?: unknown } | null)?.public_key; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 69 | if (typeof key !== 'string') throw new Error('push key malformed'); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 70 | return urlBase64ToBytes(key); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 71 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 72 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 73 | async function post(path: string, body: unknown): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 74 | const res = await fetch(path, { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 75 | method: 'POST', |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 76 | credentials: 'same-origin', |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 77 | headers: { 'content-type': 'application/json' }, |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 78 | body: JSON.stringify(body) |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 79 | }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 80 | if (!res.ok) throw new Error(`push request failed (${res.status})`); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 81 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 82 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 83 | async function removeRemote(stored: StoredPush): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 84 | if (__MALICE_DEVICE__) { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 85 | if (stored.pushkey) await runCommand({ op: 'remove_pusher', app_id: DEVICE_PUSH_APP_ID, pushkey: stored.pushkey }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 86 | } else { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 87 | await post('/api/push/unsubscribe', { endpoint: stored.endpoint }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 88 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 89 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 90 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 91 | async function register(account: string, keys: PushSubscriptionKeys): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 92 | const previous = readStored(account); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 93 | if (__MALICE_DEVICE__) { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 94 | await runCommand(devicePusherRequest(keys, window.location.origin, navigator.language, browserId())); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 95 | if (previous?.pushkey && previous.pushkey !== keys.p256dh) { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 96 | await runCommand({ op: 'remove_pusher', app_id: DEVICE_PUSH_APP_ID, pushkey: previous.pushkey }).catch(() => undefined); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 97 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 98 | } else { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 99 | await post('/api/push/subscribe', { endpoint: keys.endpoint, keys: { p256dh: keys.p256dh, auth: keys.auth } }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 100 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 101 | writeStored(account, { endpoint: keys.endpoint, pushkey: keys.p256dh }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 102 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 103 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 104 | async function dropStale(endpoint: string): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 105 | const reply = await runCommand({ op: 'pushers' }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 106 | for (const pushkey of stalePushers(reply, browserId(), endpoint)) { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 107 | await runCommand({ op: 'remove_pusher', app_id: DEVICE_PUSH_APP_ID, pushkey }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 108 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 109 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 110 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 111 | async function subscribe(reg: ServiceWorkerRegistration): Promise<PushSubscription> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 112 | return reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: await applicationServerKey() }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 113 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 114 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 115 | async function activate(account: string, sub: PushSubscription): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 116 | const keys = subscriptionKeys(sub); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 117 | if (!keys) throw new Error('subscription has no keys'); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 118 | await register(account, keys); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 119 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 120 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 121 | /** Runs inside the settings toggle after permission is granted. */ |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 122 | export function enablePush(account: string): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 123 | return serial(async () => { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 124 | try { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 125 | const reg = await registration(); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 126 | if (!reg) return; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 127 | await activate(account, (await reg.pushManager.getSubscription()) ?? (await subscribe(reg))); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 128 | pushActive.set(true); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 129 | } catch { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 130 | pushActive.set(false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 131 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 132 | }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 133 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 134 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 135 | /** Removes the server side registration and the browser subscription. Used by the toggle and before logout. */ |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 136 | export function disablePush(account: string): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 137 | return serial(async () => { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 138 | pushActive.set(false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 139 | const stored = readStored(account); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 140 | if (stored) await removeRemote(stored).catch(() => undefined); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 141 | writeStored(account, null); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 142 | const sub = await (await registration())?.pushManager.getSubscription(); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 143 | await sub?.unsubscribe().catch(() => false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 144 | }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 145 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 146 | |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 147 | /** Launch check. iOS revokes subscriptions after missed notifications and push services rotate endpoints. */ |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 148 | export function syncPush(account: string, wanted: boolean): Promise<void> { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 149 | return serial(async () => { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 150 | try { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 151 | const reg = supported && Notification.permission === 'granted' ? await registration() : null; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 152 | if (!reg) { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 153 | pushActive.set(false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 154 | return; |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 155 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 156 | let sub = await reg.pushManager.getSubscription(); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 157 | const action = syncAction(wanted, sub?.endpoint ?? null, readStored(account)); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 158 | if (action === 'subscribe') sub = await subscribe(reg); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 159 | if (action !== 'none' && sub) await activate(account, sub); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 160 | pushActive.set(wanted && sub !== null); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 161 | if (__MALICE_DEVICE__ && wanted && sub) await dropStale(sub.endpoint).catch(() => undefined); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 162 | } catch { |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 163 | pushActive.set(false); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 164 | } |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 165 | }); |
235ce394f90d | atagen | 2026-10-26 06:54 UTC | 166 | } |