notify_service.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # frozen_string_literal: true
  2. class NotifyService < BaseService
  3. include Redisable
  4. NON_EMAIL_TYPES = %i(
  5. admin.report
  6. admin.sign_up
  7. update
  8. poll
  9. status
  10. ).freeze
  11. def call(recipient, type, activity)
  12. @recipient = recipient
  13. @activity = activity
  14. @notification = Notification.new(account: @recipient, type: type, activity: @activity)
  15. return if recipient.user.nil? || blocked?
  16. @notification.save!
  17. # It's possible the underlying activity has been deleted
  18. # between the save call and now
  19. return if @notification.activity.nil?
  20. push_notification!
  21. push_to_conversation! if direct_message?
  22. send_email! if email_needed?
  23. rescue ActiveRecord::RecordInvalid
  24. nil
  25. end
  26. private
  27. def blocked_mention?
  28. FeedManager.instance.filter?(:mentions, @notification.mention.status, @recipient)
  29. end
  30. def following_sender?
  31. return @following_sender if defined?(@following_sender)
  32. @following_sender = @recipient.following?(@notification.from_account) || @recipient.requested?(@notification.from_account)
  33. end
  34. def optional_non_follower?
  35. @recipient.user.settings['interactions.must_be_follower'] && !@notification.from_account.following?(@recipient)
  36. end
  37. def optional_non_following?
  38. @recipient.user.settings['interactions.must_be_following'] && !following_sender?
  39. end
  40. def message?
  41. @notification.type == :mention
  42. end
  43. def direct_message?
  44. message? && @notification.target_status.direct_visibility?
  45. end
  46. # Returns true if the sender has been mentioned by the recipient up the thread
  47. def response_to_recipient?
  48. return false if @notification.target_status.in_reply_to_id.nil?
  49. # Using an SQL CTE to avoid unneeded back-and-forth with SQL server in case of long threads
  50. !Status.count_by_sql([<<-SQL.squish, id: @notification.target_status.in_reply_to_id, recipient_id: @recipient.id, sender_id: @notification.from_account.id, depth_limit: 100]).zero?
  51. WITH RECURSIVE ancestors(id, in_reply_to_id, mention_id, path, depth) AS (
  52. SELECT s.id, s.in_reply_to_id, m.id, ARRAY[s.id], 0
  53. FROM statuses s
  54. LEFT JOIN mentions m ON m.silent = FALSE AND m.account_id = :sender_id AND m.status_id = s.id
  55. WHERE s.id = :id
  56. UNION ALL
  57. SELECT s.id, s.in_reply_to_id, m.id, st.path || s.id, st.depth + 1
  58. FROM ancestors st
  59. JOIN statuses s ON s.id = st.in_reply_to_id
  60. LEFT JOIN mentions m ON m.silent = FALSE AND m.account_id = :sender_id AND m.status_id = s.id
  61. WHERE st.mention_id IS NULL AND NOT s.id = ANY(path) AND st.depth < :depth_limit
  62. )
  63. SELECT COUNT(*)
  64. FROM ancestors st
  65. JOIN statuses s ON s.id = st.id
  66. WHERE st.mention_id IS NOT NULL AND s.visibility = 3
  67. SQL
  68. end
  69. def from_staff?
  70. @notification.from_account.local? && @notification.from_account.user.present? && @notification.from_account.user_role&.overrides?(@recipient.user_role)
  71. end
  72. def optional_non_following_and_direct?
  73. direct_message? &&
  74. @recipient.user.settings['interactions.must_be_following_dm'] &&
  75. !following_sender? &&
  76. !response_to_recipient?
  77. end
  78. def hellbanned?
  79. @notification.from_account.silenced? && !following_sender?
  80. end
  81. def from_self?
  82. @recipient.id == @notification.from_account.id
  83. end
  84. def domain_blocking?
  85. @recipient.domain_blocking?(@notification.from_account.domain) && !following_sender?
  86. end
  87. def blocked?
  88. blocked = @recipient.unavailable?
  89. blocked ||= from_self? && @notification.type != :poll
  90. return blocked if message? && from_staff?
  91. blocked ||= domain_blocking?
  92. blocked ||= @recipient.blocking?(@notification.from_account)
  93. blocked ||= @recipient.muting_notifications?(@notification.from_account)
  94. blocked ||= hellbanned?
  95. blocked ||= optional_non_follower?
  96. blocked ||= optional_non_following?
  97. blocked ||= optional_non_following_and_direct?
  98. blocked ||= conversation_muted?
  99. blocked ||= blocked_mention? if @notification.type == :mention
  100. blocked
  101. end
  102. def conversation_muted?
  103. if @notification.target_status
  104. @recipient.muting_conversation?(@notification.target_status.conversation)
  105. else
  106. false
  107. end
  108. end
  109. def push_notification!
  110. push_to_streaming_api! if subscribed_to_streaming_api?
  111. push_to_web_push_subscriptions!
  112. end
  113. def push_to_streaming_api!
  114. redis.publish("timeline:#{@recipient.id}:notifications", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification)))
  115. end
  116. def subscribed_to_streaming_api?
  117. redis.exists?("subscribed:timeline:#{@recipient.id}") || redis.exists?("subscribed:timeline:#{@recipient.id}:notifications")
  118. end
  119. def push_to_conversation!
  120. AccountConversation.add_status(@recipient, @notification.target_status)
  121. end
  122. def push_to_web_push_subscriptions!
  123. ::Web::PushNotificationWorker.push_bulk(web_push_subscriptions.select { |subscription| subscription.pushable?(@notification) }) { |subscription| [subscription.id, @notification.id] }
  124. end
  125. def web_push_subscriptions
  126. @web_push_subscriptions ||= ::Web::PushSubscription.where(user_id: @recipient.user.id).to_a
  127. end
  128. def subscribed_to_web_push?
  129. web_push_subscriptions.any?
  130. end
  131. def send_email!
  132. return unless NotificationMailer.respond_to?(@notification.type)
  133. NotificationMailer
  134. .with(recipient: @recipient, notification: @notification)
  135. .public_send(@notification.type)
  136. .deliver_later(wait: 2.minutes)
  137. end
  138. def email_needed?
  139. (!recipient_online? || always_send_emails?) && send_email_for_notification_type?
  140. end
  141. def recipient_online?
  142. subscribed_to_streaming_api? || subscribed_to_web_push?
  143. end
  144. def always_send_emails?
  145. @recipient.user.settings.always_send_emails
  146. end
  147. def send_email_for_notification_type?
  148. NON_EMAIL_TYPES.exclude?(@notification.type) && @recipient.user.settings["notification_emails.#{@notification.type}"]
  149. end
  150. end