fan_out_on_write_service.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. fan_out_to_local_recipients!
  15. fan_out_to_public_streams! if broadcastable?
  16. end
  17. private
  18. def check_race_condition!
  19. # I don't know why but at some point we had an issue where
  20. # this service was being executed with status objects
  21. # that had a null visibility - which should not be possible
  22. # since the column in the database is not nullable.
  23. #
  24. # This check re-queues the service to be run at a later time
  25. # with the full object, if something like it occurs
  26. raise Mastodon::RaceConditionError if @status.visibility.nil?
  27. end
  28. def fan_out_to_local_recipients!
  29. deliver_to_self!
  30. notify_mentioned_accounts!
  31. notify_about_update! if update?
  32. case @status.visibility.to_sym
  33. when :public, :unlisted, :private
  34. deliver_to_all_followers!
  35. deliver_to_lists!
  36. when :limited
  37. deliver_to_mentioned_followers!
  38. else
  39. deliver_to_mentioned_followers!
  40. deliver_to_conversation!
  41. end
  42. end
  43. def fan_out_to_public_streams!
  44. broadcast_to_hashtag_streams!
  45. broadcast_to_public_streams!
  46. end
  47. def deliver_to_self!
  48. FeedManager.instance.push_to_home(@account, @status, update: update?) if @account.local?
  49. end
  50. def notify_mentioned_accounts!
  51. @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|
  52. LocalNotificationWorker.push_bulk(mentions) do |mention|
  53. [mention.account_id, mention.id, 'Mention', 'mention']
  54. end
  55. end
  56. end
  57. def notify_about_update!
  58. @status.reblogged_by_accounts.merge(Account.local).select(:id).reorder(nil).find_in_batches do |accounts|
  59. LocalNotificationWorker.push_bulk(accounts) do |account|
  60. [account.id, @status.id, 'Status', 'update']
  61. end
  62. end
  63. end
  64. def deliver_to_all_followers!
  65. @account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
  66. FeedInsertWorker.push_bulk(followers) do |follower|
  67. [@status.id, follower.id, 'home', { 'update' => update? }]
  68. end
  69. end
  70. end
  71. def deliver_to_lists!
  72. @account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
  73. FeedInsertWorker.push_bulk(lists) do |list|
  74. [@status.id, list.id, 'list', { 'update' => update? }]
  75. end
  76. end
  77. end
  78. def deliver_to_mentioned_followers!
  79. @status.mentions.joins(:account).merge(@account.followers_for_local_distribution).select(:id, :account_id).reorder(nil).find_in_batches do |mentions|
  80. FeedInsertWorker.push_bulk(mentions) do |mention|
  81. [@status.id, mention.account_id, 'home', { 'update' => update? }]
  82. end
  83. end
  84. end
  85. def broadcast_to_hashtag_streams!
  86. @status.tags.pluck(:name).each do |hashtag|
  87. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", anonymous_payload)
  88. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", anonymous_payload) if @status.local?
  89. end
  90. end
  91. def broadcast_to_public_streams!
  92. return if @status.reply? && @status.in_reply_to_account_id != @account.id
  93. redis.publish('timeline:public', anonymous_payload)
  94. redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', anonymous_payload)
  95. if @status.with_media?
  96. redis.publish('timeline:public:media', anonymous_payload)
  97. redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', anonymous_payload)
  98. end
  99. end
  100. def deliver_to_conversation!
  101. AccountConversation.add_status(@account, @status) unless update?
  102. end
  103. def anonymous_payload
  104. @anonymous_payload ||= Oj.dump(
  105. event: update? ? :'status.update' : :update,
  106. payload: InlineRenderer.render(@status, nil, :status)
  107. )
  108. end
  109. def update?
  110. @options[:update]
  111. end
  112. def broadcastable?
  113. @status.public_visibility? && !@status.reblog? && !@account.silenced?
  114. end
  115. end