2024-07-22 22:39:15 +02:00
|
|
|
// Notifications Worker
|
|
|
|
|
|
|
|
self.addEventListener("push", function (event) {
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
if (event.data) {
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
const data = event.data.json();
|
2024-08-05 14:56:57 +02:00
|
|
|
|
|
|
|
let actions = [];
|
|
|
|
|
|
|
|
switch (data.type ?? "unknown") {
|
|
|
|
case "alert":
|
|
|
|
actions = [
|
|
|
|
{
|
|
|
|
action: "markReviewed",
|
|
|
|
title: "Mark as Reviewed",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-22 22:39:15 +02:00
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
self.registration.showNotification(data.title, {
|
|
|
|
body: data.message,
|
|
|
|
icon: "/images/maskable-icon.png",
|
|
|
|
image: data.image,
|
|
|
|
badge: "/images/maskable-badge.png",
|
|
|
|
tag: data.id,
|
|
|
|
data: { id: data.id, link: data.direct_url },
|
2024-08-05 14:56:57 +02:00
|
|
|
actions,
|
2024-07-22 22:39:15 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// pass
|
|
|
|
// This push event has no data
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
if (event.notification) {
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
event.notification.close();
|
|
|
|
|
2024-08-05 14:56:57 +02:00
|
|
|
switch (event.action ?? "default") {
|
|
|
|
case "markReviewed":
|
|
|
|
if (event.notification.data) {
|
|
|
|
fetch("/api/reviews/viewed", {
|
|
|
|
method: "POST",
|
|
|
|
headers: { "Content-Type": "application/json", "X-CSRF-TOKEN": 1 },
|
|
|
|
body: JSON.stringify({ ids: [event.notification.data.id] }),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// @ts-expect-error we know this exists
|
|
|
|
if (event.notification.data) {
|
|
|
|
const url = event.notification.data.link;
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
if (clients.openWindow) {
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
return clients.openWindow(url);
|
|
|
|
}
|
|
|
|
}
|
2024-07-22 22:39:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|