move_worker.rb 3.5 KB

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