闭社主体 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.

96 lines
3.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  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. raise Mastodon::RaceConditionError if status.visibility.nil?
  7. deliver_to_self(status) if status.account.local?
  8. render_anonymous_payload(status)
  9. if status.direct_visibility?
  10. deliver_to_mentioned_followers(status)
  11. deliver_to_direct_timelines(status)
  12. else
  13. deliver_to_followers(status)
  14. deliver_to_lists(status)
  15. end
  16. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  17. deliver_to_hashtags(status)
  18. return if status.reply? && status.in_reply_to_account_id != status.account_id
  19. deliver_to_public(status)
  20. end
  21. private
  22. def deliver_to_self(status)
  23. Rails.logger.debug "Delivering status #{status.id} to author"
  24. FeedManager.instance.push_to_home(status.account, status)
  25. end
  26. def deliver_to_followers(status)
  27. Rails.logger.debug "Delivering status #{status.id} to followers"
  28. status.account.followers.where(domain: nil).joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).select(:id).reorder(nil).find_in_batches do |followers|
  29. FeedInsertWorker.push_bulk(followers) do |follower|
  30. [status.id, follower.id, :home]
  31. end
  32. end
  33. end
  34. def deliver_to_lists(status)
  35. Rails.logger.debug "Delivering status #{status.id} to lists"
  36. status.account.lists.joins(account: :user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).select(:id).reorder(nil).find_in_batches do |lists|
  37. FeedInsertWorker.push_bulk(lists) do |list|
  38. [status.id, list.id, :list]
  39. end
  40. end
  41. end
  42. def deliver_to_mentioned_followers(status)
  43. Rails.logger.debug "Delivering status #{status.id} to mentioned followers"
  44. status.mentions.includes(:account).each do |mention|
  45. mentioned_account = mention.account
  46. next if !mentioned_account.local? || !mentioned_account.following?(status.account) || FeedManager.instance.filter?(:home, status, mention.account_id)
  47. FeedManager.instance.push_to_home(mentioned_account, status)
  48. end
  49. end
  50. def render_anonymous_payload(status)
  51. @payload = InlineRenderer.render(status, nil, :status)
  52. @payload = Oj.dump(event: :update, payload: @payload)
  53. end
  54. def deliver_to_hashtags(status)
  55. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  56. status.tags.pluck(:name).each do |hashtag|
  57. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  58. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if status.local?
  59. end
  60. end
  61. def deliver_to_public(status)
  62. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  63. Redis.current.publish('timeline:public', @payload)
  64. Redis.current.publish('timeline:public:local', @payload) if status.local?
  65. end
  66. def deliver_to_direct_timelines(status)
  67. Rails.logger.debug "Delivering status #{status.id} to direct timelines"
  68. status.mentions.includes(:account).each do |mention|
  69. Redis.current.publish("timeline:direct:#{mention.account.id}", @payload) if mention.account.local?
  70. end
  71. Redis.current.publish("timeline:direct:#{status.account.id}", @payload) if status.account.local?
  72. end
  73. end