remove_status_service.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_lock("distribute:#{@status.id}") do
  20. @status.discard
  21. remove_from_self if @account.local?
  22. remove_from_followers
  23. remove_from_lists
  24. # There is no reason to send out Undo activities when the
  25. # cause is that the original object has been removed, since
  26. # original object being removed implicitly removes reblogs
  27. # of it. The Delete activity of the original is forwarded
  28. # separately.
  29. remove_from_remote_reach if @account.local? && !@options[:original_removed]
  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.with_media?
  39. remove_media
  40. end
  41. @status.destroy! if permanently?
  42. end
  43. end
  44. private
  45. # The following FeedManager calls all do not result in redis publishes for
  46. # streaming, as the `:update` option is false
  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. return if skip_streaming?
  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. # Followers, relays, people who got mentioned in the status,
  73. # or who reblogged it from someone else might not follow
  74. # the author and wouldn't normally receive the delete
  75. # notification - so here, we explicitly send it to them
  76. status_reach_finder = StatusReachFinder.new(@status, unsafe: true)
  77. ActivityPub::DeliveryWorker.push_bulk(status_reach_finder.inboxes) do |inbox_url|
  78. [signed_activity_json, @account.id, inbox_url]
  79. end
  80. end
  81. def signed_activity_json
  82. @signed_activity_json ||= Oj.dump(serialize_payload(@status, @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer, signer: @account, always_sign: true))
  83. end
  84. def remove_reblogs
  85. # We delete reblogs of the status before the original status,
  86. # because once original status is gone, reblogs will disappear
  87. # without us being able to do all the fancy stuff
  88. @status.reblogs.includes(:account).reorder(nil).find_each do |reblog|
  89. RemoveStatusService.new.call(reblog, original_removed: true, skip_streaming: skip_streaming?)
  90. end
  91. end
  92. def remove_from_hashtags
  93. @account.featured_tags.where(tag_id: @status.tags.map(&:id)).each do |featured_tag|
  94. featured_tag.decrement(@status.id)
  95. end
  96. return unless @status.public_visibility?
  97. return if skip_streaming?
  98. @status.tags.map(&:name).each do |hashtag|
  99. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", @payload)
  100. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", @payload) if @status.local?
  101. end
  102. end
  103. def remove_from_public
  104. return unless @status.public_visibility?
  105. return if skip_streaming?
  106. redis.publish('timeline:public', @payload)
  107. redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', @payload)
  108. end
  109. def remove_from_media
  110. return unless @status.public_visibility?
  111. return if skip_streaming?
  112. redis.publish('timeline:public:media', @payload)
  113. redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', @payload)
  114. end
  115. def remove_media
  116. return if @options[:redraft] || !permanently?
  117. @status.media_attachments.destroy_all
  118. end
  119. def permanently?
  120. @options[:immediate] || !(@options[:preserve] || @status.reported?)
  121. end
  122. def skip_streaming?
  123. !!@options[:skip_streaming]
  124. end
  125. end