FEATURE: Account for `ignored_users` when merging two users (#7142)

FEATURE: Account for ignored_users when merging two users (#7142)

  • FEATURE: Account for ignored_users when merging two users

Why?

This is part of the Ability to ignore a user feature. When we merge two users, we need to account for merging their list of ignored_users too.

diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb
index 71925af..475a57a 100644
--- a/app/services/user_merger.rb
+++ b/app/services/user_merger.rb
@@ -260,6 +260,9 @@ class UserMerger
     update_user_id(:muted_users, conditions: "x.muted_user_id = y.muted_user_id")
     update_user_id(:muted_users, user_id_column_name: "muted_user_id", conditions: "x.user_id = y.user_id")
 
+    update_user_id(:ignored_users, conditions: "x.ignored_user_id = y.ignored_user_id")
+    update_user_id(:ignored_users, user_id_column_name: "ignored_user_id", conditions: "x.user_id = y.user_id")
+
     Notification.where(user_id: @source_user.id).update_all(user_id: @target_user.id)
 
     update_user_id(:post_actions, conditions: ["x.post_id = y.post_id",
@@ -365,6 +368,7 @@ class UserMerger
     DraftSequence.where(user_id: @source_user.id).delete_all
     GivenDailyLike.where(user_id: @source_user.id).delete_all
     MutedUser.where(user_id: @source_user.id).or(MutedUser.where(muted_user_id: @source_user.id)).delete_all
+    IgnoredUser.where(user_id: @source_user.id).or(IgnoredUser.where(ignored_user_id: @source_user.id)).delete_all
     UserAuthTokenLog.where(user_id: @source_user.id).delete_all
     UserAvatar.where(user_id: @source_user.id).delete_all
     UserAction.where(acting_user_id: @source_user.id).delete_all
diff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rb
index 86025a5..dd9490d 100644
--- a/spec/services/user_merger_spec.rb
+++ b/spec/services/user_merger_spec.rb
@@ -290,6 +290,32 @@ describe UserMerger do
     expect(MutedUser.where(muted_user_id: source_user.id).count).to eq(0)
   end
 
+  it "merges ignored users" do
+    ignored1 = Fabricate(:user)
+    ignored2 = Fabricate(:user)
+    ignored3 = Fabricate(:user)
+    coding_horror = Fabricate(:coding_horror)
+
+    IgnoredUser.create!(user_id: source_user.id, ignored_user_id: ignored1.id)
+    IgnoredUser.create!(user_id: source_user.id, ignored_user_id: ignored2.id)
+    IgnoredUser.create!(user_id: target_user.id, ignored_user_id: ignored2.id)
+    IgnoredUser.create!(user_id: target_user.id, ignored_user_id: ignored3.id)
+    IgnoredUser.create!(user_id: walter.id, ignored_user_id: source_user.id)
+    IgnoredUser.create!(user_id: coding_horror.id, ignored_user_id: source_user.id)
+    IgnoredUser.create!(user_id: coding_horror.id, ignored_user_id: target_user.id)
+
+    merge_users!
+
+    [ignored1, ignored2, ignored3].each do |m|
+      expect(IgnoredUser.where(user_id: target_user.id, ignored_user_id: m.id).count).to eq(1)
+    end
+    expect(IgnoredUser.where(user_id: source_user.id).count).to eq(0)
+
+    expect(IgnoredUser.where(user_id: walter.id, ignored_user_id: target_user.id).count).to eq(1)
+    expect(IgnoredUser.where(user_id: coding_horror.id, ignored_user_id: target_user.id).count).to eq(1)
+    expect(IgnoredUser.where(ignored_user_id: source_user.id).count).to eq(0)
+  end
+
   context "notifications" do
     it "updates notifications" do
       Fabricate(:notification, user: source_user)

GitHub sha: d6eb892b

1 Like