local_notification_worker.rb 1.0 KB

12345678910111213141516171819202122232425262728
  1. # frozen_string_literal: true
  2. class LocalNotificationWorker
  3. include Sidekiq::Worker
  4. def perform(receiver_account_id, activity_id = nil, activity_class_name = nil, type = nil)
  5. if activity_id.nil? && activity_class_name.nil?
  6. activity = Mention.find(receiver_account_id)
  7. receiver = activity.account
  8. else
  9. receiver = Account.find(receiver_account_id)
  10. activity = activity_class_name.constantize.find(activity_id)
  11. end
  12. # For most notification types, only one notification should exist, and the older one is
  13. # preferred. For updates, such as when a status is edited, the new notification
  14. # should replace the previous ones.
  15. if type == 'update'
  16. Notification.where(account: receiver, activity: activity, type: 'update').in_batches.delete_all
  17. elsif Notification.where(account: receiver, activity: activity, type: type).any?
  18. return
  19. end
  20. NotifyService.new.call(receiver, type || activity_class_name.underscore, activity)
  21. rescue ActiveRecord::RecordNotFound
  22. true
  23. end
  24. end