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.

180 lines
5.2 KiB

  1. # frozen_string_literal: true
  2. class DeleteAccountService < 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. mute_relationships
  17. muted_by_relationships
  18. notifications
  19. owned_lists
  20. passive_relationships
  21. report_notes
  22. scheduled_statuses
  23. status_pins
  24. ).freeze
  25. ASSOCIATIONS_ON_DESTROY = %w(
  26. reports
  27. targeted_moderation_notes
  28. targeted_reports
  29. ).freeze
  30. # Suspend or remove an account and remove as much of its data
  31. # as possible. If it's a local account and it has not been confirmed
  32. # or never been approved, then side effects are skipped and both
  33. # the user and account records are removed fully. Otherwise,
  34. # it is controlled by options.
  35. # @param [Account]
  36. # @param [Hash] options
  37. # @option [Boolean] :reserve_email Keep user record. Only applicable for local accounts
  38. # @option [Boolean] :reserve_username Keep account record
  39. # @option [Boolean] :skip_side_effects Side effects are ActivityPub and streaming API payloads
  40. # @option [Time] :suspended_at Only applicable when :reserve_username is true
  41. def call(account, **options)
  42. @account = account
  43. @options = { reserve_username: true, reserve_email: true }.merge(options)
  44. if @account.local? && @account.user_unconfirmed_or_pending?
  45. @options[:reserve_email] = false
  46. @options[:reserve_username] = false
  47. @options[:skip_side_effects] = true
  48. end
  49. reject_follows!
  50. purge_user!
  51. purge_profile!
  52. purge_content!
  53. fulfill_deletion_request!
  54. end
  55. private
  56. def reject_follows!
  57. return if @account.local? || !@account.activitypub?
  58. ActivityPub::DeliveryWorker.push_bulk(Follow.where(account: @account)) do |follow|
  59. [build_reject_json(follow), follow.target_account_id, follow.account.inbox_url]
  60. end
  61. end
  62. def purge_user!
  63. return if !@account.local? || @account.user.nil?
  64. if @options[:reserve_email]
  65. @account.user.disable!
  66. @account.user.invites.where(uses: 0).destroy_all
  67. else
  68. @account.user.destroy
  69. end
  70. end
  71. def purge_content!
  72. distribute_delete_actor! if @account.local? && !@options[:skip_side_effects]
  73. @account.statuses.reorder(nil).find_in_batches do |statuses|
  74. statuses.reject! { |status| reported_status_ids.include?(status.id) } if @options[:reserve_username]
  75. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:skip_side_effects])
  76. end
  77. @account.media_attachments.reorder(nil).find_each do |media_attachment|
  78. next if @options[:reserve_username] && reported_status_ids.include?(media_attachment.status_id)
  79. media_attachment.destroy
  80. end
  81. @account.polls.reorder(nil).find_each do |poll|
  82. next if @options[:reserve_username] && reported_status_ids.include?(poll.status_id)
  83. poll.destroy
  84. end
  85. associations_for_destruction.each do |association_name|
  86. destroy_all(@account.public_send(association_name))
  87. end
  88. @account.destroy unless @options[:reserve_username]
  89. end
  90. def purge_profile!
  91. # If the account is going to be destroyed
  92. # there is no point wasting time updating
  93. # its values first
  94. return unless @options[:reserve_username]
  95. @account.silenced_at = nil
  96. @account.suspended_at = @options[:suspended_at] || Time.now.utc
  97. @account.locked = false
  98. @account.memorial = false
  99. @account.discoverable = false
  100. @account.display_name = ''
  101. @account.note = ''
  102. @account.fields = []
  103. @account.statuses_count = 0
  104. @account.followers_count = 0
  105. @account.following_count = 0
  106. @account.moved_to_account = nil
  107. @account.trust_level = :untrusted
  108. @account.avatar.destroy
  109. @account.header.destroy
  110. @account.save!
  111. end
  112. def fulfill_deletion_request!
  113. @account.deletion_request&.destroy
  114. end
  115. def destroy_all(association)
  116. association.in_batches.destroy_all
  117. end
  118. def distribute_delete_actor!
  119. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  120. [delete_actor_json, @account.id, inbox_url]
  121. end
  122. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  123. [delete_actor_json, @account.id, inbox_url]
  124. end
  125. end
  126. def delete_actor_json
  127. @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
  128. end
  129. def build_reject_json(follow)
  130. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  131. end
  132. def delivery_inboxes
  133. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  134. end
  135. def low_priority_delivery_inboxes
  136. Account.inboxes - delivery_inboxes
  137. end
  138. def reported_status_ids
  139. @reported_status_ids ||= Report.where(target_account: @account).unresolved.pluck(:status_ids).flatten.uniq
  140. end
  141. def associations_for_destruction
  142. if @options[:reserve_username]
  143. ASSOCIATIONS_ON_SUSPEND
  144. else
  145. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  146. end
  147. end
  148. end