remove_status_service.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. # @options [Boolean] :original_removed
  10. def call(status, **options)
  11. @payload = Oj.dump(event: :delete, payload: status.id.to_s)
  12. @status = status
  13. @account = status.account
  14. @tags = status.tags.pluck(:name).to_a
  15. @mentions = status.active_mentions.includes(:account).to_a
  16. @reblogs = status.reblogs.includes(:account).to_a
  17. @options = options
  18. RedisLock.acquire(lock_options) do |lock|
  19. if lock.acquired?
  20. remove_from_self if status.account.local?
  21. remove_from_followers
  22. remove_from_lists
  23. remove_from_affected
  24. remove_reblogs
  25. remove_from_hashtags
  26. remove_from_public
  27. remove_from_media if status.media_attachments.any?
  28. remove_from_spam_check
  29. remove_media
  30. @status.destroy!
  31. else
  32. raise Mastodon::RaceConditionError
  33. end
  34. end
  35. # There is no reason to send out Undo activities when the
  36. # cause is that the original object has been removed, since
  37. # original object being removed implicitly removes reblogs
  38. # of it. The Delete activity of the original is forwarded
  39. # separately.
  40. return if !@account.local? || @options[:original_removed]
  41. remove_from_remote_followers
  42. remove_from_remote_affected
  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.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).reorder(nil).find_each do |list|
  55. FeedManager.instance.unpush_from_list(list, @status)
  56. end
  57. end
  58. def remove_from_affected
  59. @mentions.map(&:account).select(&:local?).each do |account|
  60. redis.publish("timeline:#{account.id}", @payload)
  61. end
  62. end
  63. def remove_from_remote_affected
  64. # People who got mentioned in the status, or who
  65. # reblogged it from someone else might not follow
  66. # the author and wouldn't normally receive the
  67. # delete notification - so here, we explicitly
  68. # send it to them
  69. target_accounts = (@mentions.map(&:account).reject(&:local?) + @reblogs.map(&:account).reject(&:local?))
  70. target_accounts << @status.reblog.account if @status.reblog? && !@status.reblog.account.local?
  71. target_accounts.uniq!(&:id)
  72. # ActivityPub
  73. ActivityPub::DeliveryWorker.push_bulk(target_accounts.select(&:activitypub?).uniq(&:preferred_inbox_url)) do |target_account|
  74. [signed_activity_json, @account.id, target_account.preferred_inbox_url]
  75. end
  76. end
  77. def remove_from_remote_followers
  78. # ActivityPub
  79. ActivityPub::DeliveryWorker.push_bulk(@account.followers.inboxes) do |inbox_url|
  80. [signed_activity_json, @account.id, inbox_url]
  81. end
  82. relay! if relayable?
  83. end
  84. def relayable?
  85. @status.public_visibility?
  86. end
  87. def relay!
  88. ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
  89. [signed_activity_json, @account.id, inbox_url]
  90. end
  91. end
  92. def signed_activity_json
  93. @signed_activity_json ||= Oj.dump(serialize_payload(@status, @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer, signer: @account))
  94. end
  95. def remove_reblogs
  96. # We delete reblogs of the status before the original status,
  97. # because once original status is gone, reblogs will disappear
  98. # without us being able to do all the fancy stuff
  99. @reblogs.each do |reblog|
  100. RemoveStatusService.new.call(reblog, original_removed: true)
  101. end
  102. end
  103. def remove_from_hashtags
  104. @account.featured_tags.where(tag_id: @status.tags.pluck(:id)).each do |featured_tag|
  105. featured_tag.decrement(@status.id)
  106. end
  107. return unless @status.public_visibility?
  108. @tags.each do |hashtag|
  109. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", @payload)
  110. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", @payload) if @status.local?
  111. end
  112. end
  113. def remove_from_public
  114. return unless @status.public_visibility?
  115. redis.publish('timeline:public', @payload)
  116. redis.publish('timeline:public:local', @payload) if @status.local?
  117. end
  118. def remove_from_media
  119. return unless @status.public_visibility?
  120. redis.publish('timeline:public:media', @payload)
  121. redis.publish('timeline:public:local:media', @payload) if @status.local?
  122. end
  123. def remove_media
  124. return if @options[:redraft]
  125. @status.media_attachments.destroy_all
  126. end
  127. def remove_from_spam_check
  128. redis.zremrangebyscore("spam_check:#{@status.account_id}", @status.id, @status.id)
  129. end
  130. def lock_options
  131. { redis: Redis.current, key: "distribute:#{@status.id}" }
  132. end
  133. end