feat: Notification action cable event (#4154)

This commit is contained in:
Muhsin Keloth
2022-03-17 18:56:36 +05:30
committed by GitHub
parent 430547e026
commit 23fe8847d8
5 changed files with 21 additions and 1 deletions

View File

@@ -1,6 +1,12 @@
class ActionCableListener < BaseListener
include Events::Types
def notification_created(event)
notification, account = extract_notification_and_account(event)
tokens = [event.data[:notification].user.pubsub_token]
broadcast(account, tokens, NOTIFICATION_CREATED, notification.push_event_data)
end
def message_created(event)
message, account = extract_message_and_account(event)
conversation = message.conversation

View File

@@ -6,6 +6,11 @@ class BaseListener
[conversation, conversation.account]
end
def extract_notification_and_account(event)
notification = event.data[:notification]
[notification, notification.account]
end
def extract_message_and_account(event)
message = event.data[:message]
[message, message.account]

View File

@@ -39,7 +39,8 @@ class Notification < ApplicationRecord
enum notification_type: NOTIFICATION_TYPES
after_create_commit :process_notification_delivery
after_create_commit :process_notification_delivery, :dispatch_create_event
default_scope { order(id: :desc) }
PRIMARY_ACTORS = ['Conversation'].freeze
@@ -108,4 +109,8 @@ class Notification < ApplicationRecord
# when push enabled and then check in email job whether notification has been read already.
Notification::EmailNotificationJob.perform_later(self)
end
def dispatch_create_event
Rails.configuration.dispatcher.dispatch(NOTIFICATION_CREATED, Time.zone.now, notification: self)
end
end