The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

139 lines
4.3 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 optional_non_following_and_direct?
  48. direct_message? &&
  49. @recipient.user.settings.interactions['must_be_following_dm'] &&
  50. !following_sender? &&
  51. !response_to_recipient?
  52. end
  53. def hellbanned?
  54. @notification.from_account.silenced? && !following_sender?
  55. end
  56. def from_self?
  57. @recipient.id == @notification.from_account.id
  58. end
  59. def domain_blocking?
  60. @recipient.domain_blocking?(@notification.from_account.domain) && !following_sender?
  61. end
  62. def blocked?
  63. blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
  64. blocked ||= from_self? # Skip for interactions with self
  65. blocked ||= domain_blocking? # Skip for domain blocked accounts
  66. blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts
  67. blocked ||= @recipient.muting_notifications?(@notification.from_account)
  68. blocked ||= hellbanned? # Hellban
  69. blocked ||= optional_non_follower? # Options
  70. blocked ||= optional_non_following? # Options
  71. blocked ||= optional_non_following_and_direct? # Options
  72. blocked ||= conversation_muted?
  73. blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
  74. blocked
  75. end
  76. def conversation_muted?
  77. if @notification.target_status
  78. @recipient.muting_conversation?(@notification.target_status.conversation)
  79. else
  80. false
  81. end
  82. end
  83. def create_notification!
  84. @notification.save!
  85. end
  86. def push_notification!
  87. return if @notification.activity.nil?
  88. Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification)))
  89. send_push_notifications!
  90. end
  91. def push_to_conversation!
  92. return if @notification.activity.nil?
  93. AccountConversation.add_status(@recipient, @notification.target_status)
  94. end
  95. def send_push_notifications!
  96. subscriptions_ids = ::Web::PushSubscription.where(user_id: @recipient.user.id)
  97. .select { |subscription| subscription.pushable?(@notification) }
  98. .map(&:id)
  99. ::Web::PushNotificationWorker.push_bulk(subscriptions_ids) do |subscription_id|
  100. [subscription_id, @notification.id]
  101. end
  102. end
  103. def send_email!
  104. return if @notification.activity.nil?
  105. NotificationMailer.public_send(@notification.type, @recipient, @notification).deliver_later(wait: 2.minutes)
  106. end
  107. def email_enabled?
  108. @recipient.user.settings.notification_emails[@notification.type.to_s]
  109. end
  110. end