Skip to content

Commit

Permalink
delete user webpush subscription if delivery fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jeriox committed Oct 2, 2024
1 parent fa2e7d9 commit 721a1ee
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions ephios/core/services/notifications/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

0 comments on commit 721a1ee

Please sign in to comment.