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.

129 lines
3.1 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. status_pins
  23. stream_entries
  24. subscriptions
  25. ).freeze
  26. ASSOCIATIONS_ON_DESTROY = %w(
  27. reports
  28. targeted_moderation_notes
  29. targeted_reports
  30. ).freeze
  31. # Suspend an account and remove as much of its data as possible
  32. # @param [Account]
  33. # @param [Hash] options
  34. # @option [Boolean] :including_user Remove the user record as well
  35. # @option [Boolean] :destroy Remove the account record instead of suspending
  36. def call(account, **options)
  37. @account = account
  38. @options = options
  39. purge_user!
  40. purge_profile!
  41. purge_content!
  42. end
  43. private
  44. def purge_user!
  45. return if !@account.local? || @account.user.nil?
  46. if @options[:including_user]
  47. @account.user.destroy
  48. else
  49. @account.user.disable!
  50. end
  51. end
  52. def purge_content!
  53. distribute_delete_actor! if @account.local?
  54. @account.statuses.reorder(nil).find_in_batches do |statuses|
  55. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
  56. end
  57. associations_for_destruction.each do |association_name|
  58. destroy_all(@account.public_send(association_name))
  59. end
  60. @account.destroy if @options[:destroy]
  61. end
  62. def purge_profile!
  63. # If the account is going to be destroyed
  64. # there is no point wasting time updating
  65. # its values first
  66. return if @options[:destroy]
  67. @account.silenced = false
  68. @account.suspended = true
  69. @account.locked = false
  70. @account.display_name = ''
  71. @account.note = ''
  72. @account.fields = {}
  73. @account.statuses_count = 0
  74. @account.followers_count = 0
  75. @account.following_count = 0
  76. @account.moved_to_account = nil
  77. @account.avatar.destroy
  78. @account.header.destroy
  79. @account.save!
  80. end
  81. def destroy_all(association)
  82. association.in_batches.destroy_all
  83. end
  84. def distribute_delete_actor!
  85. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  86. [delete_actor_json, @account.id, inbox_url]
  87. end
  88. end
  89. def delete_actor_json
  90. return @delete_actor_json if defined?(@delete_actor_json)
  91. payload = ActiveModelSerializers::SerializableResource.new(
  92. @account,
  93. serializer: ActivityPub::DeleteActorSerializer,
  94. adapter: ActivityPub::Adapter
  95. ).as_json
  96. @delete_actor_json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
  97. end
  98. def delivery_inboxes
  99. Account.inboxes + Relay.enabled.pluck(:inbox_url)
  100. end
  101. def associations_for_destruction
  102. if @options[:destroy]
  103. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  104. else
  105. ASSOCIATIONS_ON_SUSPEND
  106. end
  107. end
  108. end