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

97 lines
3.1 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. render_anonymous_payload(status)
  8. if status.direct_visibility?
  9. deliver_to_own_conversation(status)
  10. elsif status.limited_visibility?
  11. deliver_to_mentioned_followers(status)
  12. else
  13. deliver_to_self(status) if status.account.local?
  14. deliver_to_followers(status)
  15. deliver_to_lists(status)
  16. end
  17. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  18. deliver_to_hashtags(status)
  19. return if status.reply? && status.in_reply_to_account_id != status.account_id
  20. deliver_to_public(status)
  21. deliver_to_media(status) if status.media_attachments.any?
  22. end
  23. private
  24. def deliver_to_self(status)
  25. Rails.logger.debug "Delivering status #{status.id} to author"
  26. FeedManager.instance.push_to_home(status.account, status)
  27. end
  28. def deliver_to_followers(status)
  29. Rails.logger.debug "Delivering status #{status.id} to followers"
  30. status.account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
  31. FeedInsertWorker.push_bulk(followers) do |follower|
  32. [status.id, follower.id, :home]
  33. end
  34. end
  35. end
  36. def deliver_to_lists(status)
  37. Rails.logger.debug "Delivering status #{status.id} to lists"
  38. status.account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
  39. FeedInsertWorker.push_bulk(lists) do |list|
  40. [status.id, list.id, :list]
  41. end
  42. end
  43. end
  44. def deliver_to_mentioned_followers(status)
  45. Rails.logger.debug "Delivering status #{status.id} to limited followers"
  46. FeedInsertWorker.push_bulk(status.mentions.includes(:account).map(&:account).select { |mentioned_account| mentioned_account.local? && mentioned_account.following?(status.account) }) do |follower|
  47. [status.id, follower.id, :home]
  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_media(status)
  67. Rails.logger.debug "Delivering status #{status.id} to media timeline"
  68. Redis.current.publish('timeline:public:media', @payload)
  69. Redis.current.publish('timeline:public:local:media', @payload) if status.local?
  70. end
  71. def deliver_to_own_conversation(status)
  72. AccountConversation.add_status(status.account, status)
  73. end
  74. end