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.

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