You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.4 KiB

  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. purge_user!
  41. purge_profile!
  42. purge_content!
  43. end
  44. private
  45. def purge_user!
  46. return if !@account.local? || @account.user.nil?
  47. if @options[:including_user]
  48. @account.user.destroy
  49. else
  50. @account.user.disable!
  51. end
  52. end
  53. def purge_content!
  54. distribute_delete_actor! if @account.local?
  55. @account.statuses.reorder(nil).find_in_batches do |statuses|
  56. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
  57. end
  58. associations_for_destruction.each do |association_name|
  59. destroy_all(@account.public_send(association_name))
  60. end
  61. @account.destroy if @options[:destroy]
  62. end
  63. def purge_profile!
  64. # If the account is going to be destroyed
  65. # there is no point wasting time updating
  66. # its values first
  67. return if @options[:destroy]
  68. @account.silenced = false
  69. @account.suspended = true
  70. @account.locked = false
  71. @account.display_name = ''
  72. @account.note = ''
  73. @account.fields = {}
  74. @account.statuses_count = 0
  75. @account.followers_count = 0
  76. @account.following_count = 0
  77. @account.moved_to_account = nil
  78. @account.avatar.destroy
  79. @account.header.destroy
  80. @account.save!
  81. end
  82. def destroy_all(association)
  83. association.in_batches.destroy_all
  84. end
  85. def distribute_delete_actor!
  86. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  87. [delete_actor_json, @account.id, inbox_url]
  88. end
  89. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  90. [delete_actor_json, @account.id, inbox_url]
  91. end
  92. end
  93. def delete_actor_json
  94. return @delete_actor_json if defined?(@delete_actor_json)
  95. payload = ActiveModelSerializers::SerializableResource.new(
  96. @account,
  97. serializer: ActivityPub::DeleteActorSerializer,
  98. adapter: ActivityPub::Adapter
  99. ).as_json
  100. @delete_actor_json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
  101. end
  102. def delivery_inboxes
  103. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  104. end
  105. def low_priority_delivery_inboxes
  106. Account.inboxes - delivery_inboxes
  107. end
  108. def associations_for_destruction
  109. if @options[:destroy]
  110. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  111. else
  112. ASSOCIATIONS_ON_SUSPEND
  113. end
  114. end
  115. end