notify_service.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # frozen_string_literal: true
  2. class NotifyService < BaseService
  3. def call(recipient, type, activity)
  4. @recipient = recipient
  5. @activity = activity
  6. @notification = Notification.new(account: @recipient, type: type, activity: @activity)
  7. return if recipient.user.nil? || blocked?
  8. create_notification!
  9. push_notification!
  10. push_to_conversation! if direct_message?
  11. send_email! if email_enabled?
  12. rescue ActiveRecord::RecordInvalid
  13. nil
  14. end
  15. private
  16. def blocked_mention?
  17. FeedManager.instance.filter?(:mentions, @notification.mention.status, @recipient)
  18. end
  19. def blocked_status?
  20. false
  21. end
  22. def blocked_favourite?
  23. false
  24. end
  25. def blocked_follow?
  26. false
  27. end
  28. def blocked_reblog?
  29. false
  30. end
  31. def blocked_follow_request?
  32. false
  33. end
  34. def blocked_poll?
  35. false
  36. end
  37. def following_sender?
  38. return @following_sender if defined?(@following_sender)
  39. @following_sender = @recipient.following?(@notification.from_account) || @recipient.requested?(@notification.from_account)
  40. end
  41. def optional_non_follower?
  42. @recipient.user.settings.interactions['must_be_follower'] && !@notification.from_account.following?(@recipient)
  43. end
  44. def optional_non_following?
  45. @recipient.user.settings.interactions['must_be_following'] && !following_sender?
  46. end
  47. def message?
  48. @notification.type == :mention
  49. end
  50. def direct_message?
  51. message? && @notification.target_status.direct_visibility?
  52. end
  53. def response_to_recipient?
  54. @notification.target_status.in_reply_to_account_id == @recipient.id && @notification.target_status.thread&.direct_visibility?
  55. end
  56. def from_staff?
  57. @notification.from_account.local? && @notification.from_account.user.present? && @notification.from_account.user.staff?
  58. end
  59. def optional_non_following_and_direct?
  60. direct_message? &&
  61. @recipient.user.settings.interactions['must_be_following_dm'] &&
  62. !following_sender? &&
  63. !response_to_recipient?
  64. end
  65. def hellbanned?
  66. @notification.from_account.silenced? && !following_sender?
  67. end
  68. def from_self?
  69. @recipient.id == @notification.from_account.id
  70. end
  71. def domain_blocking?
  72. @recipient.domain_blocking?(@notification.from_account.domain) && !following_sender?
  73. end
  74. def blocked?
  75. blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
  76. blocked ||= from_self? && @notification.type != :poll # Skip for interactions with self
  77. return blocked if message? && from_staff?
  78. blocked ||= domain_blocking? # Skip for domain blocked accounts
  79. blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts
  80. blocked ||= @recipient.muting_notifications?(@notification.from_account)
  81. blocked ||= hellbanned? # Hellban
  82. blocked ||= optional_non_follower? # Options
  83. blocked ||= optional_non_following? # Options
  84. blocked ||= optional_non_following_and_direct? # Options
  85. blocked ||= conversation_muted?
  86. blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
  87. blocked
  88. end
  89. def conversation_muted?
  90. if @notification.target_status
  91. @recipient.muting_conversation?(@notification.target_status.conversation)
  92. else
  93. false
  94. end
  95. end
  96. def create_notification!
  97. @notification.save!
  98. end
  99. def push_notification!
  100. return if @notification.activity.nil?
  101. Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification)))
  102. send_push_notifications!
  103. end
  104. def push_to_conversation!
  105. return if @notification.activity.nil?
  106. AccountConversation.add_status(@recipient, @notification.target_status)
  107. end
  108. def send_push_notifications!
  109. subscriptions_ids = ::Web::PushSubscription.where(user_id: @recipient.user.id)
  110. .select { |subscription| subscription.pushable?(@notification) }
  111. .map(&:id)
  112. ::Web::PushNotificationWorker.push_bulk(subscriptions_ids) do |subscription_id|
  113. [subscription_id, @notification.id]
  114. end
  115. end
  116. def send_email!
  117. return if @notification.activity.nil?
  118. NotificationMailer.public_send(@notification.type, @recipient, @notification).deliver_later(wait: 2.minutes)
  119. end
  120. def email_enabled?
  121. @recipient.user.settings.notification_emails[@notification.type.to_s]
  122. end
  123. end