2023-05-24 21:36:01 +02:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
|
|
|
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from "workbox-precaching";
|
|
|
|
import { NavigationRoute, registerRoute } from "workbox-routing";
|
|
|
|
import { NetworkFirst } from "workbox-strategies";
|
|
|
|
|
2023-06-09 20:32:34 +02:00
|
|
|
import { dbAsync } from "../src/app/db";
|
2023-05-31 18:27:32 +02:00
|
|
|
import { formatMessage, formatTitleWithDefault } from "../src/app/notificationUtils";
|
2023-05-24 21:36:01 +02:00
|
|
|
|
|
|
|
// See WebPushWorker, this is to play a sound on supported browsers,
|
|
|
|
// if the app is in the foreground
|
|
|
|
const broadcastChannel = new BroadcastChannel("web-push-broadcast");
|
|
|
|
|
|
|
|
self.addEventListener("install", () => {
|
|
|
|
console.log("[ServiceWorker] Installed");
|
|
|
|
self.skipWaiting();
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener("activate", () => {
|
|
|
|
console.log("[ServiceWorker] Activated");
|
|
|
|
self.skipWaiting();
|
|
|
|
});
|
|
|
|
|
|
|
|
// There's no good way to test this, and Chrome doesn't seem to implement this,
|
|
|
|
// so leaving it for now
|
|
|
|
self.addEventListener("pushsubscriptionchange", (event) => {
|
|
|
|
console.log("[ServiceWorker] PushSubscriptionChange");
|
|
|
|
console.log(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener("push", (event) => {
|
|
|
|
// server/types.go webPushPayload
|
|
|
|
const data = event.data.json();
|
2023-05-31 18:27:32 +02:00
|
|
|
console.log("[ServiceWorker] Received Web Push Event", { event, data });
|
2023-05-24 21:36:01 +02:00
|
|
|
|
|
|
|
event.waitUntil(
|
|
|
|
(async () => {
|
2023-06-02 14:45:05 +02:00
|
|
|
if (data.event === "subscription_expiring") {
|
|
|
|
await self.registration.showNotification("Notifications will be paused", {
|
|
|
|
body: "Open ntfy to continue receiving notifications",
|
|
|
|
icon: "/static/images/ntfy.png",
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
} else if (data.event === "message") {
|
|
|
|
const { subscription_id: subscriptionId, message } = data;
|
|
|
|
broadcastChannel.postMessage(message);
|
|
|
|
|
2023-06-09 20:32:34 +02:00
|
|
|
const db = await dbAsync();
|
2023-06-07 21:40:35 +02:00
|
|
|
const image = message.attachment?.name.match(/\.(png|jpe?g|gif|webp)$/i) ? message.attachment.url : undefined;
|
|
|
|
|
2023-06-08 20:12:41 +02:00
|
|
|
const actions = message.actions
|
|
|
|
?.filter(({ action }) => action === "view" || action === "http")
|
|
|
|
.map(({ label }) => ({
|
|
|
|
action: label,
|
|
|
|
title: label,
|
|
|
|
}));
|
|
|
|
|
2023-06-02 14:45:05 +02:00
|
|
|
await Promise.all([
|
|
|
|
(async () => {
|
|
|
|
await db.notifications.add({
|
|
|
|
...message,
|
|
|
|
subscriptionId,
|
|
|
|
// New marker (used for bubble indicator); cannot be boolean; Dexie index limitation
|
|
|
|
new: 1,
|
|
|
|
});
|
|
|
|
const badgeCount = await db.notifications.where({ new: 1 }).count();
|
|
|
|
console.log("[ServiceWorker] Setting new app badge count", { badgeCount });
|
|
|
|
self.navigator.setAppBadge?.(badgeCount);
|
|
|
|
})(),
|
|
|
|
db.subscriptions.update(subscriptionId, {
|
|
|
|
last: message.id,
|
|
|
|
}),
|
2023-06-07 21:40:35 +02:00
|
|
|
// Please update the desktop notification in Notifier.js to match any changes
|
2023-06-02 14:45:05 +02:00
|
|
|
self.registration.showNotification(formatTitleWithDefault(message, message.topic), {
|
|
|
|
tag: subscriptionId,
|
|
|
|
body: formatMessage(message),
|
2023-06-07 21:40:35 +02:00
|
|
|
icon: image ?? "/static/images/ntfy.png",
|
|
|
|
image,
|
2023-06-02 14:45:05 +02:00
|
|
|
data,
|
2023-06-07 21:40:35 +02:00
|
|
|
timestamp: message.time * 1_000,
|
2023-06-08 20:12:41 +02:00
|
|
|
actions,
|
2023-06-02 14:45:05 +02:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
// We can't ignore the push, since permission can be revoked by the browser
|
|
|
|
await self.registration.showNotification("Unknown notification received from server", {
|
|
|
|
body: "You may need to update ntfy by opening the web app",
|
2023-05-24 21:36:01 +02:00
|
|
|
icon: "/static/images/ntfy.png",
|
|
|
|
data,
|
2023-06-02 14:45:05 +02:00
|
|
|
});
|
|
|
|
}
|
2023-05-24 21:36:01 +02:00
|
|
|
})()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
2023-06-07 21:40:35 +02:00
|
|
|
console.log("[ServiceWorker] NotificationClick");
|
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
event.waitUntil(
|
|
|
|
(async () => {
|
|
|
|
const clients = await self.clients.matchAll({ type: "window" });
|
|
|
|
|
2023-06-02 14:45:05 +02:00
|
|
|
const rootUrl = new URL(self.location.origin);
|
2023-05-24 21:36:01 +02:00
|
|
|
const rootClient = clients.find((client) => client.url === rootUrl.toString());
|
|
|
|
|
2023-06-08 20:12:41 +02:00
|
|
|
if (event.notification.data?.event !== "message") {
|
2023-06-02 14:45:05 +02:00
|
|
|
if (rootClient) {
|
|
|
|
rootClient.focus();
|
|
|
|
} else {
|
|
|
|
self.clients.openWindow(rootUrl);
|
|
|
|
}
|
2023-06-08 20:12:41 +02:00
|
|
|
event.notification.close();
|
2023-06-02 14:45:05 +02:00
|
|
|
} else {
|
|
|
|
const { message } = event.notification.data;
|
|
|
|
|
2023-06-08 20:12:41 +02:00
|
|
|
if (event.action) {
|
|
|
|
const action = event.notification.data.message.actions.find(({ label }) => event.action === label);
|
|
|
|
|
|
|
|
if (action.action === "view") {
|
|
|
|
self.clients.openWindow(action.url);
|
|
|
|
|
|
|
|
if (action.clear) {
|
|
|
|
event.notification.close();
|
|
|
|
}
|
|
|
|
} else if (action.action === "http") {
|
|
|
|
try {
|
|
|
|
const response = await fetch(action.url, {
|
|
|
|
method: action.method ?? "POST",
|
|
|
|
headers: action.headers ?? {},
|
|
|
|
body: action.body,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP ${response.status} ${response.statusText}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.clear) {
|
|
|
|
event.notification.close();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("[ServiceWorker] Error performing http action", e);
|
|
|
|
self.registration.showNotification(`Unsuccessful action ${action.label} (${action.action})`, {
|
|
|
|
body: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (message.click) {
|
2023-06-02 14:45:05 +02:00
|
|
|
self.clients.openWindow(message.click);
|
|
|
|
|
2023-06-08 20:12:41 +02:00
|
|
|
event.notification.close();
|
2023-06-02 14:45:05 +02:00
|
|
|
} else {
|
2023-06-08 20:12:41 +02:00
|
|
|
const topicUrl = new URL(message.topic, self.location.origin);
|
|
|
|
const topicClient = clients.find((client) => client.url === topicUrl.toString());
|
|
|
|
|
|
|
|
if (topicClient) {
|
|
|
|
topicClient.focus();
|
|
|
|
} else if (rootClient) {
|
|
|
|
rootClient.focus();
|
|
|
|
} else {
|
|
|
|
self.clients.openWindow(topicUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
event.notification.close();
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 21:36:01 +02:00
|
|
|
})()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// self.__WB_MANIFEST is default injection point
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
precacheAndRoute(self.__WB_MANIFEST);
|
|
|
|
|
|
|
|
// clean old assets
|
|
|
|
cleanupOutdatedCaches();
|
|
|
|
|
|
|
|
// to allow work offline
|
2023-05-31 18:27:32 +02:00
|
|
|
if (import.meta.env.MODE !== "development") {
|
|
|
|
registerRoute(new NavigationRoute(createHandlerBoundToURL("/")));
|
|
|
|
registerRoute(({ url }) => url.pathname === "/config.js", new NetworkFirst());
|
|
|
|
}
|