fan_out_on_write_service.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_own_conversation(status)
  10. elsif status.limited_visibility?
  11. deliver_to_mentioned_followers(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. render_anonymous_payload(status)
  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. status.mentions.joins(:account).merge(status.account.followers_for_local_distribution).select(:id, :account_id).reorder(nil).find_in_batches do |mentions|
  47. FeedInsertWorker.push_bulk(mentions) do |mention|
  48. [status.id, mention.account_id, :home]
  49. end
  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.mb_chars.downcase}", @payload)
  60. Redis.current.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}: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. if status.local?
  67. Redis.current.publish('timeline:public:local', @payload)
  68. else
  69. Redis.current.publish('timeline:public:remote', @payload)
  70. end
  71. end
  72. def deliver_to_media(status)
  73. Rails.logger.debug "Delivering status #{status.id} to media timeline"
  74. Redis.current.publish('timeline:public:media', @payload)
  75. if status.local?
  76. Redis.current.publish('timeline:public:local:media', @payload)
  77. else
  78. Redis.current.publish('timeline:public:remote:media', @payload)
  79. end
  80. end
  81. def deliver_to_own_conversation(status)
  82. AccountConversation.add_status(status.account, status)
  83. end
  84. end