Skip to content

Commit

Permalink
[chore] Global Web Notifications handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanus3133 committed Jun 21, 2024
1 parent 471ff4f commit 6ddbebd
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions openwisp_notifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,40 @@ def send_email_notification(sender, instance, created, **kwargs):
)
def clear_notification_cache(sender, instance, **kwargs):
Notification.invalidate_unread_cache(instance.recipient)
# Reload notification widget only if notification is created or deleted
# Display notification toast when a new notification is created
ws_handlers.notification_update_handler(
recipient=instance.recipient,
reload_widget=kwargs.get('created', True),
notification=instance if kwargs.get('created', None) else None,
)

if kwargs.get('created', None):
target_org = getattr(getattr(instance, 'target', None), 'organization_id', None)
notification_setting = None

if instance.type and target_org:
# Check for specific notification setting for the target organization and type
notification_setting = instance.recipient.notificationsetting_set.filter(
organization=target_org, type=instance.type
).first()

if not notification_setting:
# Check for global notification setting
notification_setting = instance.recipient.notificationsetting_set.filter(
organization=None, type=None
).first()

if instance.type and target_org and not notification_setting:
return
else:
web_preference = (
notification_setting.web_notification if notification_setting else True
)

# Display notification toast when a new notification is created
# Reload notification widget only if notification is created or deleted
if web_preference:
ws_handlers.notification_update_handler(
recipient=instance.recipient, reload_widget=True, notification=instance
)
else:
ws_handlers.notification_update_handler(
recipient=instance.recipient, reload_widget=False, notification=None
)


@receiver(post_delete, dispatch_uid='delete_obsolete_objects')
Expand Down

0 comments on commit 6ddbebd

Please sign in to comment.