merging.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # frozen_string_literal: true
  2. module Account::Merging
  3. extend ActiveSupport::Concern
  4. def merge_with!(other_account)
  5. # Since it's the same remote resource, the remote resource likely
  6. # already believes we are following/blocking, so it's safe to
  7. # re-attribute the relationships too. However, during the presence
  8. # of the index bug users could have *also* followed the reference
  9. # account already, therefore mass update will not work and we need
  10. # to check for (and skip past) uniqueness errors
  11. owned_classes = [
  12. Status, StatusPin, MediaAttachment, Poll, Report, Tombstone, Favourite,
  13. Follow, FollowRequest, Block, Mute,
  14. AccountModerationNote, AccountPin, AccountStat, ListAccount,
  15. PollVote, Mention, AccountDeletionRequest, AccountNote, FollowRecommendationSuppression,
  16. Appeal
  17. ]
  18. owned_classes.each do |klass|
  19. klass.where(account_id: other_account.id).reorder(nil).find_each do |record|
  20. record.update_attribute(:account_id, id)
  21. rescue ActiveRecord::RecordNotUnique
  22. next
  23. end
  24. end
  25. target_classes = [
  26. Follow, FollowRequest, Block, Mute, AccountModerationNote, AccountPin,
  27. AccountNote
  28. ]
  29. target_classes.each do |klass|
  30. klass.where(target_account_id: other_account.id).reorder(nil).find_each do |record|
  31. record.update_attribute(:target_account_id, id)
  32. rescue ActiveRecord::RecordNotUnique
  33. next
  34. end
  35. end
  36. CanonicalEmailBlock.where(reference_account_id: other_account.id).find_each do |record|
  37. record.update_attribute(:reference_account_id, id)
  38. end
  39. Appeal.where(account_warning_id: other_account.id).find_each do |record|
  40. record.update_attribute(:account_warning_id, id)
  41. end
  42. # Some follow relationships have moved, so the cache is stale
  43. Rails.cache.delete_matched("followers_hash:#{id}:*")
  44. Rails.cache.delete_matched("relationships:#{id}:*")
  45. Rails.cache.delete_matched("relationships:*:#{id}")
  46. end
  47. end