move_worker.rb 1.1 KB

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