suspend_account_service.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # frozen_string_literal: true
  2. class SuspendAccountService < BaseService
  3. ASSOCIATIONS_ON_SUSPEND = %w(
  4. account_pins
  5. active_relationships
  6. block_relationships
  7. blocked_by_relationships
  8. conversation_mutes
  9. conversations
  10. custom_filters
  11. domain_blocks
  12. favourites
  13. follow_requests
  14. list_accounts
  15. media_attachments
  16. mute_relationships
  17. muted_by_relationships
  18. notifications
  19. owned_lists
  20. passive_relationships
  21. report_notes
  22. scheduled_statuses
  23. status_pins
  24. stream_entries
  25. subscriptions
  26. ).freeze
  27. ASSOCIATIONS_ON_DESTROY = %w(
  28. reports
  29. targeted_moderation_notes
  30. targeted_reports
  31. ).freeze
  32. # Suspend an account and remove as much of its data as possible
  33. # @param [Account]
  34. # @param [Hash] options
  35. # @option [Boolean] :including_user Remove the user record as well
  36. # @option [Boolean] :destroy Remove the account record instead of suspending
  37. def call(account, **options)
  38. @account = account
  39. @options = options
  40. reject_follows!
  41. purge_user!
  42. purge_profile!
  43. purge_content!
  44. end
  45. private
  46. def reject_follows!
  47. return if @account.local? || !@account.activitypub?
  48. ActivityPub::DeliveryWorker.push_bulk(Follow.where(account: @account)) do |follow|
  49. [build_reject_json(follow), follow.target_account_id, follow.account.inbox_url]
  50. end
  51. end
  52. def purge_user!
  53. return if !@account.local? || @account.user.nil?
  54. if @options[:including_user]
  55. @account.user.destroy
  56. else
  57. @account.user.disable!
  58. end
  59. end
  60. def purge_content!
  61. distribute_delete_actor! if @account.local? && !@options[:skip_distribution]
  62. @account.statuses.reorder(nil).find_in_batches do |statuses|
  63. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
  64. end
  65. associations_for_destruction.each do |association_name|
  66. destroy_all(@account.public_send(association_name))
  67. end
  68. @account.destroy if @options[:destroy]
  69. end
  70. def purge_profile!
  71. # If the account is going to be destroyed
  72. # there is no point wasting time updating
  73. # its values first
  74. return if @options[:destroy]
  75. @account.silenced = false
  76. @account.suspended = true
  77. @account.locked = false
  78. @account.display_name = ''
  79. @account.note = ''
  80. @account.fields = []
  81. @account.statuses_count = 0
  82. @account.followers_count = 0
  83. @account.following_count = 0
  84. @account.moved_to_account = nil
  85. @account.avatar.destroy
  86. @account.header.destroy
  87. @account.save!
  88. end
  89. def destroy_all(association)
  90. association.in_batches.destroy_all
  91. end
  92. def distribute_delete_actor!
  93. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  94. [delete_actor_json, @account.id, inbox_url]
  95. end
  96. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  97. [delete_actor_json, @account.id, inbox_url]
  98. end
  99. end
  100. def delete_actor_json
  101. return @delete_actor_json if defined?(@delete_actor_json)
  102. payload = ActiveModelSerializers::SerializableResource.new(
  103. @account,
  104. serializer: ActivityPub::DeleteActorSerializer,
  105. adapter: ActivityPub::Adapter
  106. ).as_json
  107. @delete_actor_json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
  108. end
  109. def build_reject_json(follow)
  110. ActiveModelSerializers::SerializableResource.new(
  111. follow,
  112. serializer: ActivityPub::RejectFollowSerializer,
  113. adapter: ActivityPub::Adapter
  114. ).to_json
  115. end
  116. def delivery_inboxes
  117. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  118. end
  119. def low_priority_delivery_inboxes
  120. Account.inboxes - delivery_inboxes
  121. end
  122. def associations_for_destruction
  123. if @options[:destroy]
  124. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  125. else
  126. ASSOCIATIONS_ON_SUSPEND
  127. end
  128. end
  129. end