about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
Diffstat (limited to 'app/services')
-rw-r--r--app/services/batched_remove_status_service.rb6
-rw-r--r--app/services/suspend_account_service.rb103
2 files changed, 81 insertions, 28 deletions
diff --git a/app/services/batched_remove_status_service.rb b/app/services/batched_remove_status_service.rb
index 75d756495..2e61904fc 100644
--- a/app/services/batched_remove_status_service.rb
+++ b/app/services/batched_remove_status_service.rb
@@ -9,7 +9,9 @@ class BatchedRemoveStatusService < BaseService
   # Remove statuses from home feeds
   # Push delete events to streaming API for home feeds and public feeds
   # @param [Status] statuses A preferably batched array of statuses
-  def call(statuses)
+  # @param [Hash] options
+  # @option [Boolean] :skip_side_effects
+  def call(statuses, **options)
     statuses = Status.where(id: statuses.map(&:id)).includes(:account, :stream_entry).flat_map { |status| [status] + status.reblogs.includes(:account, :stream_entry).to_a }
 
     @mentions = statuses.each_with_object({}) { |s, h| h[s.id] = s.active_mentions.includes(:account).to_a }
@@ -26,6 +28,8 @@ class BatchedRemoveStatusService < BaseService
       status.destroy
     end
 
+    return if options[:skip_side_effects]
+
     # Batch by source account
     statuses.group_by(&:account_id).each_value do |account_statuses|
       account = account_statuses.first.account
diff --git a/app/services/suspend_account_service.rb b/app/services/suspend_account_service.rb
index 8fc79b8ad..6ab6b2901 100644
--- a/app/services/suspend_account_service.rb
+++ b/app/services/suspend_account_service.rb
@@ -1,6 +1,41 @@
 # frozen_string_literal: true
 
 class SuspendAccountService < BaseService
+  ASSOCIATIONS_ON_SUSPEND = %w(
+    account_pins
+    active_relationships
+    block_relationships
+    blocked_by_relationships
+    conversation_mutes
+    conversations
+    custom_filters
+    domain_blocks
+    favourites
+    follow_requests
+    list_accounts
+    media_attachments
+    mute_relationships
+    muted_by_relationships
+    notifications
+    owned_lists
+    passive_relationships
+    report_notes
+    status_pins
+    stream_entries
+    subscriptions
+  ).freeze
+
+  ASSOCIATIONS_ON_DESTROY = %w(
+    reports
+    targeted_moderation_notes
+    targeted_reports
+  ).freeze
+
+  # Suspend an account and remove as much of its data as possible
+  # @param [Account]
+  # @param [Hash] options
+  # @option [Boolean] :including_user Remove the user record as well
+  # @option [Boolean] :destroy Remove the account record instead of suspending
   def call(account, **options)
     @account = account
     @options = options
@@ -8,60 +43,66 @@ class SuspendAccountService < BaseService
     purge_user!
     purge_profile!
     purge_content!
-    unsubscribe_push_subscribers!
   end
 
   private
 
   def purge_user!
-    if @options[:remove_user]
-      @account.user&.destroy
+    return if !@account.local? || @account.user.nil?
+
+    if @options[:including_user]
+      @account.user.destroy
     else
-      @account.user&.disable!
+      @account.user.disable!
     end
   end
 
   def purge_content!
-    if @account.local?
-      ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
-        [delete_actor_json, @account.id, inbox_url]
-      end
-    end
+    distribute_delete_actor! if @account.local?
 
     @account.statuses.reorder(nil).find_in_batches do |statuses|
-      BatchedRemoveStatusService.new.call(statuses)
+      BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
     end
 
-    [
-      @account.media_attachments,
-      @account.stream_entries,
-      @account.notifications,
-      @account.favourites,
-      @account.active_relationships,
-      @account.passive_relationships,
-    ].each do |association|
-      destroy_all(association)
+    associations_for_destruction.each do |association_name|
+      destroy_all(@account.public_send(association_name))
     end
+
+    @account.destroy if @options[:destroy]
   end
 
   def purge_profile!
-    @account.suspended      = true
-    @account.display_name   = ''
-    @account.note           = ''
-    @account.statuses_count = 0
+    # If the account is going to be destroyed
+    # there is no point wasting time updating
+    # its values first
+
+    return if @options[:destroy]
+
+    @account.silenced         = false
+    @account.suspended        = true
+    @account.locked           = false
+    @account.display_name     = ''
+    @account.note             = ''
+    @account.fields           = {}
+    @account.statuses_count   = 0
+    @account.followers_count  = 0
+    @account.following_count  = 0
+    @account.moved_to_account = nil
     @account.avatar.destroy
     @account.header.destroy
     @account.save!
   end
 
-  def unsubscribe_push_subscribers!
-    destroy_all(@account.subscriptions)
-  end
-
   def destroy_all(association)
     association.in_batches.destroy_all
   end
 
+  def distribute_delete_actor!
+    ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
+      [delete_actor_json, @account.id, inbox_url]
+    end
+  end
+
   def delete_actor_json
     return @delete_actor_json if defined?(@delete_actor_json)
 
@@ -77,4 +118,12 @@ class SuspendAccountService < BaseService
   def delivery_inboxes
     Account.inboxes + Relay.enabled.pluck(:inbox_url)
   end
+
+  def associations_for_destruction
+    if @options[:destroy]
+      ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
+    else
+      ASSOCIATIONS_ON_SUSPEND
+    end
+  end
 end