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.

47 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class Form::Redirect
  3. include ActiveModel::Model
  4. attr_accessor :account, :target_account, :current_password,
  5. :current_username
  6. attr_reader :acct
  7. validates :acct, presence: true, domain: { acct: true }
  8. validate :validate_target_account
  9. def valid_with_challenge?(current_user)
  10. if current_user.encrypted_password.present?
  11. errors.add(:current_password, :invalid) unless current_user.valid_password?(current_password)
  12. else
  13. errors.add(:current_username, :invalid) unless account.username == current_username
  14. end
  15. return false unless errors.empty?
  16. set_target_account
  17. valid?
  18. end
  19. def acct=(val)
  20. @acct = val.to_s.strip.gsub(/\A@/, '')
  21. end
  22. private
  23. def set_target_account
  24. @target_account = ResolveAccountService.new.call(acct)
  25. rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::Error
  26. # Validation will take care of it
  27. end
  28. def validate_target_account
  29. if target_account.nil?
  30. errors.add(:acct, I18n.t('migrations.errors.not_found'))
  31. else
  32. errors.add(:acct, I18n.t('migrations.errors.already_moved')) if account.moved_to_account_id.present? && account.moved_to_account_id == target_account.id
  33. errors.add(:acct, I18n.t('migrations.errors.move_to_self')) if account.id == target_account.id
  34. end
  35. end
  36. end