chore: Get all notification API improvments (#8549)

Co-authored-by: Sojan Jose <sojan@chatwoot.com>
This commit is contained in:
Muhsin Keloth
2024-01-17 09:02:18 +05:30
committed by GitHub
parent e67f8824d9
commit 818424259f
10 changed files with 176 additions and 31 deletions

View File

@@ -47,10 +47,6 @@ class Notification < ApplicationRecord
after_create_commit :process_notification_delivery, :dispatch_create_event
after_destroy_commit :dispatch_destroy_event
# TODO: Get rid of default scope
# https://stackoverflow.com/a/1834250/939299
default_scope { order(id: :desc) }
PRIMARY_ACTORS = ['Conversation'].freeze
def push_event_data
@@ -122,17 +118,24 @@ class Notification < ApplicationRecord
private
def process_notification_delivery
Notification::PushNotificationJob.perform_later(self)
Notification::PushNotificationJob.perform_later(self) if user_subscribed_to_notification?('push')
# Should we do something about the case where user subscribed to both push and email ?
# 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)
Notification::EmailNotificationJob.perform_later(self) if user_subscribed_to_notification?('email')
# Remove duplicate notifications
Notification::RemoveDuplicateNotificationJob.perform_later(self)
end
def user_subscribed_to_notification?(delivery_type)
notification_setting = user.notification_settings.find_by(account_id: account.id)
return false if notification_setting.blank?
# Check if the user has subscribed to the specified type of notification
notification_setting.public_send("#{delivery_type}_#{notification_type}?")
end
def dispatch_create_event
Rails.configuration.dispatcher.dispatch(NOTIFICATION_CREATED, Time.zone.now, notification: self)
end