The code powering m.abunchtell.com https://m.abunchtell.com
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

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