chore: Get all notification API improvments (#8549)
Co-authored-by: Sojan Jose <sojan@chatwoot.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -153,13 +153,6 @@ class User < ApplicationRecord
|
||||
mutations_from_database.changed?('email')
|
||||
end
|
||||
|
||||
def notifications_meta(account_id)
|
||||
{
|
||||
unread_count: notifications.where(account_id: account_id, read_at: nil).count,
|
||||
count: notifications.where(account_id: account_id).count
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def remove_macros
|
||||
|
||||
Reference in New Issue
Block a user