remove_status_service.rb 4.7 KB

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