]> littlesong.place Git - littlesongplace.git/commitdiff
Delete dead push subscriptions
authorChris Fulljames <christianfulljames@gmail.com>
Fri, 23 May 2025 00:44:19 +0000 (20:44 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Fri, 23 May 2025 00:44:19 +0000 (20:44 -0400)
src/littlesongplace/push_notifications.py
src/littlesongplace/sql/schema_update.sql

index 7445965ac14da63588b477617e9dbd0ce7942a83..5c4c51e71eeb59855ceed14a3db7099a7f43c939 100644 (file)
@@ -34,11 +34,13 @@ def get_user_subscriptions(userid):
             WHERE userid = ?
             """,
             [userid])
-    str_subs = (r["subscription"] for r in rows)
+    # print([dict(r) for r in rows])
+    # str_subs = (r["subscription"] for r in rows)
     subs = []
-    for s in str_subs:
+    for r in rows:
+        s = r["subscription"]
         try:
-            subs.append(json.loads(s))
+            subs.append((r["subid"], json.loads(s)))
         except json.decoder.JSONDecodeError:
             current_app.logger.error(f"Invalid subscription: {s}")
     return subs
@@ -61,13 +63,23 @@ def notify(userids, title, body):
 def _do_push(app, userids, title, body):
     data = {"title": title, "body": body}
     data_str = json.dumps(data)
-    for userid in userids:
-        with app.app_context():
+    sent_notifications = 0
+    with app.app_context():
+        for userid in userids:
             subs = get_user_subscriptions(userid)
-            for sub in subs:
+            for subid, sub in subs:
                 try:
                     # TODO: Use VAPID keys
                     pywebpush.webpush(sub, data_str)
+                    sent_notifications += 1
                 except pywebpush.WebPushException as ex:
-                    current_app.logger.error(f"Failed to send push: {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}")
+
+        if sent_notifications > 0:
+            app.logger.info(f"Pushed {sent_notifications} notifications")
 
index 14208a2d4a0bb0638a49c4dc7e6ec3736bb9050d..9b480f3c793cd5d87b69d2fe27b5e9d5760010b5 100644 (file)
@@ -1,5 +1,6 @@
 -- DROP TABLE IF EXISTS users_push_subscriptions
 CREATE TABLE users_push_subscriptions (
+    subid INTEGER PRIMARY KEY,
     userid INTEGER NOT NULL,
     subscription TEXT NOT NULL,
     FOREIGN KEY(userid) REFERENCES users(userid) ON DELETE CASCADE