notify_service.rb 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. send_email if email_enabled?
  10. rescue ActiveRecord::RecordInvalid
  11. return
  12. end
  13. private
  14. def blocked_mention?
  15. FeedManager.instance.filter?(:mentions, @notification.mention.status, @recipient.id)
  16. end
  17. def blocked_favourite?
  18. false
  19. end
  20. def blocked_follow?
  21. false
  22. end
  23. def blocked_reblog?
  24. false
  25. end
  26. def blocked_follow_request?
  27. false
  28. end
  29. def blocked?
  30. blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
  31. blocked ||= @recipient.id == @notification.from_account.id # Skip for interactions with self
  32. blocked ||= @recipient.domain_blocking?(@notification.from_account.domain) && !@recipient.following?(@notification.from_account) # Skip for domain blocked accounts
  33. blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts
  34. blocked ||= @recipient.muting?(@notification.from_account) # Skip for muted accounts
  35. blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban
  36. blocked ||= (@recipient.user.settings.interactions['must_be_follower'] && !@notification.from_account.following?(@recipient)) # Options
  37. blocked ||= (@recipient.user.settings.interactions['must_be_following'] && !@recipient.following?(@notification.from_account)) # Options
  38. blocked ||= conversation_muted?
  39. blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
  40. blocked
  41. end
  42. def conversation_muted?
  43. if @notification.target_status
  44. @recipient.muting_conversation?(@notification.target_status.conversation)
  45. else
  46. false
  47. end
  48. end
  49. def create_notification
  50. @notification.save!
  51. return unless @notification.browserable?
  52. Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification)))
  53. send_push_notifications
  54. end
  55. def send_push_notifications
  56. # HACK: Can be caused by quickly unfavouriting a status, since creating
  57. # a favourite and creating a notification are not wrapped in a transaction.
  58. return if @notification.activity.nil?
  59. sessions_with_subscriptions = @recipient.user.session_activations.where.not(web_push_subscription: nil)
  60. sessions_with_subscriptions_ids = sessions_with_subscriptions.select { |session| session.web_push_subscription.pushable? @notification }.map(&:id)
  61. WebPushNotificationWorker.push_bulk(sessions_with_subscriptions_ids) do |session_activation_id|
  62. [session_activation_id, @notification.id]
  63. end
  64. end
  65. def send_email
  66. NotificationMailer.public_send(@notification.type, @recipient, @notification).deliver_later
  67. end
  68. def email_enabled?
  69. @recipient.user.settings.notification_emails[@notification.type.to_s]
  70. end
  71. end