mirror of
https://github.com/kikobar/mastodon.git
synced 2024-11-17 04:32:22 +00:00
Change Move handler to also move list memberships (#24808)
This commit is contained in:
parent
9a52a7f7a0
commit
c98b012583
|
@ -9,10 +9,10 @@ class FollowMigrationService < FollowService
|
||||||
def call(source_account, target_account, old_target_account, bypass_locked: false)
|
def call(source_account, target_account, old_target_account, bypass_locked: false)
|
||||||
@old_target_account = old_target_account
|
@old_target_account = old_target_account
|
||||||
|
|
||||||
follow = source_account.active_relationships.find_by(target_account: old_target_account)
|
@original_follow = source_account.active_relationships.find_by(target_account: old_target_account)
|
||||||
reblogs = follow&.show_reblogs?
|
reblogs = @original_follow&.show_reblogs?
|
||||||
notify = follow&.notify?
|
notify = @original_follow&.notify?
|
||||||
languages = follow&.languages
|
languages = @original_follow&.languages
|
||||||
|
|
||||||
super(source_account, target_account, reblogs: reblogs, notify: notify, languages: languages, bypass_locked: bypass_locked, bypass_limit: true)
|
super(source_account, target_account, reblogs: reblogs, notify: notify, languages: languages, bypass_locked: bypass_locked, bypass_limit: true)
|
||||||
end
|
end
|
||||||
|
@ -21,6 +21,7 @@ class FollowMigrationService < FollowService
|
||||||
|
|
||||||
def request_follow!
|
def request_follow!
|
||||||
follow_request = @source_account.request_follow!(@target_account, **follow_options.merge(rate_limit: @options[:with_rate_limit], bypass_limit: @options[:bypass_limit]))
|
follow_request = @source_account.request_follow!(@target_account, **follow_options.merge(rate_limit: @options[:with_rate_limit], bypass_limit: @options[:bypass_limit]))
|
||||||
|
migrate_list_accounts!
|
||||||
|
|
||||||
if @target_account.local?
|
if @target_account.local?
|
||||||
LocalNotificationWorker.perform_async(@target_account.id, follow_request.id, follow_request.class.name, 'follow_request')
|
LocalNotificationWorker.perform_async(@target_account.id, follow_request.id, follow_request.class.name, 'follow_request')
|
||||||
|
@ -34,7 +35,16 @@ class FollowMigrationService < FollowService
|
||||||
|
|
||||||
def direct_follow!
|
def direct_follow!
|
||||||
follow = super
|
follow = super
|
||||||
|
|
||||||
|
migrate_list_accounts!
|
||||||
UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
|
UnfollowService.new.call(@source_account, @old_target_account, skip_unmerge: true)
|
||||||
|
|
||||||
follow
|
follow
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def migrate_list_accounts!
|
||||||
|
ListAccount.where(follow_id: @original_follow.id).includes(:list).find_each do |list_account|
|
||||||
|
list_account.list.accounts << @target_account
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,9 +8,9 @@ class MoveWorker
|
||||||
@target_account = Account.find(target_account_id)
|
@target_account = Account.find(target_account_id)
|
||||||
|
|
||||||
if @target_account.local? && @source_account.local?
|
if @target_account.local? && @source_account.local?
|
||||||
nb_moved = rewrite_follows!
|
num_moved = rewrite_follows!
|
||||||
@source_account.update_count!(:followers_count, -nb_moved)
|
@source_account.update_count!(:followers_count, -num_moved)
|
||||||
@target_account.update_count!(:followers_count, nb_moved)
|
@target_account.update_count!(:followers_count, num_moved)
|
||||||
else
|
else
|
||||||
queue_follow_unfollows!
|
queue_follow_unfollows!
|
||||||
end
|
end
|
||||||
|
@ -29,12 +29,18 @@ class MoveWorker
|
||||||
private
|
private
|
||||||
|
|
||||||
def rewrite_follows!
|
def rewrite_follows!
|
||||||
|
num_moved = 0
|
||||||
|
|
||||||
@source_account.passive_relationships
|
@source_account.passive_relationships
|
||||||
.where(account: Account.local)
|
.where(account: Account.local)
|
||||||
.where.not(account: @target_account.followers.local)
|
.where.not(account: @target_account.followers.local)
|
||||||
.where.not(account_id: @target_account.id)
|
.where.not(account_id: @target_account.id)
|
||||||
.in_batches
|
.in_batches do |follows|
|
||||||
.update_all(target_account_id: @target_account.id)
|
ListAccount.where(follow: follows).in_batches.update_all(account_id: @target_account.id)
|
||||||
|
num_moved += follows.update_all(target_account_id: @target_account.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
num_moved
|
||||||
end
|
end
|
||||||
|
|
||||||
def queue_follow_unfollows!
|
def queue_follow_unfollows!
|
||||||
|
|
|
@ -5,22 +5,28 @@ require 'rails_helper'
|
||||||
describe MoveWorker do
|
describe MoveWorker do
|
||||||
subject { described_class.new }
|
subject { described_class.new }
|
||||||
|
|
||||||
let(:local_follower) { Fabricate(:account) }
|
let(:local_follower) { Fabricate(:account, domain: nil) }
|
||||||
let(:blocking_account) { Fabricate(:account) }
|
let(:blocking_account) { Fabricate(:account) }
|
||||||
let(:muting_account) { Fabricate(:account) }
|
let(:muting_account) { Fabricate(:account) }
|
||||||
let(:source_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com') }
|
let(:source_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com', uri: 'https://example.org/a', inbox_url: 'https://example.org/a/inbox') }
|
||||||
let(:target_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com') }
|
let(:target_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com', uri: 'https://example.org/b', inbox_url: 'https://example.org/b/inbox') }
|
||||||
let(:local_user) { Fabricate(:user) }
|
let(:local_user) { Fabricate(:user) }
|
||||||
let(:comment) { 'old note prior to move' }
|
let(:comment) { 'old note prior to move' }
|
||||||
let!(:account_note) { Fabricate(:account_note, account: local_user.account, target_account: source_account, comment: comment) }
|
let!(:account_note) { Fabricate(:account_note, account: local_user.account, target_account: source_account, comment: comment) }
|
||||||
|
let(:list) { Fabricate(:list, account: local_follower) }
|
||||||
|
|
||||||
let(:block_service) { double }
|
let(:block_service) { double }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
stub_request(:post, 'https://example.org/a/inbox').to_return(status: 200)
|
||||||
|
stub_request(:post, 'https://example.org/b/inbox').to_return(status: 200)
|
||||||
|
|
||||||
local_follower.follow!(source_account)
|
local_follower.follow!(source_account)
|
||||||
blocking_account.block!(source_account)
|
blocking_account.block!(source_account)
|
||||||
muting_account.mute!(source_account)
|
muting_account.mute!(source_account)
|
||||||
|
|
||||||
|
list.accounts << source_account
|
||||||
|
|
||||||
allow(BlockService).to receive(:new).and_return(block_service)
|
allow(BlockService).to receive(:new).and_return(block_service)
|
||||||
allow(block_service).to receive(:call)
|
allow(block_service).to receive(:call)
|
||||||
end
|
end
|
||||||
|
@ -86,16 +92,27 @@ describe MoveWorker do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
shared_examples 'lists handling' do
|
||||||
|
it 'updates lists' do
|
||||||
|
subject.perform(source_account.id, target_account.id)
|
||||||
|
expect(list.accounts.include?(target_account)).to be true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'both accounts are distant' do
|
context 'both accounts are distant' do
|
||||||
describe 'perform' do
|
describe 'perform' do
|
||||||
it 'calls UnfollowFollowWorker' do
|
it 'calls UnfollowFollowWorker' do
|
||||||
expect_push_bulk_to_match(UnfollowFollowWorker, [[local_follower.id, source_account.id, target_account.id, false]])
|
Sidekiq::Testing.fake! do
|
||||||
subject.perform(source_account.id, target_account.id)
|
subject.perform(source_account.id, target_account.id)
|
||||||
|
expect(UnfollowFollowWorker).to have_enqueued_sidekiq_job(local_follower.id, source_account.id, target_account.id, false)
|
||||||
|
Sidekiq::Worker.drain_all
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
include_examples 'user note handling'
|
include_examples 'user note handling'
|
||||||
include_examples 'block and mute handling'
|
include_examples 'block and mute handling'
|
||||||
include_examples 'followers count handling'
|
include_examples 'followers count handling'
|
||||||
|
include_examples 'lists handling'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -104,13 +121,17 @@ describe MoveWorker do
|
||||||
|
|
||||||
describe 'perform' do
|
describe 'perform' do
|
||||||
it 'calls UnfollowFollowWorker' do
|
it 'calls UnfollowFollowWorker' do
|
||||||
expect_push_bulk_to_match(UnfollowFollowWorker, [[local_follower.id, source_account.id, target_account.id, true]])
|
Sidekiq::Testing.fake! do
|
||||||
subject.perform(source_account.id, target_account.id)
|
subject.perform(source_account.id, target_account.id)
|
||||||
|
expect(UnfollowFollowWorker).to have_enqueued_sidekiq_job(local_follower.id, source_account.id, target_account.id, true)
|
||||||
|
Sidekiq::Worker.clear_all
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
include_examples 'user note handling'
|
include_examples 'user note handling'
|
||||||
include_examples 'block and mute handling'
|
include_examples 'block and mute handling'
|
||||||
include_examples 'followers count handling'
|
include_examples 'followers count handling'
|
||||||
|
include_examples 'lists handling'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -127,6 +148,7 @@ describe MoveWorker do
|
||||||
include_examples 'user note handling'
|
include_examples 'user note handling'
|
||||||
include_examples 'block and mute handling'
|
include_examples 'block and mute handling'
|
||||||
include_examples 'followers count handling'
|
include_examples 'followers count handling'
|
||||||
|
include_examples 'lists handling'
|
||||||
|
|
||||||
it 'does not fail when a local user is already following both accounts' do
|
it 'does not fail when a local user is already following both accounts' do
|
||||||
double_follower = Fabricate(:account)
|
double_follower = Fabricate(:account)
|
||||||
|
|
Loading…
Reference in a new issue