From b73decc1ad31a39e0ec429f4804ebae8c1aef07a Mon Sep 17 00:00:00 2001 From: Chris Fulljames Date: Sat, 2 Aug 2025 14:21:55 -0400 Subject: [PATCH] Push subscription cleanup --- src/littlesongplace/push_notifications.py | 8 ++++---- src/littlesongplace/static/nav.js | 8 ++++---- src/littlesongplace/templates/activity.html | 6 +++++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/littlesongplace/push_notifications.py b/src/littlesongplace/push_notifications.py index a5c535e..dd4aa73 100644 --- a/src/littlesongplace/push_notifications.py +++ b/src/littlesongplace/push_notifications.py @@ -35,11 +35,12 @@ def subscribe(): row = db.query( """ UPDATE users_push_subscriptions - SET userid = ?, subscription = ? + SET subscription = ? WHERE subid = ? AND userid = ? RETURNING subid """, - [g.userid, json.dumps(request.json), subid, g.userid], expect_one=True) + [json.dumps(request.json), subid, g.userid], expect_one=True) + current_app.logger.info(f"{g.username} updated push subscription {row['subid']}") else: row = db.query( """ @@ -48,10 +49,9 @@ def subscribe(): RETURNING subid """, [g.userid, json.dumps(request.json), 0], expect_one=True) + current_app.logger.info(f"{g.username} registered push subscription {row['subid']}") db.commit() - current_app.logger.info(f"{g.username} registered push subscription") - return {"status": "success", "subid": row["subid"]} @bp.post("/update-subscription/") diff --git a/src/littlesongplace/static/nav.js b/src/littlesongplace/static/nav.js index 5a16cbd..e2c0338 100644 --- a/src/littlesongplace/static/nav.js +++ b/src/littlesongplace/static/nav.js @@ -259,18 +259,19 @@ function updateImageColors() { } async function periodicPushSync() { - console.log("sync"); if (!("serviceWorker" in navigator)) { return; // No service woker available } const subid = window.localStorage.getItem("subid"); - console.log(subid); if (subid) { await syncPushSubscription(); } } async function syncPushSubscription() { + if (Notification.permission != "granted") { + return; + } const registration = await navigator.serviceWorker.getRegistration(); let subscription = await registration.pushManager.getSubscription(); if (!subscription) @@ -295,10 +296,9 @@ async function syncPushSubscription() { ); const rspJson = await response.json(); - console.log("Subscription ID:", rspJson.subid); window.localStorage.setItem("subid", rspJson.subid); } periodicPushSync(); -setInterval(periodicPushSync, 10000); +setInterval(periodicPushSync, 60*60*1000); diff --git a/src/littlesongplace/templates/activity.html b/src/littlesongplace/templates/activity.html index ed82c76..3bd6d41 100644 --- a/src/littlesongplace/templates/activity.html +++ b/src/littlesongplace/templates/activity.html @@ -26,7 +26,7 @@ function updateSelections() { // Prevent this from getting called again on the next page load document.removeEventListener("DOMContentLoaded", updateSelections); - if (window.localStorage.getItem("subid")) + if (localStorage.getItem("subid") && Notification.permission == "granted") { const params = new URLSearchParams({subid: window.localStorage.getItem("subid")}); fetch(`/push-notifications/settings?${params}`).then((r) => { @@ -37,6 +37,10 @@ function updateSelections() { }); }) } + else { + document.getElementById("comment-push").checked = false; + document.getElementById("song-push").checked = false; + } } document.addEventListener("DOMContentLoaded", updateSelections()); -- 2.39.5