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.

184 lines
5.5 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. # When deleting a remote account, the account obviously doesn't
  59. # actually become deleted on its origin server, i.e. unlike a
  60. # locally deleted account it continues to have access to its home
  61. # feed and other content. To prevent it from being able to continue
  62. # to access toots it would receive because it follows local accounts,
  63. # we have to force it to unfollow them.
  64. ActivityPub::DeliveryWorker.push_bulk(Follow.where(account: @account)) do |follow|
  65. [Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), follow.target_account_id, @account.inbox_url]
  66. end
  67. end
  68. def purge_user!
  69. return if !@account.local? || @account.user.nil?
  70. if @options[:reserve_email]
  71. @account.user.disable!
  72. @account.user.invites.where(uses: 0).destroy_all
  73. else
  74. @account.user.destroy
  75. end
  76. end
  77. def purge_content!
  78. distribute_delete_actor! if @account.local? && !@options[:skip_side_effects]
  79. @account.statuses.reorder(nil).find_in_batches do |statuses|
  80. statuses.reject! { |status| reported_status_ids.include?(status.id) } if @options[:reserve_username]
  81. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:skip_side_effects])
  82. end
  83. @account.media_attachments.reorder(nil).find_each do |media_attachment|
  84. next if @options[:reserve_username] && reported_status_ids.include?(media_attachment.status_id)
  85. media_attachment.destroy
  86. end
  87. @account.polls.reorder(nil).find_each do |poll|
  88. next if @options[:reserve_username] && reported_status_ids.include?(poll.status_id)
  89. poll.destroy
  90. end
  91. associations_for_destruction.each do |association_name|
  92. destroy_all(@account.public_send(association_name))
  93. end
  94. @account.destroy unless @options[:reserve_username]
  95. end
  96. def purge_profile!
  97. # If the account is going to be destroyed
  98. # there is no point wasting time updating
  99. # its values first
  100. return unless @options[:reserve_username]
  101. @account.silenced_at = nil
  102. @account.suspended_at = @options[:suspended_at] || Time.now.utc
  103. @account.suspension_origin = :local
  104. @account.locked = false
  105. @account.memorial = false
  106. @account.discoverable = false
  107. @account.display_name = ''
  108. @account.note = ''
  109. @account.fields = []
  110. @account.statuses_count = 0
  111. @account.followers_count = 0
  112. @account.following_count = 0
  113. @account.moved_to_account = nil
  114. @account.trust_level = :untrusted
  115. @account.avatar.destroy
  116. @account.header.destroy
  117. @account.save!
  118. end
  119. def fulfill_deletion_request!
  120. @account.deletion_request&.destroy
  121. end
  122. def destroy_all(association)
  123. association.in_batches.destroy_all
  124. end
  125. def distribute_delete_actor!
  126. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  127. [delete_actor_json, @account.id, inbox_url]
  128. end
  129. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  130. [delete_actor_json, @account.id, inbox_url]
  131. end
  132. end
  133. def delete_actor_json
  134. @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
  135. end
  136. def delivery_inboxes
  137. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  138. end
  139. def low_priority_delivery_inboxes
  140. Account.inboxes - delivery_inboxes
  141. end
  142. def reported_status_ids
  143. @reported_status_ids ||= Report.where(target_account: @account).unresolved.pluck(:status_ids).flatten.uniq
  144. end
  145. def associations_for_destruction
  146. if @options[:reserve_username]
  147. ASSOCIATIONS_ON_SUSPEND
  148. else
  149. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  150. end
  151. end
  152. end