account_merging.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # frozen_string_literal: true
  2. module AccountMerging
  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).find_each do |record|
  20. begin
  21. record.update_attribute(:account_id, id)
  22. rescue ActiveRecord::RecordNotUnique
  23. next
  24. end
  25. end
  26. end
  27. target_classes = [
  28. Follow, FollowRequest, Block, Mute, AccountModerationNote, AccountPin,
  29. AccountNote
  30. ]
  31. target_classes.each do |klass|
  32. klass.where(target_account_id: other_account.id).find_each do |record|
  33. begin
  34. record.update_attribute(:target_account_id, id)
  35. rescue ActiveRecord::RecordNotUnique
  36. next
  37. end
  38. end
  39. end
  40. CanonicalEmailBlock.where(reference_account_id: other_account.id).find_each do |record|
  41. record.update_attribute(:reference_account_id, id)
  42. end
  43. Appeal.where(account_warning_id: other_account.id).find_each do |record|
  44. record.update_attribute(:account_warning_id, id)
  45. end
  46. # Some follow relationships have moved, so the cache is stale
  47. Rails.cache.delete_matched("followers_hash:#{id}:*")
  48. Rails.cache.delete_matched("relationships:#{id}:*")
  49. Rails.cache.delete_matched("relationships:*:#{id}")
  50. end
  51. end