Push could only reach one mobile device per user (a token file), quiet hours did not exist, and the web app had no service worker, so a closed tab never heard about finished work. This lands the shared interfaces first so onboarding can build on them: - push_subscriptions (web push endpoints and Expo tokens, one row per device) and user_notification_settings (quiet hours as minutes in the profile time zone, nudges, a per-day nudge counter), with CHECK constraints. - NotificationSettingsSchema and web push contracts, notifications.settings.get/update and notifications.webPush.config/subscribe/unsubscribe (owner-scoped, https endpoints only). - getNotificationSettings/updateNotificationSettings plus the policy and nudge-cap helpers. - VAPID keys from VAPID_PUBLIC_KEY/VAPID_PRIVATE_KEY or generated once and stored encrypted, and an SSRF-guarded web push sender built on the web-push package. - The web app links its manifest, registers a push-only service worker where supported (secure context, not the desktop app) and exposes webPushSupported/webPushEnabled/enableWebPush/ disableWebPush. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MRtGG9ARfqqhma3t85Ro2u
62 lines
2.1 KiB
JavaScript
62 lines
2.1 KiB
JavaScript
// Shows the server's web push notifications and opens the conversation they belong to.
|
|
// No fetch handler: the app always loads from the network.
|
|
|
|
self.addEventListener("install", () => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|
|
|
|
function appPath(value) {
|
|
return typeof value === "string" && value.startsWith("/") && !value.startsWith("//")
|
|
? value
|
|
: "/app";
|
|
}
|
|
|
|
self.addEventListener("push", (event) => {
|
|
event.waitUntil(
|
|
(async () => {
|
|
let data = {};
|
|
try {
|
|
data = event.data ? event.data.json() : {};
|
|
} catch {
|
|
data = {};
|
|
}
|
|
const windows = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
|
|
// The app in front of the user already shows what happened.
|
|
if (windows.some((client) => client.focused && client.visibilityState === "visible")) return;
|
|
await self.registration.showNotification(
|
|
typeof data.title === "string" && data.title ? data.title : "evOo",
|
|
{
|
|
body: typeof data.body === "string" ? data.body : "",
|
|
// Same tag as the in-app notification, so one replaces the other.
|
|
tag: typeof data.tag === "string" && data.tag ? data.tag : undefined,
|
|
icon: "/icon-192.png",
|
|
data: { url: appPath(data.url) },
|
|
},
|
|
);
|
|
})(),
|
|
);
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
event.waitUntil(
|
|
(async () => {
|
|
const target = new URL(appPath(event.notification.data?.url), self.location.origin).href;
|
|
const windows = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
|
|
const open =
|
|
windows.find((client) => client.url === target) ??
|
|
windows.find((client) => new URL(client.url).origin === self.location.origin);
|
|
if (!open) {
|
|
await self.clients.openWindow(target);
|
|
return;
|
|
}
|
|
const focused = await open.focus();
|
|
if (focused.url !== target) await focused.navigate(target).catch(() => undefined);
|
|
})(),
|
|
);
|
|
});
|