From 8a6d2b9110a6a44154d9a743f64027eaa267631f Mon Sep 17 00:00:00 2001 From: Chris Fulljames Date: Sat, 7 Jun 2025 10:39:03 -0400 Subject: [PATCH] Store subscription ID in localStorage --- src/littlesongplace/push_notifications.py | 14 ++++++++------ src/littlesongplace/templates/activity.html | 14 +++++++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/littlesongplace/push_notifications.py b/src/littlesongplace/push_notifications.py index c9b17c1..d2f980f 100644 --- a/src/littlesongplace/push_notifications.py +++ b/src/littlesongplace/push_notifications.py @@ -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}") diff --git a/src/littlesongplace/templates/activity.html b/src/littlesongplace/templates/activity.html index d2a270a..e9ee804 100644 --- a/src/littlesongplace/templates/activity.html +++ b/src/littlesongplace/templates/activity.html @@ -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) { -- 2.39.5