This repository has been archived on 2026-10-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Sable/src/sw.ts
T
Evie Gauthier c996e5e31b fix(notifications): iOS reliability, SW click handling, and account switching
- HandleNotificationClick moved outside ClientRoot loading gate so SW postMessages
  are never dropped during account-switch reloads
- iOS Safari event.source=null guard removed; type-check used instead
- ToRoomEvent simplified: writes pendingNotificationAtom + sets active session,
  delegates navigation to NotificationJumper
- NotificationJumper: jumpingRef prevents repeated navigation; stable performJumpRef
  avoids re-registering listeners on every atom change during account switch
- BackgroundNotifications: uses SW showNotification for reliable tap routing on iOS;
  dedupeId is now userId+eventId scoped; visibility guard prevents backgrounded
  in-app popups; ref pattern avoids stale closure on settings
- SW: persist/restore notification settings across iOS SW restarts via Cache API;
  group notifications by room (tag + renotify); prefer navigate() over postMessage
  when available; prefer visible tab on notificationclick
- pushNotification: resolveSilent respects tweaks.sound; recipientId in title;
  fallbackNotification removed (redundant)
- RoomTimeline: use ref for handleOpenEvent to stop re-pulling on every new message
2026-03-04 22:24:19 -05:00

365 lines
11 KiB
TypeScript

/// <reference lib="WebWorker" />
// eslint-disable-next-line import-x/no-extraneous-dependencies
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching';
import { EventType } from 'matrix-js-sdk/lib/@types/event';
import { createPushNotifications } from './sw/pushNotification';
export type {};
declare const self: ServiceWorkerGlobalScope;
let notificationSoundEnabled = true;
let preferPushOnMobile = false;
let showMessageContent = false;
let showEncryptedMessageContent = false;
const { handlePushNotificationPushData } = createPushNotifications(self, () => ({
notificationSoundEnabled,
showMessageContent,
showEncryptedMessageContent,
}));
/** Cache key used to persist notification settings across SW restarts (iOS kills the SW frequently). */
const SW_SETTINGS_CACHE = 'sable-sw-settings-v1';
const SW_SETTINGS_URL = '/sw-settings-meta';
async function persistSettings() {
try {
const cache = await self.caches.open(SW_SETTINGS_CACHE);
await cache.put(
SW_SETTINGS_URL,
new Response(
JSON.stringify({
notificationSoundEnabled,
preferPushOnMobile,
showMessageContent,
showEncryptedMessageContent,
}),
{ headers: { 'Content-Type': 'application/json' } }
)
);
} catch {
// Ignore — caches may be unavailable in some environments.
}
}
async function loadPersistedSettings() {
try {
const cache = await self.caches.open(SW_SETTINGS_CACHE);
const response = await cache.match(SW_SETTINGS_URL);
if (!response) return;
const s = await response.json();
if (typeof s.notificationSoundEnabled === 'boolean')
notificationSoundEnabled = s.notificationSoundEnabled;
if (typeof s.preferPushOnMobile === 'boolean') preferPushOnMobile = s.preferPushOnMobile;
if (typeof s.showMessageContent === 'boolean') showMessageContent = s.showMessageContent;
if (typeof s.showEncryptedMessageContent === 'boolean')
showEncryptedMessageContent = s.showEncryptedMessageContent;
} catch {
// Ignore — stale or missing cache is fine; we fall back to defaults.
}
}
type SessionInfo = {
accessToken: string;
baseUrl: string;
};
/**
* Store session per client (tab)
*/
const sessions = new Map<string, SessionInfo>();
const clientToResolve = new Map<string, (value: SessionInfo | undefined) => void>();
const clientToSessionPromise = new Map<string, Promise<SessionInfo | undefined>>();
async function cleanupDeadClients() {
const activeClients = await self.clients.matchAll();
const activeIds = new Set(activeClients.map((c) => c.id));
Array.from(sessions.keys()).forEach((id) => {
if (!activeIds.has(id)) {
sessions.delete(id);
clientToResolve.delete(id);
clientToSessionPromise.delete(id);
}
});
}
function setSession(clientId: string, accessToken: unknown, baseUrl: unknown) {
if (typeof accessToken === 'string' && typeof baseUrl === 'string') {
sessions.set(clientId, { accessToken, baseUrl });
} else {
// Logout or invalid session
sessions.delete(clientId);
}
const resolveSession = clientToResolve.get(clientId);
if (resolveSession) {
resolveSession(sessions.get(clientId));
clientToResolve.delete(clientId);
clientToSessionPromise.delete(clientId);
}
}
function requestSession(client: Client): Promise<SessionInfo | undefined> {
const promise =
clientToSessionPromise.get(client.id) ??
new Promise((resolve) => {
clientToResolve.set(client.id, resolve);
client.postMessage({ type: 'requestSession' });
});
if (!clientToSessionPromise.has(client.id)) {
clientToSessionPromise.set(client.id, promise);
}
return promise;
}
async function requestSessionWithTimeout(
clientId: string,
timeoutMs = 3000
): Promise<SessionInfo | undefined> {
const client = await self.clients.get(clientId);
if (!client) return undefined;
const sessionPromise = requestSession(client);
const timeout = new Promise<undefined>((resolve) => {
setTimeout(() => resolve(undefined), timeoutMs);
});
return Promise.race([sessionPromise, timeout]);
}
self.addEventListener('install', (event: ExtendableEvent) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', (event: ExtendableEvent) => {
event.waitUntil(
(async () => {
await self.clients.claim();
await cleanupDeadClients();
})()
);
});
/**
* Receive session updates from clients
*/
self.addEventListener('message', (event: ExtendableMessageEvent) => {
const client = event.source as Client | null;
if (!client) return;
const { data } = event;
if (!data || typeof data !== 'object') return;
const { type, accessToken, baseUrl } = data as Record<string, unknown>;
if (type === 'setSession') {
setSession(client.id, accessToken, baseUrl);
event.waitUntil(cleanupDeadClients());
}
if (type === 'setNotificationSettings') {
if (
typeof (data as { notificationSoundEnabled?: unknown }).notificationSoundEnabled === 'boolean'
) {
notificationSoundEnabled = (data as { notificationSoundEnabled: boolean })
.notificationSoundEnabled;
}
if (typeof (data as { preferPushOnMobile?: unknown }).preferPushOnMobile === 'boolean') {
preferPushOnMobile = (data as { preferPushOnMobile: boolean }).preferPushOnMobile;
}
if (typeof (data as { showMessageContent?: unknown }).showMessageContent === 'boolean') {
showMessageContent = (data as { showMessageContent: boolean }).showMessageContent;
}
if (
typeof (data as { showEncryptedMessageContent?: unknown }).showEncryptedMessageContent ===
'boolean'
) {
showEncryptedMessageContent = (data as { showEncryptedMessageContent: boolean })
.showEncryptedMessageContent;
}
// Persist so settings survive SW restart (iOS kills the SW aggressively).
event.waitUntil(persistSettings());
}
});
const MEDIA_PATHS = ['/_matrix/client/v1/media/download', '/_matrix/client/v1/media/thumbnail'];
function mediaPath(url: string): boolean {
try {
const { pathname } = new URL(url);
return MEDIA_PATHS.some((p) => pathname.startsWith(p));
} catch {
return false;
}
}
function validMediaRequest(url: string, baseUrl: string): boolean {
return MEDIA_PATHS.some((p) => {
const validUrl = new URL(p, baseUrl);
return url.startsWith(validUrl.href);
});
}
function fetchConfig(token: string): RequestInit {
return {
headers: {
Authorization: `Bearer ${token}`,
},
cache: 'default',
};
}
self.addEventListener('message', (event: ExtendableMessageEvent) => {
if (event.data.type === 'togglePush') {
const token = event.data?.token;
const fetchOptions = fetchConfig(token);
event.waitUntil(
fetch(`${event.data.url}/_matrix/client/v3/pushers/set`, {
method: 'POST',
...fetchOptions,
body: JSON.stringify(event.data.pusherData),
})
);
}
});
self.addEventListener('fetch', (event: FetchEvent) => {
const { url, method } = event.request;
if (method !== 'GET' || !mediaPath(url)) return;
const { clientId } = event;
if (!clientId) return;
const session = sessions.get(clientId);
if (session) {
if (validMediaRequest(url, session.baseUrl)) {
event.respondWith(fetch(url, fetchConfig(session.accessToken)));
}
return;
}
event.respondWith(
requestSessionWithTimeout(clientId).then((s) => {
if (s && validMediaRequest(url, s.baseUrl)) {
return fetch(url, fetchConfig(s.accessToken));
}
return fetch(event.request);
})
);
});
const onPushNotification = async (event: PushEvent) => {
if (!event?.data) {
return;
}
// The SW may have been restarted by the OS (iOS is aggressive about this),
// so in-memory settings would be at their defaults. Reload from the cache.
await loadPersistedSettings();
const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
const hasVisibleClient = clients.some((client) => client.visibilityState === 'visible');
if (hasVisibleClient && !preferPushOnMobile) {
return;
}
const pushData = event.data.json();
// try {
// if (typeof pushData?.unread === 'number') {
// self.navigator.setAppBadge(pushData.unread);
// if (pushData.unread == 0) {
// self.registration
// .getNotifications()
// .then((notifications) => notifications.forEach((notification) => notification.close()));
// await navigator.clearAppBadge();
// return;
// }
// } else {
// await navigator.clearAppBadge();
// }
// } catch (_) {
// // Likely Firefox/Gecko-based and doesn't support badging API
// }
await handlePushNotificationPushData(pushData);
};
self.addEventListener('push', (event: PushEvent) => event.waitUntil(onPushNotification(event)));
self.addEventListener('notificationclick', (event: NotificationEvent) => {
event.notification.close();
const messageData = event.notification.data;
const { scope } = self.registration;
const eventType = messageData?.type as EventType | undefined;
if (!eventType) return Promise.resolve();
let targetUrl = `${scope}inbox/`;
if (
(eventType === EventType.RoomMessage || eventType === EventType.RoomMessageEncrypted) &&
messageData?.room_id &&
messageData?.event_id
) {
// Include the target user ID as ?uid= so ToRoomEvent can switch to the
// correct account before navigating. This covers client.navigate() on iOS
// Safari where postMessage is unreliable after focus().
const uidParam =
typeof messageData?.user_id === 'string'
? `?uid=${encodeURIComponent(messageData.user_id)}`
: '';
targetUrl = `${scope}to/${messageData.room_id}/${messageData.event_id}${uidParam}`;
}
if (eventType === EventType.RoomMember && messageData?.content?.membership === 'invite')
targetUrl = `${scope}inbox/invites/`;
const postMessageToClient = (client: WindowClient) => {
client.postMessage({
type: 'notificationToRoomEvent',
message: messageData,
});
};
event.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
// Prefer a visible (foreground) tab; fall back to any open window client.
const focusedClient =
clientList.find((c) => c.visibilityState === 'visible') ??
clientList.find((c): c is WindowClient => 'focus' in c);
if (focusedClient) {
return focusedClient.focus().then((wc) => {
// Prefer client.navigate() — draft API, available on Chrome/Chromium
// (all Android PWAs and desktop), unavailable on iOS Safari / Firefox.
const client = wc ?? focusedClient;
if ('navigate' in client && typeof (client as any).navigate === 'function') {
return (client as any).navigate(targetUrl);
}
// navigate() unavailable: use postMessage. The existing client (whether
// it was in the foreground or just minimized) has a live JS context by
// the time focus() resolves. HandleNotificationClick in the page will
// receive the message and dispatch the account-switch + deep link.
postMessageToClient(client);
return null;
});
}
// No existing client — open a new window. ToRoomEvent handles the route.
if (self.clients.openWindow) {
return self.clients.openWindow(targetUrl).then(() => null);
}
return null;
})
);
return Promise.resolve();
});
if (self.__WB_MANIFEST) {
precacheAndRoute(self.__WB_MANIFEST);
}
cleanupOutdatedCaches();