From 5925a31b78f9eadd8daeb8e316bd6728fde547a9 Mon Sep 17 00:00:00 2001
From: Claire <claire.github-309c@sitedethib.com>
Date: Mon, 7 Nov 2022 15:38:55 +0100
Subject: [PATCH] Fix followers count not being updated when migrating follows
 (#19998)

Fixes #19900
---
 app/workers/move_worker.rb       |  4 +++-
 spec/workers/move_worker_spec.rb | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/app/workers/move_worker.rb b/app/workers/move_worker.rb
index c3167f9ca..3b429928e 100644
--- a/app/workers/move_worker.rb
+++ b/app/workers/move_worker.rb
@@ -8,7 +8,9 @@ class MoveWorker
     @target_account = Account.find(target_account_id)
 
     if @target_account.local? && @source_account.local?
-      rewrite_follows!
+      nb_moved = rewrite_follows!
+      @source_account.update_count!(:followers_count, -nb_moved)
+      @target_account.update_count!(:followers_count, nb_moved)
     else
       queue_follow_unfollows!
     end
diff --git a/spec/workers/move_worker_spec.rb b/spec/workers/move_worker_spec.rb
index be02d3192..3ca6aaf4d 100644
--- a/spec/workers/move_worker_spec.rb
+++ b/spec/workers/move_worker_spec.rb
@@ -74,6 +74,18 @@ describe MoveWorker do
     end
   end
 
+  shared_examples 'followers count handling' do
+    it 'updates the source account followers count' do
+      subject.perform(source_account.id, target_account.id)
+      expect(source_account.reload.followers_count).to eq(source_account.passive_relationships.count)
+    end
+
+    it 'updates the target account followers count' do
+      subject.perform(source_account.id, target_account.id)
+      expect(target_account.reload.followers_count).to eq(target_account.passive_relationships.count)
+    end
+  end
+
   context 'both accounts are distant' do
     describe 'perform' do
       it 'calls UnfollowFollowWorker' do
@@ -83,6 +95,7 @@ describe MoveWorker do
 
       include_examples 'user note handling'
       include_examples 'block and mute handling'
+      include_examples 'followers count handling'
     end
   end
 
@@ -97,6 +110,7 @@ describe MoveWorker do
 
       include_examples 'user note handling'
       include_examples 'block and mute handling'
+      include_examples 'followers count handling'
     end
   end
 
@@ -112,6 +126,7 @@ describe MoveWorker do
 
       include_examples 'user note handling'
       include_examples 'block and mute handling'
+      include_examples 'followers count handling'
 
       it 'does not fail when a local user is already following both accounts' do
         double_follower = Fabricate(:account)