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.

204 lines
6.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 [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. # We can safely delete the poll rather than destroy it, as any non-reported
  104. # status should have been deleted already
  105. poll.delete
  106. end
  107. associations_for_destruction.each do |association_name|
  108. destroy_all(@account.public_send(association_name))
  109. end
  110. @account.destroy unless @options[:reserve_username]
  111. end
  112. def purge_profile!
  113. # If the account is going to be destroyed
  114. # there is no point wasting time updating
  115. # its values first
  116. return unless @options[:reserve_username]
  117. @account.silenced_at = nil
  118. @account.suspended_at = @options[:suspended_at] || Time.now.utc
  119. @account.suspension_origin = :local
  120. @account.locked = false
  121. @account.memorial = false
  122. @account.discoverable = false
  123. @account.display_name = ''
  124. @account.note = ''
  125. @account.fields = []
  126. @account.statuses_count = 0
  127. @account.followers_count = 0
  128. @account.following_count = 0
  129. @account.moved_to_account = nil
  130. @account.trust_level = :untrusted
  131. @account.avatar.destroy
  132. @account.header.destroy
  133. @account.save!
  134. end
  135. def fulfill_deletion_request!
  136. @account.deletion_request&.destroy
  137. end
  138. def destroy_all(association)
  139. association.in_batches.destroy_all
  140. end
  141. def distribute_delete_actor!
  142. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  143. [delete_actor_json, @account.id, inbox_url]
  144. end
  145. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  146. [delete_actor_json, @account.id, inbox_url]
  147. end
  148. end
  149. def delete_actor_json
  150. @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
  151. end
  152. def delivery_inboxes
  153. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  154. end
  155. def low_priority_delivery_inboxes
  156. Account.inboxes - delivery_inboxes
  157. end
  158. def reported_status_ids
  159. @reported_status_ids ||= Report.where(target_account: @account).unresolved.pluck(:status_ids).flatten.uniq
  160. end
  161. def associations_for_destruction
  162. if @options[:reserve_username]
  163. ASSOCIATIONS_ON_SUSPEND
  164. else
  165. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  166. end
  167. end
  168. end