remove_status_service.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # frozen_string_literal: true
  2. class RemoveStatusService < BaseService
  3. include Redisable
  4. include Payloadable
  5. include Lockable
  6. # Delete a status
  7. # @param [Status] status
  8. # @param [Hash] options
  9. # @option [Boolean] :redraft
  10. # @option [Boolean] :immediate
  11. # @option [Boolean] :preserve
  12. # @option [Boolean] :original_removed
  13. # @option [Boolean] :skip_streaming
  14. def call(status, **options)
  15. @payload = Oj.dump(event: :delete, payload: status.id.to_s)
  16. @status = status
  17. @account = status.account
  18. @options = options
  19. with_redis_lock("distribute:#{@status.id}") do
  20. @status.discard_with_reblogs
  21. StatusPin.find_by(status: @status)&.destroy
  22. remove_from_self if @account.local?
  23. remove_from_followers
  24. remove_from_lists
  25. # There is no reason to send out Undo activities when the
  26. # cause is that the original object has been removed, since
  27. # original object being removed implicitly removes reblogs
  28. # of it. The Delete activity of the original is forwarded
  29. # separately.
  30. remove_from_remote_reach if @account.local? && !@options[:original_removed]
  31. # Since reblogs don't mention anyone, don't get reblogged,
  32. # favourited and don't contain their own media attachments
  33. # or hashtags, this can be skipped
  34. unless @status.reblog?
  35. remove_from_mentions
  36. remove_reblogs
  37. remove_from_hashtags
  38. remove_from_public
  39. remove_from_media if @status.with_media?
  40. remove_media
  41. end
  42. @status.destroy! if permanently?
  43. end
  44. end
  45. private
  46. # The following FeedManager calls all do not result in redis publishes for
  47. # streaming, as the `:update` option is false
  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.includes(:user).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).includes(account: :user).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. return if skip_streaming?
  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. # Followers, relays, people who got mentioned in the status,
  74. # or who reblogged it from someone else might not follow
  75. # the author and wouldn't normally receive the delete
  76. # notification - so here, we explicitly send it to them
  77. status_reach_finder = StatusReachFinder.new(@status, unsafe: true)
  78. ActivityPub::DeliveryWorker.push_bulk(status_reach_finder.inboxes, limit: 1_000) do |inbox_url|
  79. [signed_activity_json, @account.id, inbox_url]
  80. end
  81. end
  82. def signed_activity_json
  83. @signed_activity_json ||= Oj.dump(serialize_payload(@status, @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer, signer: @account, always_sign: true))
  84. end
  85. def remove_reblogs
  86. # We delete reblogs of the status before the original status,
  87. # because once original status is gone, reblogs will disappear
  88. # without us being able to do all the fancy stuff
  89. @status.reblogs.rewhere(deleted_at: [nil, @status.deleted_at]).includes(:account).reorder(nil).find_each do |reblog|
  90. RemoveStatusService.new.call(reblog, original_removed: true, skip_streaming: skip_streaming?)
  91. end
  92. end
  93. def remove_from_hashtags
  94. @account.featured_tags.where(tag_id: @status.tags.map(&:id)).each do |featured_tag|
  95. featured_tag.decrement(@status.id)
  96. end
  97. return unless @status.public_visibility?
  98. return if skip_streaming?
  99. @status.tags.map(&:name).each do |hashtag|
  100. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", @payload)
  101. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", @payload) if @status.local?
  102. end
  103. end
  104. def remove_from_public
  105. return unless @status.public_visibility?
  106. return if skip_streaming?
  107. redis.publish('timeline:public', @payload)
  108. redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', @payload)
  109. end
  110. def remove_from_media
  111. return unless @status.public_visibility?
  112. return if skip_streaming?
  113. redis.publish('timeline:public:media', @payload)
  114. redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', @payload)
  115. end
  116. def remove_media
  117. return if @options[:redraft] || !permanently?
  118. @status.media_attachments.destroy_all
  119. end
  120. def permanently?
  121. @options[:immediate] || !(@options[:preserve] || @status.reported?)
  122. end
  123. def skip_streaming?
  124. !!@options[:skip_streaming]
  125. end
  126. end