move more logging into the database

This commit is contained in:
2024-11-17 18:16:22 -07:00
parent f57d3b7707
commit 228f213c10
3 changed files with 83 additions and 36 deletions

View File

@@ -315,17 +315,17 @@
<button
class="block w-full text-center text-md font-bold uppercase bg-blue-600 text-white px-2 py-2 rounded-md mb-2"
>
For 1 Hour
For 1 Day
</button>
<button
class="block w-full text-center text-md font-bold uppercase bg-blue-600 text-white px-2 py-2 rounded-md mb-2"
>
For 8 Hours
For 2 Days
</button>
<button
class="block w-full text-center text-md font-bold uppercase bg-blue-600 text-white px-2 py-2 rounded-md mb-2"
>
For 24 Hours
For 1 Week
</button>
<button
class="block w-full text-center text-md font-bold uppercase bg-blue-600 text-white px-2 py-2 rounded-md mb-2"
@@ -384,7 +384,7 @@ import axios from "axios";
import { Vue, Component } from "vue-facing-decorator";
import { DEFAULT_PUSH_SERVER, NotificationIface } from "@/constants/app";
import { addLogMessage, retrieveSettingsForActiveAccount } from "@/db/index";
import { logConsoleAndDb, retrieveSettingsForActiveAccount } from "@/db/index";
import * as libsUtil from "@/libs/util";
import { urlBase64ToUint8Array } from "@/libs/crypto/vc/util";
@@ -436,19 +436,21 @@ export default class App extends Vue {
}
if (pushUrl.startsWith("http://localhost")) {
console.log("Not checking for VAPID in this local environment.");
logConsoleAndDb("Not checking for VAPID in this local environment.");
} else {
let responseData = "";
await axios
.get(pushUrl + "/web-push/vapid")
.then((response: VapidResponse) => {
this.b64 = response.data?.vapidKey || "";
console.log("Got vapid key:", this.b64);
logConsoleAndDb("Got vapid key: " + this.b64);
responseData = JSON.stringify(response.data);
navigator.serviceWorker?.addEventListener(
"controllerchange",
() => {
console.log("New service worker is now controlling the page");
logConsoleAndDb(
"New service worker is now controlling the page",
);
},
);
});
@@ -462,17 +464,23 @@ export default class App extends Vue {
},
5000,
);
await addLogMessage(
logConsoleAndDb(
"Error Setting Notifications: web push server response didn't have vapidKey: " +
responseData,
true,
);
}
}
} catch (error) {
if (window.location.host.startsWith("localhost")) {
console.log("Ignoring the error getting VAPID for local development.");
logConsoleAndDb(
"Ignoring the error getting VAPID for local development.",
);
} else {
console.error("Got an error initializing notifications:", error);
logConsoleAndDb(
"Got an error initializing notifications: " + JSON.stringify(error),
true,
);
this.$notify(
{
group: "alert",
@@ -482,10 +490,6 @@ export default class App extends Vue {
},
5000,
);
await addLogMessage(
"Error Setting Notifications: some error occurred: " +
JSON.stringify(error),
);
}
}
// there may be a long pause here on first initialization
@@ -519,7 +523,9 @@ export default class App extends Vue {
}
private askPermission(): Promise<NotificationPermission> {
console.log("Requesting permission for notifications:", navigator);
logConsoleAndDb(
"Requesting permission for notifications: " + JSON.stringify(navigator),
);
if (
!("serviceWorker" in navigator && navigator.serviceWorker?.controller)
) {
@@ -544,7 +550,9 @@ export default class App extends Vue {
};
return this.sendMessageToServiceWorker(message).then((response) => {
console.log("Response from service worker:", response);
logConsoleAndDb(
"Response from service worker: " + JSON.stringify(response),
);
});
}
@@ -647,12 +655,12 @@ export default class App extends Vue {
public async turnOnNotifications() {
return this.askPermission()
.then((permission) => {
console.log("Permission granted:", permission);
logConsoleAndDb("Permission granted: " + JSON.stringify(permission));
// Call the function and handle promises
this.subscribeToPush()
.then(() => {
console.log("Subscribed successfully.");
logConsoleAndDb("Subscribed successfully.");
return navigator.serviceWorker?.ready;
})
.then((registration) => {
@@ -684,7 +692,6 @@ export default class App extends Vue {
const utcHour =
hourNum + Math.round(new Date().getTimezoneOffset() / 60);
const finalUtcHour = (utcHour + (utcHour < 0 ? 24 : 0)) % 24;
console.log("utc hour:", utcHour, "final utc:", finalUtcHour);
const minuteNum = libsUtil.numberOrZero(this.minuteInput);
const utcMinute =
minuteNum + Math.round(new Date().getTimezoneOffset() % 60);
@@ -697,13 +704,18 @@ export default class App extends Vue {
...subscription.toJSON(),
};
await this.sendSubscriptionToServer(subscriptionWithTime);
// To help investigate potential issues with this: https://firebase.google.com/docs/cloud-messaging/migrate-v1
logConsoleAndDb(
"Subscription data sent to server with endpoint: " +
subscription.endpoint,
);
return subscriptionWithTime;
} else {
throw new Error("Subscription object is not available.");
}
})
.then(async (subscription: PushSubscriptionWithTime) => {
console.log(
logConsoleAndDb(
"Subscription data sent to server and all finished successfully.",
);
await libsUtil.sendTestThroughPushServer(subscription, true);
@@ -728,9 +740,10 @@ export default class App extends Vue {
});
})
.catch((error) => {
console.error(
"An error occurred setting notification permissions:",
error,
logConsoleAndDb(
"An error occurred setting notification permissions: " +
JSON.stringify(error),
true,
);
alert("Some error occurred setting notification permissions.");
});
@@ -761,11 +774,19 @@ export default class App extends Vue {
return registration.pushManager.subscribe(options);
})
.then((subscription) => {
console.log("Push subscription successful:", subscription);
logConsoleAndDb(
"Push subscription successful: " + JSON.stringify(subscription),
);
resolve();
})
.catch((error) => {
console.error("Push subscription failed:", error, options);
logConsoleAndDb(
"Push subscription failed: " +
JSON.stringify(error) +
" - " +
JSON.stringify(options),
true,
);
// Inform the user about the issue
alert(
@@ -781,7 +802,7 @@ export default class App extends Vue {
private sendSubscriptionToServer(
subscription: PushSubscriptionWithTime,
): Promise<void> {
console.log("About to send subscription...", subscription);
logConsoleAndDb("About to send subscription... " + subscription);
return fetch("/web-push/subscribe", {
method: "POST",
headers: {
@@ -792,7 +813,7 @@ export default class App extends Vue {
if (!response.ok) {
throw new Error("Failed to send subscription to server");
}
console.log("Subscription sent to server successfully.");
logConsoleAndDb("Subscription sent to server successfully.");
});
}
@@ -807,12 +828,15 @@ export default class App extends Vue {
if (subscription) {
return subscription.unsubscribe();
} else {
console.log("Subscription object is not available.");
logConsoleAndDb("Subscription object is not available.");
return false;
}
})
.catch((error) => {
console.error("Push provider server communication failed:", error);
logConsoleAndDb(
"Push provider server communication failed: " + JSON.stringify(error),
true,
);
return false;
});
@@ -827,7 +851,10 @@ export default class App extends Vue {
return response.ok;
})
.catch((error) => {
console.error("Push server communication failed:", error);
logConsoleAndDb(
"Push server communication failed: " + JSON.stringify(error),
true,
);
return false;
});