about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2020-11-23 17:50:16 +0100
committerGitHub <noreply@github.com>2020-11-23 17:50:16 +0100
commitf844386809e894591dce3f52ace4beba0c977a8b (patch)
treefcb001ead34e9f3dc36da9072a0c25159bf0fdaf /app
parenta2da02626ef7a026dc7f6ec4219fbb839f4c2721 (diff)
Add `tootctl accounts merge` (#15201)
* Add `tootctl accounts merge`

* Update lib/mastodon/accounts_cli.rb

Co-authored-by: Yamagishi Kazutoshi <ykzts@desire.sh>

Co-authored-by: Yamagishi Kazutoshi <ykzts@desire.sh>
Diffstat (limited to 'app')
-rw-r--r--app/models/account.rb1
-rw-r--r--app/models/concerns/account_merging.rb43
2 files changed, 44 insertions, 0 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index bc9bcc72d..f794d8a29 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -67,6 +67,7 @@ class Account < ApplicationRecord
   include Paginable
   include AccountCounters
   include DomainNormalizable
+  include AccountMerging
 
   TRUST_LEVELS = {
     untrusted: 0,
diff --git a/app/models/concerns/account_merging.rb b/app/models/concerns/account_merging.rb
new file mode 100644
index 000000000..691d02e03
--- /dev/null
+++ b/app/models/concerns/account_merging.rb
@@ -0,0 +1,43 @@
+# 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
+    ]
+
+    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]
+
+    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
+  end
+end