fan_out_on_write_service.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # frozen_string_literal: true
  2. class FanOutOnWriteService < BaseService
  3. include Redisable
  4. # Push a status into home and mentions feeds
  5. # @param [Status] status
  6. # @param [Hash] options
  7. # @option options [Boolean] update
  8. # @option options [Array<Integer>] silenced_account_ids
  9. def call(status, options = {})
  10. @status = status
  11. @account = status.account
  12. @options = options
  13. check_race_condition!
  14. warm_payload_cache!
  15. fan_out_to_local_recipients!
  16. fan_out_to_public_recipients! if broadcastable?
  17. fan_out_to_public_streams! if broadcastable?
  18. end
  19. private
  20. def check_race_condition!
  21. # I don't know why but at some point we had an issue where
  22. # this service was being executed with status objects
  23. # that had a null visibility - which should not be possible
  24. # since the column in the database is not nullable.
  25. #
  26. # This check re-queues the service to be run at a later time
  27. # with the full object, if something like it occurs
  28. raise Mastodon::RaceConditionError if @status.visibility.nil?
  29. end
  30. def fan_out_to_local_recipients!
  31. deliver_to_self!
  32. notify_mentioned_accounts!
  33. notify_about_update! if update?
  34. case @status.visibility.to_sym
  35. when :public, :unlisted, :private
  36. deliver_to_all_followers!
  37. deliver_to_lists!
  38. when :limited
  39. deliver_to_mentioned_followers!
  40. else
  41. deliver_to_mentioned_followers!
  42. deliver_to_conversation!
  43. end
  44. end
  45. def fan_out_to_public_recipients!
  46. deliver_to_hashtag_followers!
  47. end
  48. def fan_out_to_public_streams!
  49. broadcast_to_hashtag_streams!
  50. broadcast_to_public_streams!
  51. end
  52. def deliver_to_self!
  53. FeedManager.instance.push_to_home(@account, @status, update: update?) if @account.local?
  54. end
  55. def notify_mentioned_accounts!
  56. @status.active_mentions.where.not(id: @options[:silenced_account_ids] || []).joins(:account).merge(Account.local).select(:id, :account_id).reorder(nil).find_in_batches do |mentions|
  57. LocalNotificationWorker.push_bulk(mentions) do |mention|
  58. [mention.account_id, mention.id, 'Mention', 'mention']
  59. end
  60. end
  61. end
  62. def notify_about_update!
  63. @status.reblogged_by_accounts.merge(Account.local).select(:id).reorder(nil).find_in_batches do |accounts|
  64. LocalNotificationWorker.push_bulk(accounts) do |account|
  65. [account.id, @status.id, 'Status', 'update']
  66. end
  67. end
  68. end
  69. def deliver_to_all_followers!
  70. @account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
  71. FeedInsertWorker.push_bulk(followers) do |follower|
  72. [@status.id, follower.id, 'home', { 'update' => update? }]
  73. end
  74. end
  75. end
  76. def deliver_to_hashtag_followers!
  77. TagFollow.where(tag_id: @status.tags.map(&:id)).select(:id, :account_id).reorder(nil).find_in_batches do |follows|
  78. FeedInsertWorker.push_bulk(follows) do |follow|
  79. [@status.id, follow.account_id, 'tags', { 'update' => update? }]
  80. end
  81. end
  82. end
  83. def deliver_to_lists!
  84. @account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
  85. FeedInsertWorker.push_bulk(lists) do |list|
  86. [@status.id, list.id, 'list', { 'update' => update? }]
  87. end
  88. end
  89. end
  90. def deliver_to_mentioned_followers!
  91. @status.mentions.joins(:account).merge(@account.followers_for_local_distribution).select(:id, :account_id).reorder(nil).find_in_batches do |mentions|
  92. FeedInsertWorker.push_bulk(mentions) do |mention|
  93. [@status.id, mention.account_id, 'home', { 'update' => update? }]
  94. end
  95. end
  96. end
  97. def broadcast_to_hashtag_streams!
  98. @status.tags.map(&:name).each do |hashtag|
  99. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", anonymous_payload)
  100. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", anonymous_payload) if @status.local?
  101. end
  102. end
  103. def broadcast_to_public_streams!
  104. return if @status.reply? && @status.in_reply_to_account_id != @account.id
  105. redis.publish('timeline:public', anonymous_payload)
  106. redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', anonymous_payload)
  107. if @status.with_media?
  108. redis.publish('timeline:public:media', anonymous_payload)
  109. redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', anonymous_payload)
  110. end
  111. end
  112. def deliver_to_conversation!
  113. AccountConversation.add_status(@account, @status) unless update?
  114. end
  115. def warm_payload_cache!
  116. Rails.cache.write("fan-out/#{@status.id}", rendered_status)
  117. end
  118. def anonymous_payload
  119. @anonymous_payload ||= Oj.dump(
  120. event: update? ? :'status.update' : :update,
  121. payload: rendered_status
  122. )
  123. end
  124. def rendered_status
  125. @rendered_status ||= InlineRenderer.render(@status, nil, :status)
  126. end
  127. def update?
  128. @options[:update]
  129. end
  130. def broadcastable?
  131. @status.public_visibility? && !@status.reblog? && !@account.silenced?
  132. end
  133. end