From 250b754c23feaf5970668c905b58ddbed4a0f69c Mon Sep 17 00:00:00 2001 From: Julian B Date: Sat, 28 Sep 2024 14:03:25 +0200 Subject: [PATCH] delete user webpush subscription if delivery fails --- .../core/services/notifications/backends.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ephios/core/services/notifications/backends.py b/ephios/core/services/notifications/backends.py index d752bd423..ef2d06887 100644 --- a/ephios/core/services/notifications/backends.py +++ b/ephios/core/services/notifications/backends.py @@ -10,6 +10,7 @@ from django.core.exceptions import ObjectDoesNotExist from django.core.mail import mail_admins from django.utils.translation import gettext_lazy as _ +from pywebpush import WebPushException from webpush import send_user_notification from ephios.core.models.users import Notification @@ -162,14 +163,17 @@ class WebPushNotificationBackend(AbstractNotificationBackend): @classmethod def send(cls, notification): - payload = { - "head": str(notification.subject), - "body": notification.body, - "icon": "/static/ephios/img/ephios-symbol-red.svg", - } - if actions := notification.get_actions(): - payload["url"] = actions[0][1] - send_user_notification(user=notification.user, payload=payload, ttl=1000) + try: + payload = { + "head": str(notification.subject), + "body": notification.body, + "icon": "/static/ephios/img/ephios-symbol-red.svg", + } + if actions := notification.get_actions(): + payload["url"] = actions[0][1] + send_user_notification(user=notification.user, payload=payload, ttl=1000) + except WebPushException: + notification.user.webpush_info.all().delete() CORE_NOTIFICATION_BACKENDS = [EmailNotificationBackend, WebPushNotificationBackend]