Browse Source

Up to loading accounts; looping and cryptograhy next

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

91
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)) {
const secret = localStorage.getItem("secret"); return Promise.reject("Service worker not available.");
}
if (secret) { const secret = localStorage.getItem("secret");
const message: ServiceWorkerMessage = { if (!secret) {
type: "SEND_LOCAL_DATA", return Promise.reject("No secret found.");
data: secret, }
};
this.sendMessageToServiceWorker(message) return this.sendSecretToServiceWorker(secret)
.then((response) => { .then(() => this.checkNotificationSupport())
// The service worker has received the message and responded .then(() => this.requestNotificationPermission())
console.log("Response from service worker:", response); .catch((error) => Promise.reject(error));
// 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.",
);
}
// Check existing permissions private sendSecretToServiceWorker(secret: string): Promise<void> {
if (Notification.permission === "granted") { const message: ServiceWorkerMessage = {
return Promise.resolve("granted"); type: "SEND_LOCAL_DATA",
} data: secret,
};
// Request permission return this.sendMessageToServiceWorker(message).then((response) => {
return new Promise((resolve, reject) => { console.log("Response from service worker:", response);
const permissionResult = Notification.requestPermission( });
(result) => { }
resolve(result);
}, private checkNotificationSupport(): Promise<void> {
); if (!("Notification" in window)) {
alert("This browser does not support notifications.");
if (permissionResult) { return Promise.reject("This browser does not support notifications.");
permissionResult.then(resolve, reject); }
} if (Notification.permission === "granted") {
}).then((permissionResult) => { return Promise.resolve();
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 Promise.resolve();
}
private requestNotificationPermission(): Promise<NotificationPermission> {
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() { async turnOnNotifications() {

34
sw_scripts/safari-notifications.js

@ -43,55 +43,49 @@ 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++) {
let account = accounts[i]; let account = accounts[i];

Loading…
Cancel
Save