闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.4 KiB

  1. # frozen_string_literal: true
  2. class NotifyService < BaseService
  3. def call(recipient, activity)
  4. @recipient = recipient
  5. @activity = activity
  6. @notification = Notification.new(account: @recipient, activity: @activity)
  7. return if blocked? || recipient.user.nil?
  8. create_notification
  9. send_email if email_enabled?
  10. send_push_notification
  11. rescue ActiveRecord::RecordInvalid
  12. return
  13. end
  14. private
  15. def blocked_mention?
  16. FeedManager.instance.filter?(:mentions, @notification.mention.status, @recipient)
  17. end
  18. def blocked_favourite?
  19. false
  20. end
  21. def blocked_follow?
  22. false
  23. end
  24. def blocked_reblog?
  25. false
  26. end
  27. def blocked_follow_request?
  28. false
  29. end
  30. def blocked?
  31. blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
  32. blocked ||= @recipient.id == @notification.from_account.id # Skip for interactions with self
  33. blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts
  34. blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban
  35. blocked ||= (@recipient.user.settings.interactions['must_be_follower'] && !@notification.from_account.following?(@recipient)) # Options
  36. blocked ||= (@recipient.user.settings.interactions['must_be_following'] && !@recipient.following?(@notification.from_account)) # Options
  37. blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
  38. blocked
  39. end
  40. def create_notification
  41. @notification.save!
  42. return unless @notification.browserable?
  43. FeedManager.instance.broadcast(@recipient.id, event: 'notification', payload: FeedManager.instance.inline_render(@recipient, 'api/v1/notifications/show', @notification))
  44. end
  45. def send_email
  46. NotificationMailer.send(@notification.type, @recipient, @notification).deliver_later
  47. end
  48. def send_push_notification
  49. PushNotificationWorker.perform_async(@notification.id)
  50. end
  51. def email_enabled?
  52. @recipient.user.settings.notification_emails[@notification.type]
  53. end
  54. end