feat: defer Web Push updates until tomorrow (Closes #563)
CI / lint (pull_request) Successful in 1m16s
CI / build-release (pull_request) Successful in 5s
CI / release-candidate (pull_request) Has been skipped

This commit is contained in:
timmy committed 2026-08-11 10:25:56 +00:00
1 parent f4a4ed84c3
commit 7ce352553d
5 files changed
+213 -5

No files matched your search

+34 -1
View File
@@ -289,7 +289,10 @@ self.addEventListener('push', event => {
&& route === '#/my-work/update/' + notificationId
&& tag === 'stackchain-update-' + notificationId
) {
options.actions = [{ action: 'mark-read', title: 'Mark read' }];
options.actions = [
{ action: 'mark-read', title: 'Mark read' },
{ action: 'tomorrow', title: 'Tomorrow' },
];
options.data.notificationId = notificationId;
}
event.waitUntil(self.registration.showNotification('New work update', options));
@@ -306,6 +309,36 @@ async function openWorkRoute(route) {
self.addEventListener('notificationclick', event => {
const route = String(event.notification.data?.route || '');
if (event.action === 'tomorrow') {
const notificationId = Number(event.notification.data?.notificationId);
if (
!Number.isSafeInteger(notificationId) || notificationId <= 0
|| route !== '#/my-work/update/' + notificationId
|| event.notification.tag !== 'stackchain-update-' + notificationId
) return;
event.waitUntil((async () => {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), PUSH_ACTION_TIMEOUT_MS);
try {
const wake = new Date(self.__STACKCHAIN_NOW?.() || Date.now());
wake.setDate(wake.getDate() + 1);
wake.setHours(9, 0, 0, 0);
await fetchJson(BASE + 'api/v1/notifications/' + notificationId + '/later', {
method: 'PATCH',
headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({ wake_at: wake.toISOString() }),
signal: controller.signal,
});
event.notification.close();
} catch (_error) {
await openWorkRoute(route);
event.notification.close();
} finally {
clearTimeout(timeout);
}
})());
return;
}
if (event.action === 'mark-read') {
const notificationId = Number(event.notification.data?.notificationId);
if (