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.

145 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. stream_entries
  26. subscriptions
  27. ).freeze
  28. ASSOCIATIONS_ON_DESTROY = %w(
  29. reports
  30. targeted_moderation_notes
  31. targeted_reports
  32. ).freeze
  33. # Suspend an account and remove as much of its data as possible
  34. # @param [Account]
  35. # @param [Hash] options
  36. # @option [Boolean] :including_user Remove the user record as well
  37. # @option [Boolean] :destroy Remove the account record instead of suspending
  38. def call(account, **options)
  39. @account = account
  40. @options = options
  41. reject_follows!
  42. purge_user!
  43. purge_profile!
  44. purge_content!
  45. end
  46. private
  47. def reject_follows!
  48. return if @account.local? || !@account.activitypub?
  49. ActivityPub::DeliveryWorker.push_bulk(Follow.where(account: @account)) do |follow|
  50. [build_reject_json(follow), follow.target_account_id, follow.account.inbox_url]
  51. end
  52. end
  53. def purge_user!
  54. return if !@account.local? || @account.user.nil?
  55. if @options[:including_user]
  56. @account.user.destroy
  57. else
  58. @account.user.disable!
  59. end
  60. end
  61. def purge_content!
  62. distribute_delete_actor! if @account.local? && !@options[:skip_distribution]
  63. @account.statuses.reorder(nil).find_in_batches do |statuses|
  64. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
  65. end
  66. associations_for_destruction.each do |association_name|
  67. destroy_all(@account.public_send(association_name))
  68. end
  69. @account.destroy if @options[:destroy]
  70. end
  71. def purge_profile!
  72. # If the account is going to be destroyed
  73. # there is no point wasting time updating
  74. # its values first
  75. return if @options[:destroy]
  76. @account.silenced_at = nil
  77. @account.suspended_at = @options[:suspended_at] || Time.now.utc
  78. @account.locked = false
  79. @account.display_name = ''
  80. @account.note = ''
  81. @account.fields = []
  82. @account.statuses_count = 0
  83. @account.followers_count = 0
  84. @account.following_count = 0
  85. @account.moved_to_account = nil
  86. @account.avatar.destroy
  87. @account.header.destroy
  88. @account.save!
  89. end
  90. def destroy_all(association)
  91. association.in_batches.destroy_all
  92. end
  93. def distribute_delete_actor!
  94. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  95. [delete_actor_json, @account.id, inbox_url]
  96. end
  97. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  98. [delete_actor_json, @account.id, inbox_url]
  99. end
  100. end
  101. def delete_actor_json
  102. @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
  103. end
  104. def build_reject_json(follow)
  105. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  106. end
  107. def delivery_inboxes
  108. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  109. end
  110. def low_priority_delivery_inboxes
  111. Account.inboxes - delivery_inboxes
  112. end
  113. def associations_for_destruction
  114. if @options[:destroy]
  115. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  116. else
  117. ASSOCIATIONS_ON_SUSPEND
  118. end
  119. end
  120. end