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.

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