feat: Clear all previous notifications if a new notification is added to a conversation (#8490)
This commit is contained in:
16
app/jobs/notification/remove_duplicate_notification_job.rb
Normal file
16
app/jobs/notification/remove_duplicate_notification_job.rb
Normal 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
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user