aboutsummaryrefslogtreecommitdiff
path: root/app/models/concerns/account_merging.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/concerns/account_merging.rb')
-rw-r--r--app/models/concerns/account_merging.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/app/models/concerns/account_merging.rb b/app/models/concerns/account_merging.rb
new file mode 100644
index 000000000..c3b7018f2
--- /dev/null
+++ b/app/models/concerns/account_merging.rb
@@ -0,0 +1,51 @@
+# 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
+ ]
+
+ 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
+
+ # 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