闭社主体 forked from https://github.com/tootsuite/mastodon
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.

58 lines
2.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 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)
  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?
  27. blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
  28. blocked ||= @recipient.id == @notification.from_account.id # Skip for interactions with self
  29. blocked ||= @recipient.blocking?(@notification.from_account) # Skip for blocked accounts
  30. blocked ||= (@notification.from_account.silenced? && !@recipient.following?(@notification.from_account)) # Hellban
  31. blocked ||= (@recipient.user.settings(:interactions).must_be_follower && !@notification.from_account.following?(@recipient)) # Options
  32. blocked ||= (@recipient.user.settings(:interactions).must_be_following && !@recipient.following?(@notification.from_account)) # Options
  33. blocked ||= send("blocked_#{@notification.type}?") # Type-dependent filters
  34. blocked
  35. end
  36. def create_notification
  37. @notification.save!
  38. FeedManager.instance.broadcast(@recipient.id, type: 'notification', message: FeedManager.instance.inline_render(@recipient, 'api/v1/notifications/show', @notification))
  39. end
  40. def send_email
  41. NotificationMailer.send(@notification.type, @recipient, @notification).deliver_later
  42. end
  43. def email_enabled?
  44. @recipient.user.settings(:notification_emails).send(@notification.type)
  45. end
  46. end