Skip to content

Commit 5ecb3ef

Browse files
Gargronhiyuki2578
authored andcommitted
Fix account migration not affecting followers on origin server (mastodon#11980)
1 parent a5cd46e commit 5ecb3ef

4 files changed

Lines changed: 67 additions & 8 deletions

File tree

app/controllers/settings/migrations_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def create
1818
@migration = current_account.migrations.build(resource_params)
1919

2020
if @migration.save_with_challenge(current_user)
21-
current_account.update!(moved_to_account: @migration.target_account)
22-
ActivityPub::UpdateDistributionWorker.perform_async(current_account.id)
23-
ActivityPub::MoveDistributionWorker.perform_async(@migration.id)
21+
MoveService.new.call(@migration)
2422
redirect_to settings_migration_path, notice: I18n.t('migrations.moved_msg', acct: current_account.moved_to_account.acct)
2523
else
2624
render :show

app/lib/activitypub/activity/move.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ def perform
1919
origin_account.update(moved_to_account: target_account)
2020

2121
# Initiate a re-follow for each follower
22-
origin_account.followers.local.select(:id).find_in_batches do |follower_accounts|
23-
UnfollowFollowWorker.push_bulk(follower_accounts.map(&:id)) do |follower_account_id|
24-
[follower_account_id, origin_account.id, target_account.id]
25-
end
26-
end
22+
MoveWorker.perform_async(origin_account.id, target_account.id)
2723
end
2824

2925
private

app/services/move_service.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
class MoveService < BaseService
4+
def call(migration)
5+
@migration = migration
6+
@source_account = migration.account
7+
@target_account = migration.target_account
8+
9+
update_redirect!
10+
process_local_relationships!
11+
distribute_update!
12+
distribute_move!
13+
end
14+
15+
private
16+
17+
def update_redirect!
18+
@source_account.update!(moved_to_account: @target_account)
19+
end
20+
21+
def process_local_relationships!
22+
MoveWorker.perform_async(@source_account.id, @target_account.id)
23+
end
24+
25+
def distribute_update!
26+
ActivityPub::UpdateDistributionWorker.perform_async(@source_account.id)
27+
end
28+
29+
def distribute_move!
30+
ActivityPub::MoveDistributionWorker.perform_async(@migration.id)
31+
end
32+
end

app/workers/move_worker.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
class MoveWorker
4+
include Sidekiq::Worker
5+
6+
def perform(source_account_id, target_account_id)
7+
@source_account = Account.find(source_account_id)
8+
@target_account = Account.find(target_account_id)
9+
10+
if @target_account.local?
11+
rewrite_follows!
12+
else
13+
queue_follow_unfollows!
14+
end
15+
rescue ActiveRecord::RecordNotFound
16+
true
17+
end
18+
19+
private
20+
21+
def rewrite_follows!
22+
@source_account.passive_relationships
23+
.where(account: Account.local)
24+
.in_batches
25+
.update_all(target_account: @target_account)
26+
end
27+
28+
def queue_follow_unfollows!
29+
@source_account.followers.local.select(:id).find_in_batches do |accounts|
30+
UnfollowFollowWorker.push_bulk(accounts.map(&:id)) { |follower_id| [follower_id, @source_account.id, @target_account.id] }
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)