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.

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