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.

143 lines
3.6 KiB

  1. # frozen_string_literal: true
  2. class SuspendAccountService < BaseService
  3. include Payloadable
  4. ASSOCIATIONS_ON_SUSPEND = %w(
  5. account_pins
  6. active_relationships
  7. block_relationships
  8. blocked_by_relationships
  9. conversation_mutes
  10. conversations
  11. custom_filters
  12. domain_blocks
  13. favourites
  14. follow_requests
  15. list_accounts
  16. media_attachments
  17. mute_relationships
  18. muted_by_relationships
  19. notifications
  20. owned_lists
  21. passive_relationships
  22. report_notes
  23. scheduled_statuses
  24. status_pins
  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. reject_follows!
  40. purge_user!
  41. purge_profile!
  42. purge_content!
  43. end
  44. private
  45. def reject_follows!
  46. return if @account.local? || !@account.activitypub?
  47. ActivityPub::DeliveryWorker.push_bulk(Follow.where(account: @account)) do |follow|
  48. [build_reject_json(follow), follow.target_account_id, follow.account.inbox_url]
  49. end
  50. end
  51. def purge_user!
  52. return if !@account.local? || @account.user.nil?
  53. if @options[:including_user]
  54. @account.user.destroy
  55. else
  56. @account.user.disable!
  57. end
  58. end
  59. def purge_content!
  60. distribute_delete_actor! if @account.local? && !@options[:skip_distribution]
  61. @account.statuses.reorder(nil).find_in_batches do |statuses|
  62. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
  63. end
  64. associations_for_destruction.each do |association_name|
  65. destroy_all(@account.public_send(association_name))
  66. end
  67. @account.destroy if @options[:destroy]
  68. end
  69. def purge_profile!
  70. # If the account is going to be destroyed
  71. # there is no point wasting time updating
  72. # its values first
  73. return if @options[:destroy]
  74. @account.silenced_at = nil
  75. @account.suspended_at = @options[:suspended_at] || Time.now.utc
  76. @account.locked = false
  77. @account.display_name = ''
  78. @account.note = ''
  79. @account.fields = []
  80. @account.statuses_count = 0
  81. @account.followers_count = 0
  82. @account.following_count = 0
  83. @account.moved_to_account = nil
  84. @account.avatar.destroy
  85. @account.header.destroy
  86. @account.save!
  87. end
  88. def destroy_all(association)
  89. association.in_batches.destroy_all
  90. end
  91. def distribute_delete_actor!
  92. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  93. [delete_actor_json, @account.id, inbox_url]
  94. end
  95. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  96. [delete_actor_json, @account.id, inbox_url]
  97. end
  98. end
  99. def delete_actor_json
  100. @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
  101. end
  102. def build_reject_json(follow)
  103. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  104. end
  105. def delivery_inboxes
  106. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  107. end
  108. def low_priority_delivery_inboxes
  109. Account.inboxes - delivery_inboxes
  110. end
  111. def associations_for_destruction
  112. if @options[:destroy]
  113. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  114. else
  115. ASSOCIATIONS_ON_SUSPEND
  116. end
  117. end
  118. end