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.

111 lines
3.7 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. deliver_to_own_conversation(status)
  13. elsif status.limited_visibility?
  14. deliver_to_mentioned_followers(status)
  15. else
  16. deliver_to_followers(status)
  17. deliver_to_lists(status)
  18. end
  19. return if status.account.silenced? || !status.public_visibility?
  20. return if status.reblog? && !Setting.show_reblogs_in_public_timelines
  21. deliver_to_hashtags(status)
  22. return if status.reply? && status.in_reply_to_account_id != status.account_id && !Setting.show_replies_in_public_timelines
  23. deliver_to_public(status)
  24. deliver_to_media(status) if status.media_attachments.any?
  25. end
  26. private
  27. def deliver_to_self(status)
  28. Rails.logger.debug "Delivering status #{status.id} to author"
  29. FeedManager.instance.push_to_home(status.account, status)
  30. end
  31. def deliver_to_followers(status)
  32. Rails.logger.debug "Delivering status #{status.id} to followers"
  33. status.account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
  34. FeedInsertWorker.push_bulk(followers) do |follower|
  35. [status.id, follower.id, :home]
  36. end
  37. end
  38. end
  39. def deliver_to_lists(status)
  40. Rails.logger.debug "Delivering status #{status.id} to lists"
  41. status.account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
  42. FeedInsertWorker.push_bulk(lists) do |list|
  43. [status.id, list.id, :list]
  44. end
  45. end
  46. end
  47. def deliver_to_mentioned_followers(status)
  48. Rails.logger.debug "Delivering status #{status.id} to limited followers"
  49. FeedInsertWorker.push_bulk(status.mentions.includes(:account).map(&:account).select { |mentioned_account| mentioned_account.local? && mentioned_account.following?(status.account) }) do |follower|
  50. [status.id, follower.id, :home]
  51. end
  52. end
  53. def render_anonymous_payload(status)
  54. @payload = InlineRenderer.render(status, nil, :status)
  55. @payload = Oj.dump(event: :update, payload: @payload)
  56. end
  57. def deliver_to_hashtags(status)
  58. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  59. status.tags.pluck(:name).each do |hashtag|
  60. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  61. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if status.local?
  62. end
  63. end
  64. def deliver_to_public(status)
  65. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  66. Redis.current.publish('timeline:public', @payload)
  67. Redis.current.publish('timeline:public:local', @payload) if status.local?
  68. end
  69. def deliver_to_media(status)
  70. Rails.logger.debug "Delivering status #{status.id} to media timeline"
  71. Redis.current.publish('timeline:public:media', @payload)
  72. Redis.current.publish('timeline:public:local:media', @payload) if status.local?
  73. end
  74. def deliver_to_direct_timelines(status)
  75. Rails.logger.debug "Delivering status #{status.id} to direct timelines"
  76. status.mentions.includes(:account).each do |mention|
  77. Redis.current.publish("timeline:direct:#{mention.account.id}", @payload) if mention.account.local?
  78. end
  79. Redis.current.publish("timeline:direct:#{status.account.id}", @payload) if status.account.local?
  80. end
  81. def deliver_to_own_conversation(status)
  82. AccountConversation.add_status(status.account, status)
  83. end
  84. end