suspend_account_service.rb 3.37 KB
Newer Older
Eugen Rochko's avatar
Eugen Rochko committed
1
2
3
# frozen_string_literal: true

class SuspendAccountService < BaseService
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  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
Eugen Rochko's avatar
Eugen Rochko committed
23
    scheduled_statuses
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    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
40
  def call(account, **options)
Eugen Rochko's avatar
Eugen Rochko committed
41
    @account = account
42
    @options = options
Eugen Rochko's avatar
Eugen Rochko committed
43

44
45
46
    purge_user!
    purge_profile!
    purge_content!
Eugen Rochko's avatar
Eugen Rochko committed
47
48
49
50
  end

  private

51
  def purge_user!
52
53
54
55
    return if !@account.local? || @account.user.nil?

    if @options[:including_user]
      @account.user.destroy
56
    else
57
      @account.user.disable!
58
    end
Eugen Rochko's avatar
Eugen Rochko committed
59
60
  end

61
  def purge_content!
62
    distribute_delete_actor! if @account.local?
63

64
    @account.statuses.reorder(nil).find_in_batches do |statuses|
65
      BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
Eugen Rochko's avatar
Eugen Rochko committed
66
67
    end

68
69
    associations_for_destruction.each do |association_name|
      destroy_all(@account.public_send(association_name))
70
    end
71
72

    @account.destroy if @options[:destroy]
Eugen Rochko's avatar
Eugen Rochko committed
73
74
  end

75
  def purge_profile!
76
77
78
79
80
81
82
83
84
85
86
    # 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             = ''
87
    @account.fields           = []
88
89
90
91
    @account.statuses_count   = 0
    @account.followers_count  = 0
    @account.following_count  = 0
    @account.moved_to_account = nil
Eugen Rochko's avatar
Eugen Rochko committed
92
93
94
95
96
    @account.avatar.destroy
    @account.header.destroy
    @account.save!
  end

97
98
  def destroy_all(association)
    association.in_batches.destroy_all
Eugen Rochko's avatar
Eugen Rochko committed
99
  end
100

101
102
103
104
  def distribute_delete_actor!
    ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
      [delete_actor_json, @account.id, inbox_url]
    end
105
106
107
108

    ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
      [delete_actor_json, @account.id, inbox_url]
    end
109
110
  end

111
  def delete_actor_json
112
113
    return @delete_actor_json if defined?(@delete_actor_json)

114
115
116
117
118
119
    payload = ActiveModelSerializers::SerializableResource.new(
      @account,
      serializer: ActivityPub::DeleteActorSerializer,
      adapter: ActivityPub::Adapter
    ).as_json

120
    @delete_actor_json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
121
  end
122
123

  def delivery_inboxes
124
125
126
127
128
    @delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
  end

  def low_priority_delivery_inboxes
    Account.inboxes - delivery_inboxes
129
  end
130
131
132
133
134
135
136
137

  def associations_for_destruction
    if @options[:destroy]
      ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
    else
      ASSOCIATIONS_ON_SUSPEND
    end
  end
Eugen Rochko's avatar
Eugen Rochko committed
138
end