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.

168 lines
5.1 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_media
  40. end
  41. @status.destroy! if @options[:immediate] || !@status.reported?
  42. else
  43. raise Mastodon::RaceConditionError
  44. end
  45. end
  46. end
  47. private
  48. def remove_from_self
  49. FeedManager.instance.unpush_from_home(@account, @status)
  50. end
  51. def remove_from_followers
  52. @account.followers_for_local_distribution.reorder(nil).find_each do |follower|
  53. FeedManager.instance.unpush_from_home(follower, @status)
  54. end
  55. end
  56. def remove_from_lists
  57. @account.lists_for_local_distribution.select(:id, :account_id).reorder(nil).find_each do |list|
  58. FeedManager.instance.unpush_from_list(list, @status)
  59. end
  60. end
  61. def remove_from_mentions
  62. # For limited visibility statuses, the mentions that determine
  63. # who receives them in their home feed are a subset of followers
  64. # and therefore the delete is already handled by sending it to all
  65. # followers. Here we send a delete to actively mentioned accounts
  66. # that may not follow the account
  67. @status.active_mentions.find_each do |mention|
  68. redis.publish("timeline:#{mention.account_id}", @payload)
  69. end
  70. end
  71. def remove_from_remote_reach
  72. return if @status.reblog?
  73. # People who got mentioned in the status, or who
  74. # reblogged it from someone else might not follow
  75. # the author and wouldn't normally receive the
  76. # delete notification - so here, we explicitly
  77. # send it to them
  78. status_reach_finder = StatusReachFinder.new(@status)
  79. ActivityPub::DeliveryWorker.push_bulk(status_reach_finder.inboxes) do |inbox_url|
  80. [signed_activity_json, @account.id, inbox_url]
  81. end
  82. end
  83. def remove_from_remote_followers
  84. ActivityPub::DeliveryWorker.push_bulk(@account.followers.inboxes) do |inbox_url|
  85. [signed_activity_json, @account.id, inbox_url]
  86. end
  87. relay! if relayable?
  88. end
  89. def relayable?
  90. @status.public_visibility?
  91. end
  92. def relay!
  93. ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
  94. [signed_activity_json, @account.id, inbox_url]
  95. end
  96. end
  97. def signed_activity_json
  98. @signed_activity_json ||= Oj.dump(serialize_payload(@status, @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer, signer: @account))
  99. end
  100. def remove_reblogs
  101. # We delete reblogs of the status before the original status,
  102. # because once original status is gone, reblogs will disappear
  103. # without us being able to do all the fancy stuff
  104. @status.reblogs.includes(:account).find_each do |reblog|
  105. RemoveStatusService.new.call(reblog, original_removed: true)
  106. end
  107. end
  108. def remove_from_hashtags
  109. @account.featured_tags.where(tag_id: @status.tags.map(&:id)).each do |featured_tag|
  110. featured_tag.decrement(@status.id)
  111. end
  112. return unless @status.public_visibility?
  113. @status.tags.map(&:name).each do |hashtag|
  114. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", @payload)
  115. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", @payload) if @status.local?
  116. end
  117. end
  118. def remove_from_public
  119. return unless @status.public_visibility?
  120. redis.publish('timeline:public', @payload)
  121. redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', @payload)
  122. end
  123. def remove_from_media
  124. return unless @status.public_visibility?
  125. redis.publish('timeline:public:media', @payload)
  126. redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', @payload)
  127. end
  128. def remove_media
  129. return if @options[:redraft] || (!@options[:immediate] && @status.reported?)
  130. @status.media_attachments.destroy_all
  131. end
  132. def lock_options
  133. { redis: Redis.current, key: "distribute:#{@status.id}" }
  134. end
  135. end