The code powering m.abunchtell.com https://m.abunchtell.com
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

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