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.

173 lines
5.3 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. @options = options
  16. RedisLock.acquire(lock_options) do |lock|
  17. if lock.acquired?
  18. remove_from_self if @account.local?
  19. remove_from_followers
  20. remove_from_lists
  21. # There is no reason to send out Undo activities when the
  22. # cause is that the original object has been removed, since
  23. # original object being removed implicitly removes reblogs
  24. # of it. The Delete activity of the original is forwarded
  25. # separately.
  26. if @account.local? && !@options[:original_removed]
  27. remove_from_remote_followers
  28. remove_from_remote_reach
  29. end
  30. # Since reblogs don't mention anyone, don't get reblogged,
  31. # favourited and don't contain their own media attachments
  32. # or hashtags, this can be skipped
  33. unless @status.reblog?
  34. remove_from_mentions
  35. remove_reblogs
  36. remove_from_hashtags
  37. remove_from_public
  38. remove_from_media if @status.media_attachments.any?
  39. remove_from_spam_check
  40. remove_media
  41. end
  42. @status.destroy! if @options[:immediate] || !@status.reported?
  43. else
  44. raise Mastodon::RaceConditionError
  45. end
  46. end
  47. end
  48. private
  49. def remove_from_self
  50. FeedManager.instance.unpush_from_home(@account, @status)
  51. end
  52. def remove_from_followers
  53. @account.followers_for_local_distribution.reorder(nil).find_each do |follower|
  54. FeedManager.instance.unpush_from_home(follower, @status)
  55. end
  56. end
  57. def remove_from_lists
  58. @account.lists_for_local_distribution.select(:id, :account_id).reorder(nil).find_each do |list|
  59. FeedManager.instance.unpush_from_list(list, @status)
  60. end
  61. end
  62. def remove_from_mentions
  63. # For limited visibility statuses, the mentions that determine
  64. # who receives them in their home feed are a subset of followers
  65. # and therefore the delete is already handled by sending it to all
  66. # followers. Here we send a delete to actively mentioned accounts
  67. # that may not follow the account
  68. @status.active_mentions.find_each do |mention|
  69. redis.publish("timeline:#{mention.account_id}", @payload)
  70. end
  71. end
  72. def remove_from_remote_reach
  73. return if @status.reblog?
  74. # People who got mentioned in the status, or who
  75. # reblogged it from someone else might not follow
  76. # the author and wouldn't normally receive the
  77. # delete notification - so here, we explicitly
  78. # send it to them
  79. status_reach_finder = StatusReachFinder.new(@status)
  80. ActivityPub::DeliveryWorker.push_bulk(status_reach_finder.inboxes) do |inbox_url|
  81. [signed_activity_json, @account.id, inbox_url]
  82. end
  83. end
  84. def remove_from_remote_followers
  85. ActivityPub::DeliveryWorker.push_bulk(@account.followers.inboxes) do |inbox_url|
  86. [signed_activity_json, @account.id, inbox_url]
  87. end
  88. relay! if relayable?
  89. end
  90. def relayable?
  91. @status.public_visibility?
  92. end
  93. def relay!
  94. ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
  95. [signed_activity_json, @account.id, inbox_url]
  96. end
  97. end
  98. def signed_activity_json
  99. @signed_activity_json ||= Oj.dump(serialize_payload(@status, @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer, signer: @account))
  100. end
  101. def remove_reblogs
  102. # We delete reblogs of the status before the original status,
  103. # because once original status is gone, reblogs will disappear
  104. # without us being able to do all the fancy stuff
  105. @status.reblogs.includes(:account).find_each do |reblog|
  106. RemoveStatusService.new.call(reblog, original_removed: true)
  107. end
  108. end
  109. def remove_from_hashtags
  110. @account.featured_tags.where(tag_id: @status.tags.map(&:id)).each do |featured_tag|
  111. featured_tag.decrement(@status.id)
  112. end
  113. return unless @status.public_visibility?
  114. @status.tags.map(&:name).each do |hashtag|
  115. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", @payload)
  116. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", @payload) if @status.local?
  117. end
  118. end
  119. def remove_from_public
  120. return unless @status.public_visibility?
  121. redis.publish('timeline:public', @payload)
  122. redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', @payload)
  123. end
  124. def remove_from_media
  125. return unless @status.public_visibility?
  126. redis.publish('timeline:public:media', @payload)
  127. redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', @payload)
  128. end
  129. def remove_media
  130. return if @options[:redraft] || (!@options[:immediate] && @status.reported?)
  131. @status.media_attachments.destroy_all
  132. end
  133. def remove_from_spam_check
  134. redis.zremrangebyscore("spam_check:#{@status.account_id}", @status.id, @status.id)
  135. end
  136. def lock_options
  137. { redis: Redis.current, key: "distribute:#{@status.id}" }
  138. end
  139. end