You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.9 KiB

  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. copy_account_notes!
  13. carry_blocks_over!
  14. carry_mutes_over!
  15. rescue ActiveRecord::RecordNotFound
  16. true
  17. end
  18. private
  19. def rewrite_follows!
  20. @source_account.passive_relationships
  21. .where(account: Account.local)
  22. .where.not(account: @target_account.followers.local)
  23. .where.not(account_id: @target_account.id)
  24. .in_batches
  25. .update_all(target_account_id: @target_account.id)
  26. end
  27. def queue_follow_unfollows!
  28. bypass_locked = @target_account.local?
  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, bypass_locked] }
  31. end
  32. end
  33. def copy_account_notes!
  34. AccountNote.where(target_account: @source_account).find_each do |note|
  35. text = I18n.with_locale(note.account.user.locale || I18n.default_locale) do
  36. I18n.t('move_handler.copy_account_note_text', acct: @source_account.acct)
  37. end
  38. new_note = AccountNote.find_by(account: note.account, target_account: @target_account)
  39. if new_note.nil?
  40. AccountNote.create!(account: note.account, target_account: @target_account, comment: [text, note.comment].join('\n'))
  41. else
  42. new_note.update!(comment: [text, note.comment, '\n', new_note.comment].join('\n'))
  43. end
  44. end
  45. end
  46. def carry_blocks_over!
  47. @source_account.blocked_by_relationships.where(account: Account.local).find_each do |block|
  48. unless block.account.blocking?(@target_account) || block.account.following?(@target_account)
  49. BlockService.new.call(block.account, @target_account)
  50. add_account_note_if_needed!(block.account, 'move_handler.carry_blocks_over_text')
  51. end
  52. end
  53. end
  54. def carry_mutes_over!
  55. @source_account.muted_by_relationships.where(account: Account.local).find_each do |mute|
  56. MuteService.new.call(mute.account, @target_account, notifications: mute.hide_notifications) unless mute.account.muting?(@target_account) || mute.account.following?(@target_account)
  57. add_account_note_if_needed!(mute.account, 'move_handler.carry_mutes_over_text')
  58. end
  59. end
  60. def add_account_note_if_needed!(account, id)
  61. unless AccountNote.where(account: account, target_account: @target_account).exists?
  62. text = I18n.with_locale(account.user.locale || I18n.default_locale) do
  63. I18n.t(id, acct: @source_account.acct)
  64. end
  65. AccountNote.create!(account: account, target_account: @target_account, comment: text)
  66. end
  67. end
  68. end