From 6558aeab227bc816fa5c8a46ff67bf726b057e7d Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Sat, 16 Dec 2023 23:47:49 -0500 Subject: [PATCH] Updates to delete missing subscriptions --- app.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index d7737ca..fbb5eba 100644 --- a/app.py +++ b/app.py @@ -150,11 +150,27 @@ class WebPushService(): vapid_private_key=vapid_key.private_key, vapid_claims={"sub": CONTACT_EMAIL} ) + time.sleep(1) return True except WebPushException as ex: - print(f"Failed to send push notification for {subscription_info['endpoint']} -- {ex}") - return False + now = datetime.datetime.now().isoformat() + endpoint = subscription_info['endpoint'] + print(f"{now}: Failed to send push notification for {endpoint} -- {ex}") + + unsubscribed_msg = '410 Gone' + if unsubscribed_msg in ex.args[0]: + subscription = Subscription.query.filter_by(endpoint=endpoint).first() + + # Delete the subscription if found + if subscription: + db.session.delete(subscription) + db.session.commit() + print(f"Committed delete of {subscription_info}") + else: + print(f"Could not find subscription at: {endpoint}") + else: + print("Wrong error.", ex.args[0]) def _send_daily_notifications(self) -> None: