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.

56 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,
  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. 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).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