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.

63 lines
2.2 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. 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.blocking?(@notification.from_account) # Skip for blocked accounts
  33. blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban
  34. blocked ||= (@recipient.user.settings.interactions['must_be_follower'] && !@notification.from_account.following?(@recipient)) # Options
  35. blocked ||= (@recipient.user.settings.interactions['must_be_following'] && !@recipient.following?(@notification.from_account)) # Options
  36. blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
  37. blocked
  38. end
  39. def create_notification
  40. @notification.save!
  41. return unless @notification.browserable?
  42. Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, 'api/v1/notifications/show')))
  43. end
  44. def send_email
  45. NotificationMailer.send(@notification.type, @recipient, @notification).deliver_later
  46. end
  47. def email_enabled?
  48. @recipient.user.settings.notification_emails[@notification.type]
  49. end
  50. end