remove_status_service.rb 4.6 KB

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