2016-12-06 17:41:42 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SuspendAccountService < BaseService
|
2019-06-04 23:11:18 +02:00
|
|
|
include Payloadable
|
|
|
|
|
2018-12-03 01:32:08 +01: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
|
|
|
|
mute_relationships
|
|
|
|
muted_by_relationships
|
|
|
|
notifications
|
|
|
|
owned_lists
|
|
|
|
passive_relationships
|
|
|
|
report_notes
|
2019-01-05 12:43:28 +01:00
|
|
|
scheduled_statuses
|
2018-12-03 01:32:08 +01:00
|
|
|
status_pins
|
|
|
|
).freeze
|
|
|
|
|
|
|
|
ASSOCIATIONS_ON_DESTROY = %w(
|
|
|
|
reports
|
|
|
|
targeted_moderation_notes
|
|
|
|
targeted_reports
|
|
|
|
).freeze
|
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
# Suspend or remove an account and remove as much of its data
|
|
|
|
# as possible. If it's a local account and it has not been confirmed
|
|
|
|
# or never been approved, then side effects are skipped and both
|
|
|
|
# the user and account records are removed fully. Otherwise,
|
|
|
|
# it is controlled by options.
|
2018-12-03 01:32:08 +01:00
|
|
|
# @param [Account]
|
|
|
|
# @param [Hash] options
|
2019-09-11 16:32:44 +02:00
|
|
|
# @option [Boolean] :reserve_email Keep user record. Only applicable for local accounts
|
|
|
|
# @option [Boolean] :reserve_username Keep account record
|
|
|
|
# @option [Boolean] :skip_side_effects Side effects are ActivityPub and streaming API payloads
|
|
|
|
# @option [Time] :suspended_at Only applicable when :reserve_username is true
|
2017-12-06 11:41:57 +01:00
|
|
|
def call(account, **options)
|
2016-12-06 17:41:42 +01:00
|
|
|
@account = account
|
2019-09-11 16:32:44 +02:00
|
|
|
@options = { reserve_username: true, reserve_email: true }.merge(options)
|
|
|
|
|
|
|
|
if @account.local? && @account.user_unconfirmed_or_pending?
|
|
|
|
@options[:reserve_email] = false
|
|
|
|
@options[:reserve_username] = false
|
|
|
|
@options[:skip_side_effects] = true
|
|
|
|
end
|
2016-12-06 17:41:42 +01:00
|
|
|
|
2019-03-10 16:18:58 +01:00
|
|
|
reject_follows!
|
2017-11-07 19:06:44 +01:00
|
|
|
purge_user!
|
|
|
|
purge_profile!
|
|
|
|
purge_content!
|
2016-12-06 17:41:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-03-10 16:18:58 +01: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 19:06:44 +01:00
|
|
|
def purge_user!
|
2018-12-03 01:32:08 +01:00
|
|
|
return if !@account.local? || @account.user.nil?
|
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
if @options[:reserve_email]
|
2018-12-03 01:32:08 +01:00
|
|
|
@account.user.disable!
|
2019-07-26 18:55:33 +02:00
|
|
|
@account.user.invites.where(uses: 0).destroy_all
|
2019-09-11 16:32:44 +02:00
|
|
|
else
|
|
|
|
@account.user.destroy
|
2017-11-07 19:06:44 +01:00
|
|
|
end
|
2017-06-14 18:01:27 +02:00
|
|
|
end
|
|
|
|
|
2017-11-07 19:06:44 +01:00
|
|
|
def purge_content!
|
2019-09-11 16:32:44 +02:00
|
|
|
distribute_delete_actor! if @account.local? && !@options[:skip_side_effects]
|
2018-01-04 14:40:49 +01:00
|
|
|
|
2017-06-14 18:01:35 +02:00
|
|
|
@account.statuses.reorder(nil).find_in_batches do |statuses|
|
2019-09-11 16:32:44 +02:00
|
|
|
statuses.reject! { |status| reported_status_ids.include?(status.id) } if @options[:reserve_username]
|
|
|
|
BatchedRemoveStatusService.new.call(statuses, skip_side_effects: @options[:skip_side_effects])
|
|
|
|
end
|
|
|
|
|
|
|
|
@account.media_attachments.reorder(nil).find_each do |media_attachment|
|
|
|
|
next if @options[:reserve_username] && reported_status_ids.include?(media_attachment.status_id)
|
|
|
|
|
|
|
|
media_attachment.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
@account.polls.reorder(nil).find_each do |poll|
|
|
|
|
next if @options[:reserve_username] && reported_status_ids.include?(poll.status_id)
|
|
|
|
|
|
|
|
poll.destroy
|
2016-12-06 18:32:36 +01:00
|
|
|
end
|
|
|
|
|
2018-12-03 01:32:08 +01:00
|
|
|
associations_for_destruction.each do |association_name|
|
|
|
|
destroy_all(@account.public_send(association_name))
|
2017-05-04 23:44:39 +02:00
|
|
|
end
|
2018-12-03 01:32:08 +01:00
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
@account.destroy unless @options[:reserve_username]
|
2016-12-06 17:41:42 +01:00
|
|
|
end
|
|
|
|
|
2017-11-07 19:06:44 +01:00
|
|
|
def purge_profile!
|
2018-12-03 01:32:08 +01:00
|
|
|
# If the account is going to be destroyed
|
|
|
|
# there is no point wasting time updating
|
|
|
|
# its values first
|
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
return unless @options[:reserve_username]
|
2018-12-03 01:32:08 +01:00
|
|
|
|
2019-05-14 19:05:02 +02:00
|
|
|
@account.silenced_at = nil
|
|
|
|
@account.suspended_at = @options[:suspended_at] || Time.now.utc
|
2018-12-03 01:32:08 +01:00
|
|
|
@account.locked = false
|
2019-09-11 16:32:44 +02:00
|
|
|
@account.memorial = false
|
|
|
|
@account.discoverable = false
|
2018-12-03 01:32:08 +01:00
|
|
|
@account.display_name = ''
|
|
|
|
@account.note = ''
|
2019-03-05 15:19:54 +01:00
|
|
|
@account.fields = []
|
2018-12-03 01:32:08 +01:00
|
|
|
@account.statuses_count = 0
|
|
|
|
@account.followers_count = 0
|
|
|
|
@account.following_count = 0
|
|
|
|
@account.moved_to_account = nil
|
2019-09-11 16:32:44 +02:00
|
|
|
@account.trust_level = :untrusted
|
2016-12-06 17:41:42 +01:00
|
|
|
@account.avatar.destroy
|
|
|
|
@account.header.destroy
|
|
|
|
@account.save!
|
|
|
|
end
|
|
|
|
|
2017-05-04 23:44:39 +02:00
|
|
|
def destroy_all(association)
|
|
|
|
association.in_batches.destroy_all
|
2016-12-06 17:41:42 +01:00
|
|
|
end
|
2018-01-04 14:40:49 +01:00
|
|
|
|
2018-12-03 01:32:08 +01:00
|
|
|
def distribute_delete_actor!
|
|
|
|
ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
|
|
|
|
[delete_actor_json, @account.id, inbox_url]
|
|
|
|
end
|
2019-02-12 05:10:43 +01:00
|
|
|
|
|
|
|
ActivityPub::LowPriorityDeliveryWorker.push_bulk(low_priority_delivery_inboxes) do |inbox_url|
|
|
|
|
[delete_actor_json, @account.id, inbox_url]
|
|
|
|
end
|
2018-12-03 01:32:08 +01:00
|
|
|
end
|
|
|
|
|
2018-01-04 14:40:49 +01:00
|
|
|
def delete_actor_json
|
2019-06-04 23:11:18 +02:00
|
|
|
@delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
|
2018-01-04 14:40:49 +01:00
|
|
|
end
|
2018-08-20 13:28:05 +02:00
|
|
|
|
2019-03-10 16:18:58 +01:00
|
|
|
def build_reject_json(follow)
|
2019-06-04 23:11:18 +02:00
|
|
|
Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
|
2019-03-10 16:18:58 +01:00
|
|
|
end
|
|
|
|
|
2018-08-20 13:28:05 +02:00
|
|
|
def delivery_inboxes
|
2019-02-12 05:10:43 +01:00
|
|
|
@delivery_inboxes ||= @account.followers.inboxes + Relay.enabled.pluck(:inbox_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def low_priority_delivery_inboxes
|
|
|
|
Account.inboxes - delivery_inboxes
|
2018-08-20 13:28:05 +02:00
|
|
|
end
|
2018-12-03 01:32:08 +01:00
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
def reported_status_ids
|
|
|
|
@reported_status_ids ||= Report.where(target_account: @account).unresolved.pluck(:status_ids).flatten.uniq
|
|
|
|
end
|
|
|
|
|
2018-12-03 01:32:08 +01:00
|
|
|
def associations_for_destruction
|
2019-09-11 16:32:44 +02:00
|
|
|
if @options[:reserve_username]
|
2018-12-03 01:32:08 +01:00
|
|
|
ASSOCIATIONS_ON_SUSPEND
|
2019-09-11 16:32:44 +02:00
|
|
|
else
|
|
|
|
ASSOCIATIONS_ON_SUSPEND + ASSOCIATIONS_ON_DESTROY
|
2018-12-03 01:32:08 +01:00
|
|
|
end
|
|
|
|
end
|
2016-12-06 17:41:42 +01:00
|
|
|
end
|