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.

174 lines
5.2 KiB

  1. # frozen_string_literal: true
  2. class RemoveStatusService < BaseService
  3. include Redisable
  4. include Payloadable
  5. # Delete a status
  6. # @param [Status] status
  7. # @param [Hash] options
  8. # @option [Boolean] :redraft
  9. # @option [Boolean] :immediate
  10. # @option [Boolean] :original_removed
  11. def call(status, **options)
  12. @payload = Oj.dump(event: :delete, payload: status.id.to_s)
  13. @status = status
  14. @account = status.account
  15. @tags = status.tags.pluck(:name).to_a
  16. @mentions = status.active_mentions.includes(:account).to_a
  17. @reblogs = status.reblogs.includes(:account).to_a
  18. @options = options
  19. RedisLock.acquire(lock_options) do |lock|
  20. if lock.acquired?
  21. remove_from_self if status.account.local?
  22. remove_from_followers
  23. remove_from_lists
  24. remove_from_affected
  25. remove_reblogs
  26. remove_from_hashtags
  27. remove_from_public
  28. remove_from_media if status.media_attachments.any?
  29. remove_from_spam_check
  30. remove_media
  31. @status.destroy! if @options[:immediate] || !@status.reported?
  32. else
  33. raise Mastodon::RaceConditionError
  34. end
  35. end
  36. # There is no reason to send out Undo activities when the
  37. # cause is that the original object has been removed, since
  38. # original object being removed implicitly removes reblogs
  39. # of it. The Delete activity of the original is forwarded
  40. # separately.
  41. return if !@account.local? || @options[:original_removed]
  42. remove_from_remote_followers
  43. remove_from_remote_affected
  44. end
  45. private
  46. def remove_from_self
  47. FeedManager.instance.unpush_from_home(@account, @status)
  48. end
  49. def remove_from_followers
  50. @account.followers_for_local_distribution.reorder(nil).find_each do |follower|
  51. FeedManager.instance.unpush_from_home(follower, @status)
  52. end
  53. end
  54. def remove_from_lists
  55. @account.lists_for_local_distribution.select(:id, :account_id).reorder(nil).find_each do |list|
  56. FeedManager.instance.unpush_from_list(list, @status)
  57. end
  58. end
  59. def remove_from_affected
  60. @mentions.map(&:account).select(&:local?).each do |account|
  61. redis.publish("timeline:#{account.id}", @payload)
  62. end
  63. end
  64. def remove_from_remote_affected
  65. # People who got mentioned in the status, or who
  66. # reblogged it from someone else might not follow
  67. # the author and wouldn't normally receive the
  68. # delete notification - so here, we explicitly
  69. # send it to them
  70. target_accounts = (@mentions.map(&:account).reject(&:local?) + @reblogs.map(&:account).reject(&:local?))
  71. target_accounts << @status.reblog.account if @status.reblog? && !@status.reblog.account.local?
  72. target_accounts.uniq!(&:id)
  73. # ActivityPub
  74. ActivityPub::DeliveryWorker.push_bulk(target_accounts.select(&:activitypub?).uniq(&:preferred_inbox_url)) do |target_account|
  75. [signed_activity_json, @account.id, target_account.preferred_inbox_url]
  76. end
  77. end
  78. def remove_from_remote_followers
  79. # ActivityPub
  80. ActivityPub::DeliveryWorker.push_bulk(@account.followers.inboxes) do |inbox_url|
  81. [signed_activity_json, @account.id, inbox_url]
  82. end
  83. relay! if relayable?
  84. end
  85. def relayable?
  86. @status.public_visibility?
  87. end
  88. def relay!
  89. ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
  90. [signed_activity_json, @account.id, inbox_url]
  91. end
  92. end
  93. def signed_activity_json
  94. @signed_activity_json ||= Oj.dump(serialize_payload(@status, @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer, signer: @account))
  95. end
  96. def remove_reblogs
  97. # We delete reblogs of the status before the original status,
  98. # because once original status is gone, reblogs will disappear
  99. # without us being able to do all the fancy stuff
  100. @reblogs.each do |reblog|
  101. RemoveStatusService.new.call(reblog, original_removed: true)
  102. end
  103. end
  104. def remove_from_hashtags
  105. @account.featured_tags.where(tag_id: @status.tags.pluck(:id)).each do |featured_tag|
  106. featured_tag.decrement(@status.id)
  107. end
  108. return unless @status.public_visibility?
  109. @tags.each do |hashtag|
  110. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", @payload)
  111. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", @payload) if @status.local?
  112. end
  113. end
  114. def remove_from_public
  115. return unless @status.public_visibility?
  116. redis.publish('timeline:public', @payload)
  117. if @status.local?
  118. redis.publish('timeline:public:local', @payload)
  119. else
  120. redis.publish('timeline:public:remote', @payload)
  121. end
  122. end
  123. def remove_from_media
  124. return unless @status.public_visibility?
  125. redis.publish('timeline:public:media', @payload)
  126. if @status.local?
  127. redis.publish('timeline:public:local:media', @payload)
  128. else
  129. redis.publish('timeline:public:remote:media', @payload)
  130. end
  131. end
  132. def remove_media
  133. return if @options[:redraft] || (!@options[:immediate] && @status.reported?)
  134. @status.media_attachments.destroy_all
  135. end
  136. def remove_from_spam_check
  137. redis.zremrangebyscore("spam_check:#{@status.account_id}", @status.id, @status.id)
  138. end
  139. def lock_options
  140. { redis: Redis.current, key: "distribute:#{@status.id}" }
  141. end
  142. end