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.

152 lines
4.7 KiB

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