]> littlesong.place Git - littlesongplace.git/commitdiff
Store VAPID public key in file
authorChris Fulljames <christianfulljames@gmail.com>
Sat, 12 Jul 2025 12:27:24 +0000 (08:27 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Sat, 23 Aug 2025 11:30:17 +0000 (07:30 -0400)
src/littlesongplace/__init__.py
src/littlesongplace/datadir.py
src/littlesongplace/push_notifications.py
src/littlesongplace/templates/activity.html
src/littlesongplace/templates/index.html

index 448fa9b767206168e6538d2247d6642a973424c3..8153ff1c3bf899dfe7b152442b8fe145e15ddd1c 100644 (file)
@@ -46,6 +46,12 @@ if "DATA_DIR" in os.environ:
     )
     app.logger.setLevel(logging.INFO)
 
+VAPID_PUBLIC_KEY = ""
+_vapid_key_path = datadir.get_vapid_public_key_path()
+if _vapid_key_path.exists():
+    with open(_vapid_key_path, "r") as f:
+        VAPID_PUBLIC_KEY = f.read().strip()
+
 @app.route("/")
 def index():
     all_users = db.query("select * from users order by username asc")
@@ -139,6 +145,7 @@ def inject_global_vars():
         gif_data=get_gif_data(),
         # Add to Playlist dropdown entries
         current_user_playlists=get_current_user_playlists(),
+        vapid_public_key=VAPID_PUBLIC_KEY,
         **colors.DEFAULT_COLORS,
     )
 
index 27d5254465316b7c14b1ab9df216634c4fc7bd94..b9dfcaa35a65fc8637a64645fd10ddef02b6f354 100644 (file)
@@ -28,6 +28,9 @@ def get_user_images_path(userid):
 def get_app_log_path():
     return _data_dir / "app.log"
 
+def get_vapid_public_key_path():
+    return _data_dir / "vapid-public.key"
+
 def get_vapid_private_key_path():
     return _data_dir / "vapid-private.key"
 
index 15b3ada7e531862c6592d2d3a0928c790adf01de..4ae9d0cc6a7c76c93bababc09d311fd39fcb19fe 100644 (file)
@@ -147,12 +147,10 @@ def _do_push(app, userids, title, body):
 
                     sent_notifications += 1
                 except pywebpush.WebPushException as ex:
-                    if ex.response.status_code == 410:  # Subscription deleted
-                        app.logger.warning(f"Deleting dead push subscription: {subid}")
-                        db.query("DELETE FROM users_push_subscriptions WHERE subid = ?", [subid])
-                        db.commit()
-                    else:
-                        app.logger.error(f"Failed to send push: {ex}")
+                    # Failed to send notification, delete this subscription
+                    app.logger.warning(f"Deleting dead push subscription: {subid} - {ex}")
+                    db.query("DELETE FROM users_push_subscriptions WHERE subid = ?", [subid])
+                    db.commit()
 
         if sent_notifications > 0:
             app.logger.info(f"Pushed {sent_notifications} notifications")
index b6834006813e5ed3d1f707069bd4904d81773f3e..f28531a0a63e655922f1f26a3da7a5a00d134ac6 100644 (file)
@@ -85,7 +85,7 @@ async function enablePushNotifications() {
             if (!existingSubscription || !window.localStorage.getItem("subid"))
             {
                 // Subscribe via browser's push service
-                const vapid_public_key = "BLsO37LostwqKch7SFr5Df0MexEoBOcujdMRY7wJurRPc_MGdz9rAkMrqs_dil4qSFxVbVyAA3FqLEPSL-WRNZs";
+                const vapid_public_key = "{{ vapid_public_key }}";
                 const options = {userVisibleOnly: true, applicationServerKey: vapid_public_key};
                 const subscription = await registration.pushManager.subscribe(options);
                 console.log(JSON.stringify(subscription));
index a1e72dc6676384f925906a5550d6056d2c4498ec..6d980918565c991011bd3efa9ce478fbbc212da7 100644 (file)
@@ -4,8 +4,6 @@
 
 {% block body %}
 
-<button onclick="requestNotificationPermission()" class="button">Enable Notifications</button>
-
 <h2 class="mt0">hello!</h2>
 <div style="display: flex; flex-direction: row; justify-content: center; gap: 10px; align-items: center;">
 <div>🎶</div>