Fix various edge cases with local moves (#24812)

This commit is contained in:
Claire
2023-05-03 19:19:25 +02:00
committed by GitHub
parent 1e75eb690d
commit a2a22bad23
4 changed files with 108 additions and 32 deletions

View File

@ -18,6 +18,7 @@ class ListAccount < ApplicationRecord
belongs_to :follow_request, optional: true
validates :account_id, uniqueness: { scope: :list_id }
validate :validate_relationship
before_validation :set_follow
@ -30,4 +31,12 @@ class ListAccount < ApplicationRecord
rescue ActiveRecord::RecordNotFound
self.follow_request = FollowRequest.find_by!(account_id: list.account_id, target_account_id: account.id)
end
def validate_relationship
return if list.account_id == account_id
errors.add(:account_id, 'follow relationship missing') if follow_id.nil? && follow_request_id.nil?
errors.add(:follow, 'mismatched accounts') if follow_id.present? && follow.target_account_id != account_id
errors.add(:follow_request, 'mismatched accounts') if follow_request_id.present? && follow_request.target_account_id != account_id
end
end

View File

@ -33,6 +33,16 @@ class FollowMigrationService < FollowService
follow_request
end
def change_follow_options!
migrate_list_accounts!
super
end
def change_follow_request_options!
migrate_list_accounts!
super
end
def direct_follow!
follow = super
@ -45,6 +55,8 @@ class FollowMigrationService < FollowService
def migrate_list_accounts!
ListAccount.where(follow_id: @original_follow.id).includes(:list).find_each do |list_account|
list_account.list.accounts << @target_account
rescue ActiveRecord::RecordInvalid
nil
end
end
end

View File

@ -31,6 +31,32 @@ class MoveWorker
def rewrite_follows!
num_moved = 0
# First, approve pending follow requests for the new account,
# this allows correctly processing list memberships with pending
# follow requests
FollowRequest.where(account: @source_account.followers, target_account_id: @target_account.id).find_each do |follow_request|
ListAccount.where(follow_id: follow_request.id).includes(:list).find_each do |list_account|
list_account.list.accounts << @target_account
rescue ActiveRecord::RecordInvalid
nil
end
follow_request.authorize!
end
# Then handle accounts that follow both the old and new account
@source_account.passive_relationships
.where(account: Account.local)
.where(account: @target_account.followers.local)
.in_batches do |follows|
ListAccount.where(follow: follows).includes(:list).find_each do |list_account|
list_account.list.accounts << @target_account
rescue ActiveRecord::RecordInvalid
nil
end
end
# Finally, handle the common case of accounts not following the new account
@source_account.passive_relationships
.where(account: Account.local)
.where.not(account: @target_account.followers.local)