From: Chris Fulljames Date: Sat, 12 Jul 2025 12:27:24 +0000 (-0400) Subject: Store VAPID public key in file X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=b38b5d129f46314849adf41d817f9de819cba552;p=littlesongplace.git Store VAPID public key in file --- diff --git a/src/littlesongplace/__init__.py b/src/littlesongplace/__init__.py index 448fa9b..8153ff1 100644 --- a/src/littlesongplace/__init__.py +++ b/src/littlesongplace/__init__.py @@ -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, ) diff --git a/src/littlesongplace/datadir.py b/src/littlesongplace/datadir.py index 27d5254..b9dfcaa 100644 --- a/src/littlesongplace/datadir.py +++ b/src/littlesongplace/datadir.py @@ -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" diff --git a/src/littlesongplace/push_notifications.py b/src/littlesongplace/push_notifications.py index 15b3ada..4ae9d0c 100644 --- a/src/littlesongplace/push_notifications.py +++ b/src/littlesongplace/push_notifications.py @@ -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") diff --git a/src/littlesongplace/templates/activity.html b/src/littlesongplace/templates/activity.html index b683400..f28531a 100644 --- a/src/littlesongplace/templates/activity.html +++ b/src/littlesongplace/templates/activity.html @@ -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)); diff --git a/src/littlesongplace/templates/index.html b/src/littlesongplace/templates/index.html index a1e72dc..6d98091 100644 --- a/src/littlesongplace/templates/index.html +++ b/src/littlesongplace/templates/index.html @@ -4,8 +4,6 @@ {% block body %} - -

hello!

🎶