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.

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