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.

137 lines
4.3 KiB

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