2019-09-19 18:58:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: account_migrations
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# account_id :bigint(8)
|
|
|
|
# acct :string default(""), not null
|
|
|
|
# followers_count :bigint(8) default(0), not null
|
|
|
|
# target_account_id :bigint(8)
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
|
|
|
class AccountMigration < ApplicationRecord
|
2021-02-02 13:49:57 +00:00
|
|
|
include Redisable
|
2022-05-12 22:02:35 +00:00
|
|
|
include Lockable
|
2021-02-02 13:49:57 +00:00
|
|
|
|
2019-09-19 18:58:19 +00:00
|
|
|
COOLDOWN_PERIOD = 30.days.freeze
|
|
|
|
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :target_account, class_name: 'Account'
|
|
|
|
|
|
|
|
before_validation :set_target_account
|
|
|
|
before_validation :set_followers_count
|
|
|
|
|
2023-10-24 10:06:10 +00:00
|
|
|
normalizes :acct, with: ->(acct) { acct.strip.delete_prefix('@') }
|
|
|
|
|
2019-09-19 18:58:19 +00:00
|
|
|
validates :acct, presence: true, domain: { acct: true }
|
|
|
|
validate :validate_migration_cooldown
|
|
|
|
validate :validate_target_account
|
|
|
|
|
2024-09-13 08:43:21 +00:00
|
|
|
scope :within_cooldown, -> { where(created_at: cooldown_duration_ago..) }
|
2019-09-19 18:58:19 +00:00
|
|
|
|
|
|
|
attr_accessor :current_password, :current_username
|
|
|
|
|
2024-09-13 08:43:21 +00:00
|
|
|
def self.cooldown_duration_ago
|
|
|
|
Time.current - COOLDOWN_PERIOD
|
|
|
|
end
|
|
|
|
|
2019-09-19 18:58:19 +00:00
|
|
|
def save_with_challenge(current_user)
|
|
|
|
if current_user.encrypted_password.present?
|
|
|
|
errors.add(:current_password, :invalid) unless current_user.valid_password?(current_password)
|
|
|
|
else
|
|
|
|
errors.add(:current_username, :invalid) unless account.username == current_username
|
|
|
|
end
|
|
|
|
|
|
|
|
return false unless errors.empty?
|
|
|
|
|
2023-05-02 16:16:07 +00:00
|
|
|
with_redis_lock("account_migration:#{account.id}") do
|
2022-05-12 22:02:35 +00:00
|
|
|
save
|
2021-02-02 13:49:57 +00:00
|
|
|
end
|
2019-09-19 18:58:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cooldown_at
|
|
|
|
created_at + COOLDOWN_PERIOD
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_target_account
|
2022-11-07 02:31:38 +00:00
|
|
|
self.target_account = ResolveAccountService.new.call(acct, skip_cache: true)
|
2024-10-08 14:59:51 +00:00
|
|
|
rescue Webfinger::Error, *Mastodon::HTTP_CONNECTION_ERRORS, Mastodon::Error, Addressable::URI::InvalidURIError
|
2019-09-19 18:58:19 +00:00
|
|
|
# Validation will take care of it
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_followers_count
|
|
|
|
self.followers_count = account.followers_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_target_account
|
|
|
|
if target_account.nil?
|
|
|
|
errors.add(:acct, I18n.t('migrations.errors.not_found'))
|
|
|
|
else
|
|
|
|
errors.add(:acct, I18n.t('migrations.errors.missing_also_known_as')) unless target_account.also_known_as.include?(ActivityPub::TagManager.instance.uri_for(account))
|
|
|
|
errors.add(:acct, I18n.t('migrations.errors.already_moved')) if account.moved_to_account_id.present? && account.moved_to_account_id == target_account.id
|
|
|
|
errors.add(:acct, I18n.t('migrations.errors.move_to_self')) if account.id == target_account.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_migration_cooldown
|
|
|
|
errors.add(:base, I18n.t('migrations.errors.on_cooldown')) if account.migrations.within_cooldown.exists?
|
|
|
|
end
|
|
|
|
end
|