account_deleter.rb 2.58 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class AccountDeleter
  # Things that are not removed from the database:
  # - Comments
  # - Likes
  # - Messages
  # - NotificationActors
  #
  # Given that the User in question will be tombstoned, all of the
  # above will come from an anonomized account (via the UI).
  # The deleted user will appear as "Deleted Account" in
  # the interface.

  attr_accessor :person, :user

21 22 23
  def initialize(person)
    self.person = person
    self.user = person.owner
24 25 26
  end

  def perform!
27 28 29 30
    # close person
    delete_standard_person_associations
    delete_contacts_of_me
    tombstone_person_and_profile
31

32
    close_user if user
33

34
    mark_account_deletion_complete
35 36
  end

37 38 39 40 41 42
  # user deletion methods
  def close_user
    remove_share_visibilities_on_contacts_posts
    disconnect_contacts
    delete_standard_user_associations
    tombstone_user
43 44
  end

45 46 47 48
  # user deletions
  def normal_ar_user_associates_to_delete
    %i[tag_followings services aspects user_preferences
       notifications blocks authorizations o_auth_applications pairwise_pseudonymous_identifiers]
49 50 51 52
  end

  def delete_standard_user_associations
    normal_ar_user_associates_to_delete.each do |asso|
53 54 55
      user.send(asso).ids.each_slice(20) do |ids|
        User.reflect_on_association(asso).class_name.constantize.where(id: ids).destroy_all
      end
56 57 58
    end
  end

59 60 61 62
  def normal_ar_person_associates_to_delete
    %i[posts photos mentions participations roles blocks conversation_visibilities]
  end

63 64
  def delete_standard_person_associations
    normal_ar_person_associates_to_delete.each do |asso|
65 66 67
      person.send(asso).ids.each_slice(20) do |ids|
        Person.reflect_on_association(asso).class_name.constantize.where(id: ids).destroy_all
      end
68 69 70 71 72 73 74 75 76 77
    end
  end

  def disconnect_contacts
    user.contacts.destroy_all
  end

  # Currently this would get deleted due to the db foreign key constrainsts,
  # but we'll keep this method here for completeness
  def remove_share_visibilities_on_contacts_posts
78
    ShareVisibility.for_a_user(user).find_each(batch_size: 20, &:destroy)
79 80 81
  end

  def tombstone_person_and_profile
82 83
    person.lock_access!
    person.clear_profile!
84 85 86
  end

  def tombstone_user
87
    user.clear_account!
88 89 90
  end

  def delete_contacts_of_me
91
    Contact.all_contacts_of_person(person).find_each(batch_size: 20, &:destroy)
92
  end
93 94

  def mark_account_deletion_complete
95
    AccountDeletion.find_by(person: person)&.update_attributes(completed_at: Time.now.utc)
96
  end
97
end