Add support for mark as reviewed notification action

This commit is contained in:
Nicolas Mowen 2024-08-03 18:19:25 -06:00
parent 315f26b48d
commit 005c3a7c77
2 changed files with 42 additions and 8 deletions

View File

@ -165,6 +165,7 @@ class WebPushClient(Communicator): # type: ignore[misc]
"direct_url": direct_url, "direct_url": direct_url,
"image": image, "image": image,
"id": reviewId, "id": reviewId,
"type": "alert",
} }
), ),
) )

View File

@ -1,10 +1,29 @@
// Notifications Worker // Notifications Worker
self.addEventListener("push", function (event) { self.addEventListener("push", function (event) {
console.log(`received push at ${new Date()}`)
// @ts-expect-error we know this exists // @ts-expect-error we know this exists
if (event.data) { if (event.data) {
// @ts-expect-error we know this exists // @ts-expect-error we know this exists
const data = event.data.json(); const data = event.data.json();
let actions = [];
switch (data.type ?? "unknown") {
case "alert":
actions = [
{
action: "markReviewed",
title: "Mark as Reviewed",
},
{
action: "snooze",
title: "Snooze",
},
];
break;
}
// @ts-expect-error we know this exists // @ts-expect-error we know this exists
self.registration.showNotification(data.title, { self.registration.showNotification(data.title, {
body: data.message, body: data.message,
@ -13,6 +32,7 @@ self.addEventListener("push", function (event) {
badge: "/images/maskable-badge.png", badge: "/images/maskable-badge.png",
tag: data.id, tag: data.id,
data: { id: data.id, link: data.direct_url }, data: { id: data.id, link: data.direct_url },
actions,
}); });
} else { } else {
// pass // pass
@ -26,14 +46,27 @@ self.addEventListener("notificationclick", (event) => {
// @ts-expect-error we know this exists // @ts-expect-error we know this exists
event.notification.close(); event.notification.close();
// @ts-expect-error we know this exists switch (event.action ?? "default") {
if (event.notification.data) { case "markReviewed":
const url = event.notification.data.link; if (event.notification.data) {
// eslint-disable-next-line no-undef fetch("/api/reviews/viewed", {
if (clients.openWindow) { method: "POST",
// eslint-disable-next-line no-undef body: JSON.stringify({ ids: [event.notification.data.id] }),
return clients.openWindow(url); });
} }
break;
case "snooze":
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);
}
}
} }
} }
}); });