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> {
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<void> {
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<void> {
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<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() {

34
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];

Loading…
Cancel
Save