Browse Source

Up to loading accounts; looping and cryptograhy next

home-view-notification-improvements
Matthew Raymer 1 year ago
parent
commit
aba0d832db
  1. 67
      src/App.vue
  2. 32
      sw_scripts/safari-notifications.js

67
src/App.vue

@ -326,62 +326,51 @@ export default class App extends Vue {
} }
private askPermission(): Promise<NotificationPermission> { private askPermission(): Promise<NotificationPermission> {
if ("serviceWorker" in navigator && navigator.serviceWorker.controller) { if (!("serviceWorker" in navigator && navigator.serviceWorker.controller)) {
return Promise.reject("Service worker not available.");
}
const secret = localStorage.getItem("secret"); const secret = localStorage.getItem("secret");
if (!secret) {
return Promise.reject("No secret found.");
}
if (secret) { return this.sendSecretToServiceWorker(secret)
.then(() => this.checkNotificationSupport())
.then(() => this.requestNotificationPermission())
.catch((error) => Promise.reject(error));
}
private sendSecretToServiceWorker(secret: string): Promise<void> {
const message: ServiceWorkerMessage = { const message: ServiceWorkerMessage = {
type: "SEND_LOCAL_DATA", type: "SEND_LOCAL_DATA",
data: secret, data: secret,
}; };
this.sendMessageToServiceWorker(message) return this.sendMessageToServiceWorker(message).then((response) => {
.then((response) => {
// The service worker has received the message and responded
console.log("Response from service worker:", response); console.log("Response from service worker:", response);
// Perform the next action here });
// Check if Notifications are supported }
private checkNotificationSupport(): Promise<void> {
if (!("Notification" in window)) { if (!("Notification" in window)) {
alert("This browser does not support notifications."); alert("This browser does not support notifications.");
return Promise.reject( return Promise.reject("This browser does not support notifications.");
"This browser does not support notifications.",
);
} }
// Check existing permissions
if (Notification.permission === "granted") { if (Notification.permission === "granted") {
return Promise.resolve("granted"); return Promise.resolve();
} }
return Promise.resolve();
// 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") { private requestNotificationPermission(): Promise<NotificationPermission> {
alert( return Notification.requestPermission().then((permission) => {
"We need notification permission to provide certain features.", if (permission !== "granted") {
); alert("We need notification permission to provide certain features.");
return Promise.reject("We weren't granted permission."); throw new Error("We weren't granted permission.");
} }
return permission;
return permissionResult;
}); });
})
.catch((error) => {
console.error("Error:", error);
});
}
}
} }
async turnOnNotifications() { async turnOnNotifications() {

32
sw_scripts/safari-notifications.js

@ -43,54 +43,48 @@ async function getSettingById(id) {
} }
function getAllAccounts() { function fetchAllAccounts() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let openRequest = indexedDB.open('TimeSafariAccounts'); let openRequest = indexedDB.open('TimeSafariAccounts');
openRequest.onupgradeneeded = (event) => { openRequest.onupgradeneeded = function(event) {
let db = event.target.result; let db = event.target.result;
if (!db.objectStoreNames.contains('accounts')) { if (!db.objectStoreNames.contains('accounts')) {
db.createObjectStore('accounts', { keyPath: 'id' }); db.createObjectStore('accounts', { keyPath: 'id' });
} }
}; };
openRequest.onsuccess = (event) => { openRequest.onsuccess = function(event) {
let db = event.target.result; let db = event.target.result;
let transaction = db.transaction('accounts', 'readonly'); let transaction = db.transaction('accounts', 'readonly');
let objectStore = transaction.objectStore('accounts'); let objectStore = transaction.objectStore('accounts');
let getAllRequest = objectStore.getAll(); let getAllRequest = objectStore.getAll();
getAllRequest.onsuccess = () => resolve(getAllRequest.result); getAllRequest.onsuccess = function() {
getAllRequest.onerror = () => reject(getAllRequest.error); 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() { async function getNotificationCount() {
let secret = null; let secret = null;
if ('secret' in self) { if ('secret' in self) {
secret = self.secret; secret = self.secret;
const secretUint8Array = self.decodeBase64(secret); const secretUint8Array = self.decodeBase64(secret);
/**
const settings = await getSettingById(1); const settings = await getSettingById(1);
const activeDid = settings['activeDid']; const activeDid = settings['activeDid'];
const accounts = await fetchAllAccounts(); const accounts = await fetchAllAccounts();
/**
let result = null; let result = null;
for (var i = 0; i < accounts.length; i++) { for (var i = 0; i < accounts.length; i++) {

Loading…
Cancel
Save