about summary refs log tree commit diff
path: root/app/models/concerns/account_merging.rb
blob: 8d37c6e5679ab18cd0e0cd9374e4c2150adc5c05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

module AccountMerging
  extend ActiveSupport::Concern

  def merge_with!(other_account)
    # Since it's the same remote resource, the remote resource likely
    # already believes we are following/blocking, so it's safe to
    # re-attribute the relationships too. However, during the presence
    # of the index bug users could have *also* followed the reference
    # account already, therefore mass update will not work and we need
    # to check for (and skip past) uniqueness errors

    owned_classes = [
      Status, StatusPin, MediaAttachment, Poll, Report, Tombstone, Favourite,
      Follow, FollowRequest, Block, Mute, AccountIdentityProof,
      AccountModerationNote, AccountPin, AccountStat, ListAccount,
      PollVote, Mention, AccountDeletionRequest, AccountNote, FollowRecommendationSuppression
    ]

    owned_classes.each do |klass|
      klass.where(account_id: other_account.id).find_each do |record|
        begin
          record.update_attribute(:account_id, id)
        rescue ActiveRecord::RecordNotUnique
          next
        end
      end
    end

    target_classes = [
      Follow, FollowRequest, Block, Mute, AccountModerationNote, AccountPin,
      AccountNote
    ]

    target_classes.each do |klass|
      klass.where(target_account_id: other_account.id).find_each do |record|
        begin
          record.update_attribute(:target_account_id, id)
        rescue ActiveRecord::RecordNotUnique
          next
        end
      end
    end

    CanonicalEmailBlock.where(reference_account_id: other_account.id).find_each do |record|
      record.update_attribute(:reference_account_id, id)
    end

    # Some follow relationships have moved, so the cache is stale
    Rails.cache.delete_matched("followers_hash:#{id}:*")
    Rails.cache.delete_matched("relationships:#{id}:*")
    Rails.cache.delete_matched("relationships:*:#{id}")
  end
end