feat: Clear all previous notifications if a new notification is added to a conversation (#8490)

This commit is contained in:
Muhsin Keloth
2023-12-06 14:03:43 +05:30
committed by GitHub
parent 76711d95ff
commit db33b7d1dc
6 changed files with 57 additions and 6 deletions

View File

@@ -0,0 +1,16 @@
class Notification::RemoveDuplicateNotificationJob < ApplicationJob
queue_as :default
def perform(notification)
return unless notification.is_a?(Notification)
user_id = notification.user_id
primary_actor_id = notification.primary_actor_id
# Find older notifications with the same user and primary_actor_id, excluding the new one
duplicate_notifications = Notification.where(user_id: user_id, primary_actor_id: primary_actor_id)
.where.not(id: notification.id)
duplicate_notifications.each(&:destroy)
end
end

View File

@@ -10,7 +10,7 @@ class ActionCableListener < BaseListener
def notification_deleted(event)
notification, account, unread_count, count = extract_notification_and_account(event)
tokens = [event.data[:notification].user.pubsub_token]
broadcast(account, tokens, NOTIFICATION_DELETED, notification: notification, unread_count: unread_count, count: count)
broadcast(account, tokens, NOTIFICATION_DELETED, { notification: notification.push_event_data, unread_count: unread_count, count: count })
end
def account_cache_invalidated(event)

View File

@@ -52,19 +52,23 @@ class Notification < ApplicationRecord
def push_event_data
# Secondary actor could be nil for cases like system assigning conversation
{
payload = {
id: id,
notification_type: notification_type,
primary_actor_type: primary_actor_type,
primary_actor_id: primary_actor_id,
primary_actor: primary_actor_data,
read_at: read_at,
secondary_actor: secondary_actor&.push_event_data,
user: user&.push_event_data,
created_at: created_at.to_i,
account_id: account_id,
push_message_title: push_message_title
account_id: account_id
}
if primary_actor.present?
payload[:primary_actor] = primary_actor_data
payload[:push_message_title] = push_message_title
end
payload
end
def primary_actor_data
@@ -121,6 +125,9 @@ class Notification < ApplicationRecord
# In future, we could probably add condition here to enqueue the job for 30 seconds later
# when push enabled and then check in email job whether notification has been read already.
Notification::EmailNotificationJob.perform_later(self)
# Remove duplicate notifications
Notification::RemoveDuplicateNotificationJob.perform_later(self)
end
def dispatch_create_event