]> littlesong.place Git - littlesongplace.git/commitdiff
Store subscription ID in localStorage
authorChris Fulljames <christianfulljames@gmail.com>
Sat, 7 Jun 2025 14:39:03 +0000 (10:39 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Sat, 23 Aug 2025 11:30:17 +0000 (07:30 -0400)
src/littlesongplace/push_notifications.py
src/littlesongplace/templates/activity.html

index c9b17c1e94438de0ba8ea6fa42a9943295a022e6..d2f980f384feb922c6ee140856719b0e65b3d99b 100644 (file)
@@ -33,9 +33,7 @@ def subscribe():
 
     current_app.logger.info(f"{g.username} registered push subscription")
 
-    session["subid"] = row["subid"]
-
-    return {"status": "success"}
+    return {"status": "success", "subid": row["subid"]}
 
 @bp.post("/update-settings")
 @auth.requires_login
@@ -44,11 +42,15 @@ def update_settings():
         # Request must contain valid subscription JSON
         abort(400)
 
-    if "subid" not in session:
-        return {"status": "failed", "message": "no subid in current session"}
 
     bitfield = 0
     settings = request.json
+
+    if ("subid" not in settings) or ("comments" not in settings) or ("songs" not in settings):
+        abort(400)
+
+    subid = settings["subid"]
+
     if settings["comments"]:
         bitfield |= SubscriptionSetting.COMMENTS
     if settings["songs"]:
@@ -60,7 +62,7 @@ def update_settings():
             SET settings = ?
             WHERE subid = ? AND userid = ?
             """,
-            [bitfield, session["subid"], g.userid])
+            [bitfield, subid, g.userid])
     db.commit()
 
     current_app.logger.info(f"{g.username} updated push subscription settings: {bitfield:04x}")
index d2a270a99dc37af1f77b4b1b0a1f26d155cac0f9..e9ee804d23bb5f0fb918c9bcf056c680b567271d 100644 (file)
@@ -41,7 +41,11 @@ async function updateSettings() {
         "/push-notifications/update-settings", {
             method: "post",
             headers: {"Content-Type": "application/json"},
-            body: JSON.stringify({comments: comment_push_enabled, songs: song_push_enabled})
+            body: JSON.stringify({
+                comments: comment_push_enabled,
+                songs: song_push_enabled,
+                subid: window.localStorage.getItem("subid"),
+            })
         }
     );
 }
@@ -59,7 +63,7 @@ async function enablePushNotifications() {
         try {
             const registration = await navigator.serviceWorker.getRegistration();
             const existingSubscription = await registration.pushManager.getSubscription();
-            if (!existingSubscription)
+            if (!existingSubscription || !window.localStorage.getItem("subid"))
             {
                 // Subscribe via browser's push service
                 const vapid_public_key = "BLsO37LostwqKch7SFr5Df0MexEoBOcujdMRY7wJurRPc_MGdz9rAkMrqs_dil4qSFxVbVyAA3FqLEPSL-WRNZs";
@@ -68,13 +72,17 @@ async function enablePushNotifications() {
                 console.log(JSON.stringify(subscription));
 
                 // Register subscription with LSP server
-                await fetch(
+                const response = await fetch(
                     "/push-notifications/subscribe", {
                         method: "post",
                         headers: {"Content-Type": "application/json"},
                         body: JSON.stringify(subscription)
                     }
                 );
+
+                const rspJson = await response.json();
+                console.log("Subscription ID:", rspJson.subid);
+                window.localStorage.setItem("subid", rspJson.subid);
             }
         }
         catch (err) {