account_migration.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. include Lockable
  17. COOLDOWN_PERIOD = 30.days.freeze
  18. belongs_to :account
  19. belongs_to :target_account, class_name: 'Account'
  20. before_validation :set_target_account
  21. before_validation :set_followers_count
  22. normalizes :acct, with: ->(acct) { acct.strip.delete_prefix('@') }
  23. validates :acct, presence: true, domain: { acct: true }
  24. validate :validate_migration_cooldown
  25. validate :validate_target_account
  26. scope :within_cooldown, ->(now = Time.now.utc) { where(arel_table[:created_at].gteq(now - COOLDOWN_PERIOD)) }
  27. attr_accessor :current_password, :current_username
  28. def save_with_challenge(current_user)
  29. if current_user.encrypted_password.present?
  30. errors.add(:current_password, :invalid) unless current_user.valid_password?(current_password)
  31. else
  32. errors.add(:current_username, :invalid) unless account.username == current_username
  33. end
  34. return false unless errors.empty?
  35. with_redis_lock("account_migration:#{account.id}") do
  36. save
  37. end
  38. end
  39. def cooldown_at
  40. created_at + COOLDOWN_PERIOD
  41. end
  42. private
  43. def set_target_account
  44. self.target_account = ResolveAccountService.new.call(acct, skip_cache: true)
  45. rescue Webfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::Error, Addressable::URI::InvalidURIError
  46. # Validation will take care of it
  47. end
  48. def set_followers_count
  49. self.followers_count = account.followers_count
  50. end
  51. def validate_target_account
  52. if target_account.nil?
  53. errors.add(:acct, I18n.t('migrations.errors.not_found'))
  54. else
  55. errors.add(:acct, I18n.t('migrations.errors.missing_also_known_as')) unless target_account.also_known_as.include?(ActivityPub::TagManager.instance.uri_for(account))
  56. errors.add(:acct, I18n.t('migrations.errors.already_moved')) if account.moved_to_account_id.present? && account.moved_to_account_id == target_account.id
  57. errors.add(:acct, I18n.t('migrations.errors.move_to_self')) if account.id == target_account.id
  58. end
  59. end
  60. def validate_migration_cooldown
  61. errors.add(:base, I18n.t('migrations.errors.on_cooldown')) if account.migrations.within_cooldown.exists?
  62. end
  63. end