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.

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