suspend_account_service.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 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_at = nil
  76. @account.suspended_at = @options[:suspended_at] || Time.now.utc
  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. @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
  102. end
  103. def build_reject_json(follow)
  104. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  105. end
  106. def delivery_inboxes
  107. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  108. end
  109. def low_priority_delivery_inboxes
  110. Account.inboxes - delivery_inboxes
  111. end
  112. def associations_for_destruction
  113. if @options[:destroy]
  114. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  115. else
  116. ASSOCIATIONS_ON_SUSPEND
  117. end
  118. end
  119. end