move_worker.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. AccountNote.create!(account: note.account, target_account: @target_account, comment: [text, note.comment].join("\n"))
  45. else
  46. new_note.update!(comment: [text, note.comment, "\n", new_note.comment].join("\n"))
  47. end
  48. rescue => e
  49. @deferred_error = e
  50. end
  51. end
  52. def carry_blocks_over!
  53. @source_account.blocked_by_relationships.where(account: Account.local).find_each do |block|
  54. unless block.account.blocking?(@target_account) || block.account.following?(@target_account)
  55. BlockService.new.call(block.account, @target_account)
  56. add_account_note_if_needed!(block.account, 'move_handler.carry_blocks_over_text')
  57. end
  58. rescue => e
  59. @deferred_error = e
  60. end
  61. end
  62. def carry_mutes_over!
  63. @source_account.muted_by_relationships.where(account: Account.local).find_each do |mute|
  64. MuteService.new.call(mute.account, @target_account, notifications: mute.hide_notifications) unless mute.account.muting?(@target_account) || mute.account.following?(@target_account)
  65. add_account_note_if_needed!(mute.account, 'move_handler.carry_mutes_over_text')
  66. rescue => e
  67. @deferred_error = e
  68. end
  69. end
  70. def add_account_note_if_needed!(account, id)
  71. unless AccountNote.where(account: account, target_account: @target_account).exists?
  72. text = I18n.with_locale(account.user&.locale || I18n.default_locale) do
  73. I18n.t(id, acct: @source_account.acct)
  74. end
  75. AccountNote.create!(account: account, target_account: @target_account, comment: text)
  76. end
  77. end
  78. end