1
0

move_worker.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. num_moved = rewrite_follows!
  9. @source_account.update_count!(:followers_count, -num_moved)
  10. @target_account.update_count!(:followers_count, num_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. num_moved = 0
  25. # First, approve pending follow requests for the new account,
  26. # this allows correctly processing list memberships with pending
  27. # follow requests
  28. FollowRequest.where(account: @source_account.followers, target_account_id: @target_account.id).find_each do |follow_request|
  29. ListAccount.where(follow_id: follow_request.id).includes(:list).find_each do |list_account|
  30. list_account.list.accounts << @target_account
  31. rescue ActiveRecord::RecordInvalid
  32. nil
  33. end
  34. follow_request.authorize!
  35. end
  36. # Then handle accounts that follow both the old and new account
  37. @source_account.passive_relationships
  38. .where(account: Account.local)
  39. .where(account: @target_account.followers.local)
  40. .in_batches do |follows|
  41. ListAccount.where(follow: follows).includes(:list).find_each do |list_account|
  42. list_account.list.accounts << @target_account
  43. rescue ActiveRecord::RecordInvalid
  44. nil
  45. end
  46. end
  47. # Finally, handle the common case of accounts not following the new account
  48. @source_account.passive_relationships
  49. .where(account: Account.local)
  50. .where.not(account: @target_account.followers.local)
  51. .where.not(account_id: @target_account.id)
  52. .in_batches do |follows|
  53. ListAccount.where(follow: follows).in_batches.update_all(account_id: @target_account.id)
  54. num_moved += follows.update_all(target_account_id: @target_account.id)
  55. end
  56. num_moved
  57. end
  58. def queue_follow_unfollows!
  59. bypass_locked = @target_account.local?
  60. @source_account.followers.local.select(:id).reorder(nil).find_in_batches do |accounts|
  61. UnfollowFollowWorker.push_bulk(accounts.map(&:id)) { |follower_id| [follower_id, @source_account.id, @target_account.id, bypass_locked] }
  62. rescue => e
  63. @deferred_error = e
  64. end
  65. end
  66. def copy_account_notes!
  67. AccountNote.where(target_account: @source_account).find_each do |note|
  68. text = I18n.with_locale(note.account.user&.locale.presence || I18n.default_locale) do
  69. I18n.t('move_handler.copy_account_note_text', acct: @source_account.acct)
  70. end
  71. new_note = AccountNote.find_by(account: note.account, target_account: @target_account)
  72. if new_note.nil?
  73. begin
  74. AccountNote.create!(account: note.account, target_account: @target_account, comment: [text, note.comment].join("\n"))
  75. rescue ActiveRecord::RecordInvalid
  76. AccountNote.create!(account: note.account, target_account: @target_account, comment: note.comment)
  77. end
  78. else
  79. new_note.update!(comment: [text, note.comment, "\n", new_note.comment].join("\n"))
  80. end
  81. rescue ActiveRecord::RecordInvalid
  82. nil
  83. rescue => e
  84. @deferred_error = e
  85. end
  86. end
  87. def carry_blocks_over!
  88. @source_account.blocked_by_relationships.where(account: Account.local).find_each do |block|
  89. unless block.account.blocking?(@target_account) || block.account.following?(@target_account)
  90. BlockService.new.call(block.account, @target_account)
  91. add_account_note_if_needed!(block.account, 'move_handler.carry_blocks_over_text')
  92. end
  93. rescue => e
  94. @deferred_error = e
  95. end
  96. end
  97. def carry_mutes_over!
  98. @source_account.muted_by_relationships.where(account: Account.local).find_each do |mute|
  99. MuteService.new.call(mute.account, @target_account, notifications: mute.hide_notifications) unless mute.account.muting?(@target_account) || mute.account.following?(@target_account)
  100. add_account_note_if_needed!(mute.account, 'move_handler.carry_mutes_over_text')
  101. rescue => e
  102. @deferred_error = e
  103. end
  104. end
  105. def add_account_note_if_needed!(account, id)
  106. unless AccountNote.where(account: account, target_account: @target_account).exists?
  107. text = I18n.with_locale(account.user&.locale.presence || I18n.default_locale) do
  108. I18n.t(id, acct: @source_account.acct)
  109. end
  110. AccountNote.create!(account: account, target_account: @target_account, comment: text)
  111. end
  112. end
  113. end