]> littlesong.place Git - littlesongplace.git/commitdiff
Push subscription cleanup
authorChris Fulljames <christianfulljames@gmail.com>
Sat, 2 Aug 2025 18:21:55 +0000 (14:21 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Sat, 23 Aug 2025 11:30:17 +0000 (07:30 -0400)
src/littlesongplace/push_notifications.py
src/littlesongplace/static/nav.js
src/littlesongplace/templates/activity.html

index a5c535e9a340b62cb3c38950ff6325dfcd1fc304..dd4aa735230208038b28680392d2bd46a20f5ca4 100644 (file)
@@ -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/<int:subid>")
index 5a16cbda4f08dd35d8d9e389118440689b430049..e2c0338b2f52dbfd95537ea2b919c984245b23d2 100644 (file)
@@ -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);
 
index ed82c76f5df3facbd94f89503532cce944f1a727..3bd6d41b53a5a972503315af7c8e54e8c57038c6 100644 (file)
@@ -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());