Reformat code
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 28 Oct 2024 11:49:26 +0000 (12:49 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 28 Oct 2024 11:49:26 +0000 (12:49 +0100)
wcfsetup/install/files/service-worker/index.php

index 034c1a52f8d82c32c3415832ce71012bbb7eca2b..b3232835b568370a09c8ed01212adff50efd429d 100644 (file)
@@ -19,28 +19,30 @@ self.addEventListener("push", (event) => {
 
        const payload = event.data.json();
 
-       getLastNotificationTimestamp().then(lastNotificationTimestamp => {
-    if (!lastNotificationTimestamp || payload.time < lastNotificationTimestamp) {
-      return;
-    }
-
-    event.waitUntil(
-      removeOldNotifications(payload.notificationID, payload.time).then(() =>
-        self.registration.showNotification(payload.title, {
-          body: payload.message,
-          icon: payload.icon,
-          timestamp: payload.time * 1000,
-          tag: payload.notificationID,
-          data: {
-            url: payload.url,
-            time: payload.time,
-          },
-        }),
-      ).then(() => {
-        sendToClients(payload);
-      }),
-    );
-  });
+       getLastNotificationTimestamp().then((lastNotificationTimestamp) => {
+               if (!lastNotificationTimestamp || payload.time < lastNotificationTimestamp) {
+                       return;
+               }
+
+               event.waitUntil(
+                       removeOldNotifications(payload.notificationID, payload.time)
+                               .then(() =>
+                                       self.registration.showNotification(payload.title, {
+                                               body: payload.message,
+                                               icon: payload.icon,
+                                               timestamp: payload.time * 1000,
+                                               tag: payload.notificationID,
+                                               data: {
+                                                       url: payload.url,
+                                                       time: payload.time,
+                                               },
+                                       }),
+                               )
+                               .then(() => {
+                                       sendToClients(payload);
+                               }),
+               );
+       });
 });
 
 self.addEventListener("notificationclick", (event) => {
@@ -49,13 +51,13 @@ self.addEventListener("notificationclick", (event) => {
        event.waitUntil(self.clients.openWindow(event.notification.data.url));
 });
 
-self.addEventListener('message', event => {
-  if (event.data && event.data.type === 'SAVE_LAST_NOTIFICATION_TIMESTAMP') {
-    saveLastNotificationTimestamp(event.data.timestamp);
-  }
+self.addEventListener("message", (event) => {
+       if (event.data && event.data.type === "SAVE_LAST_NOTIFICATION_TIMESTAMP") {
+               saveLastNotificationTimestamp(event.data.timestamp);
+       }
 });
 
-async function sendToClients(payload){
+async function sendToClients(payload) {
        const allClients = await self.clients.matchAll({
                includeUncontrolled: true,
                type: "window",
@@ -87,72 +89,74 @@ async function removeOldNotifications(notificationID, time) {
  * IndexedDB functions to store the last notification timestamp.
  */
 function openDatabase() {
-  return new Promise((resolve, reject) => {
-    const request = indexedDB.open('WOLTLAB_SUITE_CORE', 1);
-
-    request.onupgradeneeded = (event) => {
-      const db = event.target.result;
-      if (!db.objectStoreNames.contains('notifications')) {
-        db.createObjectStore('notifications');
-      }
-    };
-
-    request.onsuccess = (event) => {
-      resolve(event.target.result);
-
-      if (!db.objectStoreNames.contains('notifications')) {
-        db.createObjectStore('notifications');
-      }
-    };
-
-    request.onerror = (event) => {
-      reject('Database error: ' + event.target.errorCode);
-    };
-  });
+       return new Promise((resolve, reject) => {
+               const request = indexedDB.open("WOLTLAB_SUITE_CORE", 1);
+
+               request.onupgradeneeded = (event) => {
+                       const db = event.target.result;
+                       if (!db.objectStoreNames.contains("notifications")) {
+                               db.createObjectStore("notifications");
+                       }
+               };
+
+               request.onsuccess = (event) => {
+                       resolve(event.target.result);
+
+                       if (!db.objectStoreNames.contains("notifications")) {
+                               db.createObjectStore("notifications");
+                       }
+               };
+
+               request.onerror = (event) => {
+                       reject("Database error: " + event.target.errorCode);
+               };
+       });
 }
 
 function saveLastNotificationTimestamp(timestamp) {
-  if (!timestamp || timestamp <= 0) {
-    return;
-  }
-
-  openDatabase().then(db => {
-    const tx = db.transaction('notifications', 'readwrite');
-    const store = tx.objectStore('notifications');
-    const getRequest = store.get('lastNotification');
-
-    getRequest.onsuccess = () => {
-      const storedTimestamp = getRequest.result;
-
-      // Check if the new timestamp is greater than the stored timestamp
-      if (storedTimestamp === undefined || timestamp > storedTimestamp) {
-        store.put(timestamp, 'lastNotification');
-      }
-    };
-
-    getRequest.onerror = () => {
-      console.error('Failed to retrieve timestamp', getRequest.error);
-    };
-
-    tx.onerror = () => {
-      console.error('Transaction error', tx.error);
-    };
-  }).catch(err => console.error('Failed to open database', err));
+       if (!timestamp || timestamp <= 0) {
+               return;
+       }
+
+       openDatabase()
+               .then((db) => {
+                       const tx = db.transaction("notifications", "readwrite");
+                       const store = tx.objectStore("notifications");
+                       const getRequest = store.get("lastNotification");
+
+                       getRequest.onsuccess = () => {
+                               const storedTimestamp = getRequest.result;
+
+                               // Check if the new timestamp is greater than the stored timestamp
+                               if (storedTimestamp === undefined || timestamp > storedTimestamp) {
+                                       store.put(timestamp, "lastNotification");
+                               }
+                       };
+
+                       getRequest.onerror = () => {
+                               console.error("Failed to retrieve timestamp", getRequest.error);
+                       };
+
+                       tx.onerror = () => {
+                               console.error("Transaction error", tx.error);
+                       };
+               })
+               .catch((err) => console.error("Failed to open database", err));
 }
 
 function getLastNotificationTimestamp() {
-  return openDatabase().then(db => {
-    return new Promise((resolve, reject) => {
-      const tx = db.transaction('notifications', 'readonly');
-      const store = tx.objectStore('notifications');
-      const request = store.get('lastNotification');
-
-      request.onsuccess = () => {
-        resolve(request.result);
-      };
-      request.onerror = () => {
-        reject('Failed to retrieve timestamp');
-      };
-    });
-  });
+       return openDatabase().then((db) => {
+               return new Promise((resolve, reject) => {
+                       const tx = db.transaction("notifications", "readonly");
+                       const store = tx.objectStore("notifications");
+                       const request = store.get("lastNotification");
+
+                       request.onsuccess = () => {
+                               resolve(request.result);
+                       };
+                       request.onerror = () => {
+                               reject("Failed to retrieve timestamp");
+                       };
+               });
+       });
 }