From 64b38a67753cda601f72222b2a387ac930708f8d Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 28 Oct 2024 14:32:03 +0100 Subject: [PATCH] Improve the consistency of property/function names --- .../templates/headIncludeJavaScript.tpl | 2 +- ts/WoltLabSuite/Core/BootstrapFrontend.ts | 4 ++-- .../Core/Notification/ServiceWorker.ts | 14 ++++++------- .../Core/Ui/User/Menu/Data/Notification.ts | 6 +++--- .../js/WoltLabSuite/Core/BootstrapFrontend.js | 2 +- .../Core/Notification/ServiceWorker.js | 16 +++++++-------- .../Core/Ui/User/Menu/Data/Notification.js | 4 ++-- .../UserNotificationHandler.class.php | 8 +++----- .../install/files/service-worker/index.php | 20 +++++++++---------- 9 files changed, 37 insertions(+), 39 deletions(-) diff --git a/com.woltlab.wcf/templates/headIncludeJavaScript.tpl b/com.woltlab.wcf/templates/headIncludeJavaScript.tpl index d587217e64..a607acfb5e 100644 --- a/com.woltlab.wcf/templates/headIncludeJavaScript.tpl +++ b/com.woltlab.wcf/templates/headIncludeJavaScript.tpl @@ -82,7 +82,7 @@ window.addEventListener('pageshow', function(event) { 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}, diff --git a/ts/WoltLabSuite/Core/BootstrapFrontend.ts b/ts/WoltLabSuite/Core/BootstrapFrontend.ts index 3e8ae0d61f..e23ec7592c 100644 --- a/ts/WoltLabSuite/Core/BootstrapFrontend.ts +++ b/ts/WoltLabSuite/Core/BootstrapFrontend.ts @@ -29,7 +29,7 @@ interface BootstrapOptions { publicKey: string; serviceWorkerJsUrl: string; registerUrl: string; - lastReadNotification: number; + notificationLastReadTime: number; }; dynamicColorScheme: boolean; endpointUserPopover: string; @@ -115,7 +115,7 @@ export function setup(options: BootstrapOptions): void { options.serviceWorker.publicKey, options.serviceWorker.serviceWorkerJsUrl, options.serviceWorker.registerUrl, - options.serviceWorker.lastReadNotification, + options.serviceWorker.notificationLastReadTime, ); } } diff --git a/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts b/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts index ba85f7381e..d24b8136c3 100644 --- a/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts +++ b/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts @@ -96,10 +96,10 @@ class ServiceWorker { 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, }); } } @@ -131,7 +131,7 @@ export function setup( publicKey: string, serviceWorkerJsUrl: string, registerUrl: string, - lastReadNotification: number, + notificationLastReadTime: number, ): void { if (!serviceWorkerSupported()) { return; @@ -139,7 +139,7 @@ export function setup( _serviceWorker = new ServiceWorker(publicKey, serviceWorkerJsUrl, registerUrl); if (Notification.permission === "granted") { registerServiceWorker(); - _serviceWorker.updateLastNotificationTime(lastReadNotification); + _serviceWorker.updateNotificationLastReadTime(notificationLastReadTime); } } @@ -147,6 +147,6 @@ export function registerServiceWorker(): void { 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)); } diff --git a/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.ts b/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.ts index 20a51e680a..d4dcd5119d 100644 --- a/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.ts +++ b/ts/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.ts @@ -13,7 +13,7 @@ import { EventUpdateCounter, UserMenuButton, UserMenuData, UserMenuFooter, UserM 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 { @@ -280,14 +280,14 @@ class UserMenuDataNotification implements DesktopNotifications, UserMenuProvider const response = (await dboAction("markAsConfirmed", "wcf\\data\\user\\notification\\UserNotificationAction") .objectIds([objectId]) .dispatch()) as ResponseMarkAsRead; - updateLastNotificationTime(); + updateNotificationLastReadTime(); this.updateCounter(response.totalCount); } async markAllAsRead(): Promise { await dboAction("markAllAsConfirmed", "wcf\\data\\user\\notification\\UserNotificationAction").dispatch(); - updateLastNotificationTime(); + updateNotificationLastReadTime(); this.updateCounter(0); } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js b/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js index e5eafebf94..e0b503eabc 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js @@ -78,7 +78,7 @@ define(["require", "exports", "tslib", "./BackgroundQueue", "./Bootstrap", "./Ui 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", () => { diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js index 9113f4c342..80f54f4ea4 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js @@ -11,7 +11,7 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi exports.serviceWorkerSupported = serviceWorkerSupported; exports.setup = setup; exports.registerServiceWorker = registerServiceWorker; - exports.updateLastNotificationTime = updateLastNotificationTime; + exports.updateNotificationLastReadTime = updateNotificationLastReadTime; let _serviceWorker = null; class ServiceWorker { #publicKey; @@ -88,10 +88,10 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi } 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, }); } } @@ -114,20 +114,20 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi } 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)); } }); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.js index b748337fb8..c077e3cc1c 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Menu/Data/Notification.js @@ -209,12 +209,12 @@ define(["require", "exports", "tslib", "../../../../Ajax", "../View", "../Manage 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) { diff --git a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php index f5b7fae90e..926aa9d6a0 100644 --- a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php +++ b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php @@ -1101,12 +1101,10 @@ class UserNotificationHandler extends SingletonFactory } /** - * 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 diff --git a/wcfsetup/install/files/service-worker/index.php b/wcfsetup/install/files/service-worker/index.php index 26a97172e9..369c225afd 100644 --- a/wcfsetup/install/files/service-worker/index.php +++ b/wcfsetup/install/files/service-worker/index.php @@ -2,11 +2,11 @@ @\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 + * @since 6.1 */ self.addEventListener("push", (event) => { @@ -19,8 +19,8 @@ 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; } @@ -52,8 +52,8 @@ self.addEventListener("notificationclick", (event) => { }); 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); } }); @@ -105,12 +105,12 @@ function openDatabase() { }; 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; } @@ -141,7 +141,7 @@ function saveLastNotificationTimestamp(timestamp) { .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"); @@ -152,7 +152,7 @@ function getLastNotificationTimestamp() { resolve(request.result); }; request.onerror = () => { - reject("Failed to retrieve timestamp"); + reject(new Error("Failed to retrieve timestamp")); }; }); }); -- 2.20.1