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.
 
 
 
 

73 lines
2.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. 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 ||= conversation_muted?
  37. blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
  38. blocked
  39. end
  40. def conversation_muted?
  41. if @notification.target_status
  42. @recipient.muting_conversation?(@notification.target_status.conversation)
  43. else
  44. false
  45. end
  46. end
  47. def create_notification
  48. @notification.save!
  49. return unless @notification.browserable?
  50. Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, 'api/v1/notifications/show')))
  51. end
  52. def send_email
  53. NotificationMailer.public_send(@notification.type, @recipient, @notification).deliver_later
  54. end
  55. def email_enabled?
  56. @recipient.user.settings.notification_emails[@notification.type.to_s]
  57. end
  58. end