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.

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