account_migration.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: account_migrations
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # acct :string default(""), not null
  9. # followers_count :bigint(8) default(0), not null
  10. # target_account_id :bigint(8)
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. #
  14. class AccountMigration < ApplicationRecord
  15. include Redisable
  16. COOLDOWN_PERIOD = 30.days.freeze
  17. belongs_to :account
  18. belongs_to :target_account, class_name: 'Account'
  19. before_validation :set_target_account
  20. before_validation :set_followers_count
  21. validates :acct, presence: true, domain: { acct: true }
  22. validate :validate_migration_cooldown
  23. validate :validate_target_account
  24. scope :within_cooldown, ->(now = Time.now.utc) { where(arel_table[:created_at].gteq(now - COOLDOWN_PERIOD)) }
  25. attr_accessor :current_password, :current_username
  26. def save_with_challenge(current_user)
  27. if current_user.encrypted_password.present?
  28. errors.add(:current_password, :invalid) unless current_user.valid_password?(current_password)
  29. else
  30. errors.add(:current_username, :invalid) unless account.username == current_username
  31. end
  32. return false unless errors.empty?
  33. RedisLock.acquire(lock_options) do |lock|
  34. if lock.acquired?
  35. save
  36. else
  37. raise Mastodon::RaceConditionError
  38. end
  39. end
  40. end
  41. def cooldown_at
  42. created_at + COOLDOWN_PERIOD
  43. end
  44. def acct=(val)
  45. super(val.to_s.strip.gsub(/\A@/, ''))
  46. end
  47. private
  48. def set_target_account
  49. self.target_account = ResolveAccountService.new.call(acct)
  50. rescue Webfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::Error
  51. # Validation will take care of it
  52. end
  53. def set_followers_count
  54. self.followers_count = account.followers_count
  55. end
  56. def validate_target_account
  57. if target_account.nil?
  58. errors.add(:acct, I18n.t('migrations.errors.not_found'))
  59. else
  60. errors.add(:acct, I18n.t('migrations.errors.missing_also_known_as')) unless target_account.also_known_as.include?(ActivityPub::TagManager.instance.uri_for(account))
  61. errors.add(:acct, I18n.t('migrations.errors.already_moved')) if account.moved_to_account_id.present? && account.moved_to_account_id == target_account.id
  62. errors.add(:acct, I18n.t('migrations.errors.move_to_self')) if account.id == target_account.id
  63. end
  64. end
  65. def validate_migration_cooldown
  66. errors.add(:base, I18n.t('migrations.errors.on_cooldown')) if account.migrations.within_cooldown.exists?
  67. end
  68. def lock_options
  69. { redis: redis, key: "account_migration:#{account.id}" }
  70. end
  71. end