diff --git a/.rubocop.yml b/.rubocop.yml index e0c98cdc4..c3096dd4a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,6 +16,7 @@ Metrics/ClassLength: - 'app/models/message.rb' - 'app/builders/messages/facebook/message_builder.rb' - 'app/controllers/api/v1/accounts/contacts_controller.rb' + - 'app/listeners/action_cable_listener.rb' RSpec/ExampleLength: Max: 25 Style/Documentation: diff --git a/app/listeners/action_cable_listener.rb b/app/listeners/action_cable_listener.rb index 030f306a8..6fe47953f 100644 --- a/app/listeners/action_cable_listener.rb +++ b/app/listeners/action_cable_listener.rb @@ -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 diff --git a/app/listeners/base_listener.rb b/app/listeners/base_listener.rb index d4326133a..4315d400d 100644 --- a/app/listeners/base_listener.rb +++ b/app/listeners/base_listener.rb @@ -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] diff --git a/app/models/notification.rb b/app/models/notification.rb index 7599d4d6e..863f1cd39 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -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 diff --git a/lib/events/types.rb b/lib/events/types.rb index ec6d1d353..b015cd3c5 100644 --- a/lib/events/types.rb +++ b/lib/events/types.rb @@ -39,6 +39,9 @@ module Events::Types CONTACT_MERGED = 'contact.merged' CONTACT_DELETED = 'contact.deleted' + # notification events + NOTIFICATION_CREATED = 'notification.created' + # agent events AGENT_ADDED = 'agent.added' AGENT_REMOVED = 'agent.removed'