publicKey: '{@SERVICE_WORKER_PUBLIC_KEY|encodeJS}',
serviceWorkerJsUrl: '{$__wcf->getPath('wcf')}service-worker/',
registerUrl: '{link controller="RegisterServiceWorker"}{/link}',
- lastReadNotification: {$__wcf->getUserNotificationHandler()->getLastReadNotificationTime()}
+ notificationLastReadTime: {$__wcf->getUserNotificationHandler()->getTimeOfLastReadNotification()}
},
{/if}
dynamicColorScheme: {if $__wcf->getStyleHandler()->getColorScheme() === 'system'}true{else}false{/if},
publicKey: string;
serviceWorkerJsUrl: string;
registerUrl: string;
- lastReadNotification: number;
+ notificationLastReadTime: number;
};
dynamicColorScheme: boolean;
endpointUserPopover: string;
options.serviceWorker.publicKey,
options.serviceWorker.serviceWorkerJsUrl,
options.serviceWorker.registerUrl,
- options.serviceWorker.lastReadNotification,
+ options.serviceWorker.notificationLastReadTime,
);
}
}
return outputArray;
}
- public updateLastNotificationTime(timestamp: number): void {
+ public updateNotificationLastReadTime(timestamp: number): void {
window.navigator.serviceWorker.controller?.postMessage({
- type: "SAVE_LAST_NOTIFICATION_TIMESTAMP",
- timestamp: timestamp,
+ type: "UPDATE_NOTIFICATION_LAST_READ_TIME",
+ timestamp,
});
}
}
publicKey: string,
serviceWorkerJsUrl: string,
registerUrl: string,
- lastReadNotification: number,
+ notificationLastReadTime: number,
): void {
if (!serviceWorkerSupported()) {
return;
_serviceWorker = new ServiceWorker(publicKey, serviceWorkerJsUrl, registerUrl);
if (Notification.permission === "granted") {
registerServiceWorker();
- _serviceWorker.updateLastNotificationTime(lastReadNotification);
+ _serviceWorker.updateNotificationLastReadTime(notificationLastReadTime);
}
}
void _serviceWorker?.register();
}
-export function updateLastNotificationTime(timestamp?: number): void {
- _serviceWorker?.updateLastNotificationTime(timestamp ?? Math.round(Date.now() / 1000));
+export function updateNotificationLastReadTime(timestamp?: number): void {
+ _serviceWorker?.updateNotificationLastReadTime(timestamp ?? Math.round(Date.now() / 1000));
}
import { registerProvider } from "../Manager";
import * as Language from "../../../../Language";
import { enableNotifications } from "../../../../Notification/Handler";
-import { registerServiceWorker, updateLastNotificationTime } from "../../../../Notification/ServiceWorker";
+import { registerServiceWorker, updateNotificationLastReadTime } from "../../../../Notification/ServiceWorker";
let originalFavicon = "";
function setFaviconCounter(counter: number): void {
const response = (await dboAction("markAsConfirmed", "wcf\\data\\user\\notification\\UserNotificationAction")
.objectIds([objectId])
.dispatch()) as ResponseMarkAsRead;
- updateLastNotificationTime();
+ updateNotificationLastReadTime();
this.updateCounter(response.totalCount);
}
async markAllAsRead(): Promise<void> {
await dboAction("markAllAsConfirmed", "wcf\\data\\user\\notification\\UserNotificationAction").dispatch();
- updateLastNotificationTime();
+ updateNotificationLastReadTime();
this.updateCounter(0);
}
if (User_1.default.userId) {
UiFeedDialog.setup();
if (options.serviceWorker) {
- (0, ServiceWorker_1.setup)(options.serviceWorker.publicKey, options.serviceWorker.serviceWorkerJsUrl, options.serviceWorker.registerUrl, options.serviceWorker.lastReadNotification);
+ (0, ServiceWorker_1.setup)(options.serviceWorker.publicKey, options.serviceWorker.serviceWorkerJsUrl, options.serviceWorker.registerUrl, options.serviceWorker.notificationLastReadTime);
}
}
(0, LazyLoader_1.whenFirstSeen)("woltlab-core-reaction-summary", () => {
exports.serviceWorkerSupported = serviceWorkerSupported;
exports.setup = setup;
exports.registerServiceWorker = registerServiceWorker;
- exports.updateLastNotificationTime = updateLastNotificationTime;
+ exports.updateNotificationLastReadTime = updateNotificationLastReadTime;
let _serviceWorker = null;
class ServiceWorker {
#publicKey;
}
return outputArray;
}
- updateLastNotificationTime(timestamp) {
+ updateNotificationLastReadTime(timestamp) {
window.navigator.serviceWorker.controller?.postMessage({
- type: "SAVE_LAST_NOTIFICATION_TIMESTAMP",
- timestamp: timestamp,
+ type: "UPDATE_NOTIFICATION_LAST_READ_TIME",
+ timestamp,
});
}
}
}
return true;
}
- function setup(publicKey, serviceWorkerJsUrl, registerUrl, lastReadNotification) {
+ function setup(publicKey, serviceWorkerJsUrl, registerUrl, notificationLastReadTime) {
if (!serviceWorkerSupported()) {
return;
}
_serviceWorker = new ServiceWorker(publicKey, serviceWorkerJsUrl, registerUrl);
if (Notification.permission === "granted") {
registerServiceWorker();
- _serviceWorker.updateLastNotificationTime(lastReadNotification);
+ _serviceWorker.updateNotificationLastReadTime(notificationLastReadTime);
}
}
function registerServiceWorker() {
void _serviceWorker?.register();
}
- function updateLastNotificationTime(timestamp) {
- _serviceWorker?.updateLastNotificationTime(timestamp ?? Math.round(Date.now() / 1000));
+ function updateNotificationLastReadTime(timestamp) {
+ _serviceWorker?.updateNotificationLastReadTime(timestamp ?? Math.round(Date.now() / 1000));
}
});
const response = (await (0, Ajax_1.dboAction)("markAsConfirmed", "wcf\\data\\user\\notification\\UserNotificationAction")
.objectIds([objectId])
.dispatch());
- (0, ServiceWorker_1.updateLastNotificationTime)();
+ (0, ServiceWorker_1.updateNotificationLastReadTime)();
this.updateCounter(response.totalCount);
}
async markAllAsRead() {
await (0, Ajax_1.dboAction)("markAllAsConfirmed", "wcf\\data\\user\\notification\\UserNotificationAction").dispatch();
- (0, ServiceWorker_1.updateLastNotificationTime)();
+ (0, ServiceWorker_1.updateNotificationLastReadTime)();
this.updateCounter(0);
}
updateCounter(count) {
}
/**
- * Returns the timestamp of the last read notification for the active user.
- * Or `0` if no notification has been read yet.
- *
- * @return int
+ * Returns the timestamp of the last read notification for the active user
+ * or `0` if no notification has been read yet.
*/
- public function getLastReadNotificationTime(): int
+ public function getTimeOfLastReadNotification(): int
{
$sql = "SELECT MAX(confirmTime)
FROM wcf1_user_notification
@\header('Service-Worker-Allowed: /');
@\header('Content-Type: text/javascript; charset=utf-8');
-?>
-/**
+?>/**
* @author Olaf Braun
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.1
*/
self.addEventListener("push", (event) => {
const payload = event.data.json();
- getLastNotificationTimestamp().then((lastNotificationTimestamp) => {
- if (lastNotificationTimestamp && payload.time < lastNotificationTimestamp) {
+ getTimeOfLastReadNotification().then((notificationLastReadTime) => {
+ if (notificationLastReadTime && payload.time < notificationLastReadTime) {
return;
}
});
self.addEventListener("message", (event) => {
- if (event.data && event.data.type === "SAVE_LAST_NOTIFICATION_TIMESTAMP") {
- saveLastNotificationTimestamp(event.data.timestamp);
+ if (event.data && event.data.type === "UPDATE_NOTIFICATION_LAST_READ_TIME") {
+ updateNotificationLastReadTime(event.data.timestamp);
}
});
};
request.onerror = (event) => {
- reject("Database error: %s", event.target.errorCode);
+ reject(new Error(`Database error: ${event.target.errorCode}`));
};
});
}
-function saveLastNotificationTimestamp(timestamp) {
+function updateNotificationLastReadTime(timestamp) {
if (!timestamp || timestamp <= 0) {
return;
}
.catch((err) => console.error("Failed to open database: %s", err));
}
-function getLastNotificationTimestamp() {
+function getTimeOfLastReadNotification() {
return openDatabase().then((db) => {
return new Promise((resolve, reject) => {
const tx = db.transaction("notifications", "readonly");
resolve(request.result);
};
request.onerror = () => {
- reject("Failed to retrieve timestamp");
+ reject(new Error("Failed to retrieve timestamp"));
};
});
});