fan_out_on_write_service.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # frozen_string_literal: true
  2. class FanOutOnWriteService < BaseService
  3. # Push a status into home and mentions feeds
  4. # @param [Status] status
  5. def call(status)
  6. deliver_to_self(status) if status.account.local?
  7. deliver_to_followers(status)
  8. deliver_to_mentioned(status)
  9. return if status.account.silenced? || !status.public_visibility? || status.reblog? || (status.reply? && status.in_reply_to_account_id != status.account_id)
  10. deliver_to_hashtags(status)
  11. deliver_to_public(status)
  12. end
  13. private
  14. def deliver_to_self(status)
  15. Rails.logger.debug "Delivering status #{status.id} to author"
  16. FeedManager.instance.push(:home, status.account, status)
  17. end
  18. def deliver_to_followers(status)
  19. Rails.logger.debug "Delivering status #{status.id} to followers"
  20. status.account.followers.where(domain: nil).joins(:user).where('users.current_sign_in_at > ?', 14.days.ago).find_each do |follower|
  21. next if FeedManager.instance.filter?(:home, status, follower)
  22. FeedManager.instance.push(:home, follower, status)
  23. end
  24. end
  25. def deliver_to_mentioned(status)
  26. Rails.logger.debug "Delivering status #{status.id} to mentioned accounts"
  27. status.mentions.includes(:account).each do |mention|
  28. mentioned_account = mention.account
  29. next if !mentioned_account.local? || FeedManager.instance.filter?(:mentions, status, mentioned_account)
  30. FeedManager.instance.push(:mentions, mentioned_account, status)
  31. end
  32. end
  33. def deliver_to_hashtags(status)
  34. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  35. status.tags.find_each do |tag|
  36. FeedManager.instance.broadcast("hashtag:#{tag.name}", type: 'update', id: status.id)
  37. end
  38. end
  39. def deliver_to_public(status)
  40. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  41. FeedManager.instance.broadcast(:public, type: 'update', id: status.id)
  42. end
  43. end