suspend_account_service.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # frozen_string_literal: true
  2. class SuspendAccountService < 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. end
  54. private
  55. def reject_follows!
  56. return if @account.local? || !@account.activitypub?
  57. ActivityPub::DeliveryWorker.push_bulk(Follow.where(account: @account)) do |follow|
  58. [build_reject_json(follow), follow.target_account_id, follow.account.inbox_url]
  59. end
  60. end
  61. def purge_user!
  62. return if !@account.local? || @account.user.nil?
  63. if @options[:reserve_email]
  64. @account.user.disable!
  65. @account.user.invites.where(uses: 0).destroy_all
  66. else
  67. @account.user.destroy
  68. end
  69. end
  70. def purge_content!
  71. distribute_delete_actor! if @account.local? && !@options[:skip_side_effects]
  72. @account.statuses.reorder(nil).find_in_batches do |statuses|
  73. statuses.reject! { |status| reported_status_ids.include?(status.id) } if @options[:reserve_username]
  74. BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:skip_side_effects])
  75. end
  76. @account.media_attachments.reorder(nil).find_each do |media_attachment|
  77. next if @options[:reserve_username] && reported_status_ids.include?(media_attachment.status_id)
  78. media_attachment.destroy
  79. end
  80. @account.polls.reorder(nil).find_each do |poll|
  81. next if @options[:reserve_username] && reported_status_ids.include?(poll.status_id)
  82. poll.destroy
  83. end
  84. associations_for_destruction.each do |association_name|
  85. destroy_all(@account.public_send(association_name))
  86. end
  87. @account.destroy unless @options[:reserve_username]
  88. end
  89. def purge_profile!
  90. # If the account is going to be destroyed
  91. # there is no point wasting time updating
  92. # its values first
  93. return unless @options[:reserve_username]
  94. @account.silenced_at = nil
  95. @account.suspended_at = @options[:suspended_at] || Time.now.utc
  96. @account.locked = false
  97. @account.memorial = false
  98. @account.discoverable = false
  99. @account.display_name = ''
  100. @account.note = ''
  101. @account.fields = []
  102. @account.statuses_count = 0
  103. @account.followers_count = 0
  104. @account.following_count = 0
  105. @account.moved_to_account = nil
  106. @account.trust_level = :untrusted
  107. @account.avatar.destroy
  108. @account.header.destroy
  109. @account.save!
  110. end
  111. def destroy_all(association)
  112. association.in_batches.destroy_all
  113. end
  114. def distribute_delete_actor!
  115. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  116. [delete_actor_json, @account.id, inbox_url]
  117. end
  118. ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
  119. [delete_actor_json, @account.id, inbox_url]
  120. end
  121. end
  122. def delete_actor_json
  123. @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
  124. end
  125. def build_reject_json(follow)
  126. Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
  127. end
  128. def delivery_inboxes
  129. @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  130. end
  131. def low_priority_delivery_inboxes
  132. Account.inboxes - delivery_inboxes
  133. end
  134. def reported_status_ids
  135. @reported_status_ids ||= Report.where(target_account: @account).unresolved.pluck(:status_ids).flatten.uniq
  136. end
  137. def associations_for_destruction
  138. if @options[:reserve_username]
  139. ASSOCIATIONS_ON_SUSPEND
  140. else
  141. ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
  142. end
  143. end
  144. end