From aba0d832db04f53d0dd74a3d20a85a4305339b58 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Sun, 12 Nov 2023 05:08:18 -0500 Subject: [PATCH] Up to loading accounts; looping and cryptograhy next --- src/App.vue | 91 +++++++++++++----------------- sw_scripts/safari-notifications.js | 34 +++++------ 2 files changed, 54 insertions(+), 71 deletions(-) diff --git a/src/App.vue b/src/App.vue index f81ddfc7e..9100d997a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -326,62 +326,51 @@ export default class App extends Vue { } private askPermission(): Promise { - if ("serviceWorker" in navigator && navigator.serviceWorker.controller) { - const secret = localStorage.getItem("secret"); + if (!("serviceWorker" in navigator && navigator.serviceWorker.controller)) { + return Promise.reject("Service worker not available."); + } - if (secret) { - const message: ServiceWorkerMessage = { - type: "SEND_LOCAL_DATA", - data: secret, - }; + const secret = localStorage.getItem("secret"); + if (!secret) { + return Promise.reject("No secret found."); + } - this.sendMessageToServiceWorker(message) - .then((response) => { - // The service worker has received the message and responded - console.log("Response from service worker:", response); - // Perform the next action here - // Check if Notifications are supported - if (!("Notification" in window)) { - alert("This browser does not support notifications."); - return Promise.reject( - "This browser does not support notifications.", - ); - } + return this.sendSecretToServiceWorker(secret) + .then(() => this.checkNotificationSupport()) + .then(() => this.requestNotificationPermission()) + .catch((error) => Promise.reject(error)); + } - // Check existing permissions - if (Notification.permission === "granted") { - return Promise.resolve("granted"); - } + private sendSecretToServiceWorker(secret: string): Promise { + const message: ServiceWorkerMessage = { + type: "SEND_LOCAL_DATA", + data: secret, + }; - // Request permission - return new Promise((resolve, reject) => { - const permissionResult = Notification.requestPermission( - (result) => { - resolve(result); - }, - ); - - if (permissionResult) { - permissionResult.then(resolve, reject); - } - }).then((permissionResult) => { - console.log("Permission result:", permissionResult); - - if (permissionResult !== "granted") { - alert( - "We need notification permission to provide certain features.", - ); - return Promise.reject("We weren't granted permission."); - } - - return permissionResult; - }); - }) - .catch((error) => { - console.error("Error:", error); - }); - } + return this.sendMessageToServiceWorker(message).then((response) => { + console.log("Response from service worker:", response); + }); + } + + private checkNotificationSupport(): Promise { + if (!("Notification" in window)) { + alert("This browser does not support notifications."); + return Promise.reject("This browser does not support notifications."); + } + if (Notification.permission === "granted") { + return Promise.resolve(); } + return Promise.resolve(); + } + + private requestNotificationPermission(): Promise { + return Notification.requestPermission().then((permission) => { + if (permission !== "granted") { + alert("We need notification permission to provide certain features."); + throw new Error("We weren't granted permission."); + } + return permission; + }); } async turnOnNotifications() { diff --git a/sw_scripts/safari-notifications.js b/sw_scripts/safari-notifications.js index f48b862be..4d5d2a46f 100644 --- a/sw_scripts/safari-notifications.js +++ b/sw_scripts/safari-notifications.js @@ -43,55 +43,49 @@ async function getSettingById(id) { } -function getAllAccounts() { +function fetchAllAccounts() { return new Promise((resolve, reject) => { let openRequest = indexedDB.open('TimeSafariAccounts'); - openRequest.onupgradeneeded = (event) => { + openRequest.onupgradeneeded = function(event) { let db = event.target.result; if (!db.objectStoreNames.contains('accounts')) { db.createObjectStore('accounts', { keyPath: 'id' }); } }; - openRequest.onsuccess = (event) => { + openRequest.onsuccess = function(event) { let db = event.target.result; let transaction = db.transaction('accounts', 'readonly'); let objectStore = transaction.objectStore('accounts'); let getAllRequest = objectStore.getAll(); - getAllRequest.onsuccess = () => resolve(getAllRequest.result); - getAllRequest.onerror = () => reject(getAllRequest.error); + getAllRequest.onsuccess = function() { + resolve(getAllRequest.result); + }; + getAllRequest.onerror = function() { + reject(getAllRequest.error); + }; }; - openRequest.onerror = () => reject(openRequest.error); + openRequest.onerror = function() { + reject(openRequest.error); + }; }); } -async function fetchAllAccounts() { - try { - const accounts = await getAllAccounts(); - console.log('Accounts:', accounts); - return accounts; - - } catch (error) { - console.error('Could not get accounts:', error); - } -} - - async function getNotificationCount() { let secret = null; if ('secret' in self) { secret = self.secret; const secretUint8Array = self.decodeBase64(secret); -/** const settings = await getSettingById(1); const activeDid = settings['activeDid']; const accounts = await fetchAllAccounts(); - +/** + let result = null; for (var i = 0; i < accounts.length; i++) { let account = accounts[i];