2016-12-06 08:41:42 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SuspendAccountService < BaseService
|
2019-06-04 14:11:18 -07:00
|
|
|
include Payloadable
|
|
|
|
|
2018-12-02 16:32:08 -08:00
|
|
|
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
|
2019-01-05 03:43:28 -08:00
|
|
|
scheduled_statuses
|
2018-12-02 16:32:08 -08:00
|
|
|
status_pins
|
|
|
|
).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
|
2017-12-06 02:41:57 -08:00
|
|
|
def call(account, **options)
|
2016-12-06 08:41:42 -08:00
|
|
|
@account = account
|
2017-11-07 10:06:44 -08:00
|
|
|
@options = options
|
2016-12-06 08:41:42 -08:00
|
|
|
|
2019-03-10 08:18:58 -07:00
|
|
|
reject_follows!
|
2017-11-07 10:06:44 -08:00
|
|
|
purge_user!
|
|
|
|
purge_profile!
|
|
|
|
purge_content!
|
2016-12-06 08:41:42 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-03-10 08:18:58 -07:00
|
|
|
def reject_follows!
|
|
|
|
return if @account.local? || !@account.activitypub?
|
|
|
|
|
|
|
|
ActivityPub::DeliveryWorker.push_bulk(Follow.where(account: @account)) do |follow|
|
|
|
|
[build_reject_json(follow), follow.target_account_id, follow.account.inbox_url]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-07 10:06:44 -08:00
|
|
|
def purge_user!
|
2018-12-02 16:32:08 -08:00
|
|
|
return if !@account.local? || @account.user.nil?
|
|
|
|
|
|
|
|
if @options[:including_user]
|
|
|
|
@account.user.destroy
|
2017-11-07 10:06:44 -08:00
|
|
|
else
|
2018-12-02 16:32:08 -08:00
|
|
|
@account.user.disable!
|
2019-07-26 09:55:33 -07:00
|
|
|
@account.user.invites.where(uses: 0).destroy_all
|
2017-11-07 10:06:44 -08:00
|
|
|
end
|
2017-06-14 09:01:27 -07:00
|
|
|
end
|
|
|
|
|
2017-11-07 10:06:44 -08:00
|
|
|
def purge_content!
|
2019-03-19 08:33:30 -07:00
|
|
|
distribute_delete_actor! if @account.local? && !@options[:skip_distribution]
|
2018-01-04 05:40:49 -08:00
|
|
|
|
2017-06-14 09:01:35 -07:00
|
|
|
@account.statuses.reorder(nil).find_in_batches do |statuses|
|
2018-12-02 16:32:08 -08:00
|
|
|
BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:destroy])
|
2016-12-06 09:32:36 -08:00
|
|
|
end
|
|
|
|
|
2018-12-02 16:32:08 -08:00
|
|
|
associations_for_destruction.each do |association_name|
|
|
|
|
destroy_all(@account.public_send(association_name))
|
2017-05-04 14:44:39 -07:00
|
|
|
end
|
2018-12-02 16:32:08 -08:00
|
|
|
|
|
|
|
@account.destroy if @options[:destroy]
|
2016-12-06 08:41:42 -08:00
|
|
|
end
|
|
|
|
|
2017-11-07 10:06:44 -08:00
|
|
|
def purge_profile!
|
2018-12-02 16:32:08 -08:00
|
|
|
# If the account is going to be destroyed
|
|
|
|
# there is no point wasting time updating
|
|
|
|
# its values first
|
|
|
|
|
|
|
|
return if @options[:destroy]
|
|
|
|
|
2019-05-14 10:05:02 -07:00
|
|
|
@account.silenced_at = nil
|
|
|
|
@account.suspended_at = @options[:suspended_at] || Time.now.utc
|
2018-12-02 16:32:08 -08:00
|
|
|
@account.locked = false
|
|
|
|
@account.display_name = ''
|
|
|
|
@account.note = ''
|
2019-03-05 06:19:54 -08:00
|
|
|
@account.fields = []
|
2018-12-02 16:32:08 -08:00
|
|
|
@account.statuses_count = 0
|
|
|
|
@account.followers_count = 0
|
|
|
|
@account.following_count = 0
|
|
|
|
@account.moved_to_account = nil
|
2016-12-06 08:41:42 -08:00
|
|
|
@account.avatar.destroy
|
|
|
|
@account.header.destroy
|
|
|
|
@account.save!
|
|
|
|
end
|
|
|
|
|
2017-05-04 14:44:39 -07:00
|
|
|
def destroy_all(association)
|
|
|
|
association.in_batches.destroy_all
|
2016-12-06 08:41:42 -08:00
|
|
|
end
|
2018-01-04 05:40:49 -08:00
|
|
|
|
2018-12-02 16:32:08 -08:00
|
|
|
def distribute_delete_actor!
|
|
|
|
ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
|
|
|
|
[delete_actor_json, @account.id, inbox_url]
|
|
|
|
end
|
2019-02-11 20:10:43 -08:00
|
|
|
|
|
|
|
ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
|
|
|
|
[delete_actor_json, @account.id, inbox_url]
|
|
|
|
end
|
2018-12-02 16:32:08 -08:00
|
|
|
end
|
|
|
|
|
2018-01-04 05:40:49 -08:00
|
|
|
def delete_actor_json
|
2019-06-04 14:11:18 -07:00
|
|
|
@delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
|
2018-01-04 05:40:49 -08:00
|
|
|
end
|
2018-08-20 04:28:05 -07:00
|
|
|
|
2019-03-10 08:18:58 -07:00
|
|
|
def build_reject_json(follow)
|
2019-06-04 14:11:18 -07:00
|
|
|
Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
|
2019-03-10 08:18:58 -07:00
|
|
|
end
|
|
|
|
|
2018-08-20 04:28:05 -07:00
|
|
|
def delivery_inboxes
|
2019-02-11 20:10:43 -08:00
|
|
|
@delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def low_priority_delivery_inboxes
|
|
|
|
Account.inboxes - delivery_inboxes
|
2018-08-20 04:28:05 -07:00
|
|
|
end
|
2018-12-02 16:32:08 -08:00
|
|
|
|
|
|
|
def associations_for_destruction
|
|
|
|
if @options[:destroy]
|
|
|
|
ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
|
|
|
|
else
|
|
|
|
ASSOCIATIONS_ON_SUSPEND
|
|
|
|
end
|
|
|
|
end
|
2016-12-06 08:41:42 -08:00
|
|
|
end
|