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.

109 lines
3.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 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. if status.direct_visibility?
  9. deliver_to_mentioned_followers(status)
  10. deliver_to_own_conversation(status)
  11. elsif status.limited_visibility?
  12. deliver_to_mentioned_followers(status)
  13. else
  14. deliver_to_followers(status)
  15. deliver_to_lists(status)
  16. end
  17. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  18. render_anonymous_payload(status)
  19. deliver_to_hashtags(status)
  20. return if status.reply? && status.in_reply_to_account_id != status.account_id
  21. deliver_to_public(status)
  22. deliver_to_media(status) if status.media_attachments.any?
  23. end
  24. private
  25. def deliver_to_self(status)
  26. Rails.logger.debug "Delivering status #{status.id} to author"
  27. FeedManager.instance.push_to_home(status.account, status)
  28. end
  29. def deliver_to_followers(status)
  30. Rails.logger.debug "Delivering status #{status.id} to followers"
  31. status.account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
  32. FeedInsertWorker.push_bulk(followers) do |follower|
  33. [status.id, follower.id, :home]
  34. end
  35. end
  36. end
  37. def deliver_to_lists(status)
  38. Rails.logger.debug "Delivering status #{status.id} to lists"
  39. status.account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
  40. FeedInsertWorker.push_bulk(lists) do |list|
  41. [status.id, list.id, :list]
  42. end
  43. end
  44. end
  45. def deliver_to_mentioned_followers(status)
  46. Rails.logger.debug "Delivering status #{status.id} to limited followers"
  47. status.mentions.joins(:account).merge(status.account.followers_for_local_distribution).select(:id, :account_id).reorder(nil).find_in_batches do |mentions|
  48. FeedInsertWorker.push_bulk(mentions) do |mention|
  49. [status.id, mention.account_id, :home]
  50. end
  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.mb_chars.downcase}", @payload)
  61. Redis.current.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}: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. if status.local?
  68. Redis.current.publish('timeline:public:local', @payload)
  69. else
  70. Redis.current.publish('timeline:public:remote', @payload)
  71. end
  72. end
  73. def deliver_to_media(status)
  74. Rails.logger.debug "Delivering status #{status.id} to media timeline"
  75. Redis.current.publish('timeline:public:media', @payload)
  76. if status.local?
  77. Redis.current.publish('timeline:public:local:media', @payload)
  78. else
  79. Redis.current.publish('timeline:public:remote:media', @payload)
  80. end
  81. end
  82. def deliver_to_own_conversation(status)
  83. AccountConversation.add_status(status.account, status)
  84. end
  85. end