Skip to content

Commit 2050e40

Browse files
committed
Change the last notification time when the service worker receives a notification
1 parent 7ea7477 commit 2050e40

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

ts/WoltLabSuite/Core/Notification/Handler.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as Ajax from "../Ajax";
1212
import { AjaxCallbackSetup } from "../Ajax/Data";
1313
import * as Core from "../Core";
1414
import * as EventHandler from "../Event/Handler";
15-
import { serviceWorkerSupported } from "./ServiceWorker";
15+
import { serviceWorkerSupported, updateNotificationLastReadTime } from "./ServiceWorker";
1616
import { updateCounter } from "WoltLabSuite/Core/Ui/User/Menu/Manager";
1717

1818
interface NotificationHandlerOptions {
@@ -206,6 +206,9 @@ class NotificationHandler {
206206

207207
this.lastRequestTimestamp = data.returnValues.lastRequestTimestamp;
208208

209+
// Update the last read time for the service worker
210+
updateNotificationLastReadTime(this.lastRequestTimestamp);
211+
209212
EventHandler.fire("com.woltlab.wcf.notification", "afterPoll", pollData);
210213

211214
this.showNotification(pollData);
@@ -264,6 +267,10 @@ class NotificationHandler {
264267
silent: !window.ENABLE_DEBUG_MODE,
265268
};
266269
}
270+
271+
updateLastRequestTimestamp(timestamp: number): void {
272+
this.lastRequestTimestamp = Math.max(timestamp, this.lastRequestTimestamp);
273+
}
267274
}
268275

269276
let notificationHandler: NotificationHandler;
@@ -281,6 +288,10 @@ export function enableNotifications(): void {
281288
notificationHandler!.enableNotifications();
282289
}
283290

291+
export function updateLastRequestTimestamp(timestamp: number): void {
292+
notificationHandler?.updateLastRequestTimestamp(timestamp);
293+
}
294+
284295
export function poll(): void {
285296
notificationHandler?.dispatchRequest();
286297
}

ts/WoltLabSuite/Core/Notification/ServiceWorker.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
10+
import { updateLastRequestTimestamp } from "WoltLabSuite/Core/Notification/Handler";
1011

1112
let _serviceWorker: ServiceWorker | null = null;
1213

@@ -24,6 +25,15 @@ class ServiceWorker {
2425
scope: "/",
2526
});
2627
this.#serviceWorkerRegistration = window.navigator.serviceWorker.ready;
28+
29+
window.navigator.serviceWorker.addEventListener("message", (event) => {
30+
// Validate that this is a message from our service worker
31+
if (!(event.source instanceof window.ServiceWorker) || event.source.scriptURL !== this.#serviceWorkerJsUrl) {
32+
return;
33+
}
34+
35+
updateLastRequestTimestamp(event.data.time);
36+
});
2737
}
2838

2939
async register(): Promise<void> {

wcfsetup/install/files/js/WoltLabSuite/Core/Notification/Handler.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)