You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

  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, AccountIdentityProof,
  14. AccountModerationNote, AccountPin, AccountStat, ListAccount,
  15. PollVote, Mention, AccountDeletionRequest, AccountNote, FollowRecommendationSuppression
  16. ]
  17. owned_classes.each do |klass|
  18. klass.where(account_id: other_account.id).find_each do |record|
  19. begin
  20. record.update_attribute(:account_id, id)
  21. rescue ActiveRecord::RecordNotUnique
  22. next
  23. end
  24. end
  25. end
  26. target_classes = [
  27. Follow, FollowRequest, Block, Mute, AccountModerationNote, AccountPin,
  28. AccountNote
  29. ]
  30. target_classes.each do |klass|
  31. klass.where(target_account_id: other_account.id).find_each do |record|
  32. begin
  33. record.update_attribute(:target_account_id, id)
  34. rescue ActiveRecord::RecordNotUnique
  35. next
  36. end
  37. end
  38. end
  39. CanonicalEmailBlock.where(reference_account_id: other_account.id).find_each do |record|
  40. record.update_attribute(:reference_account_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