2019-09-19 20:58:19 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: account_aliases
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# account_id :bigint(8)
|
|
|
|
# acct :string default(""), not null
|
|
|
|
# uri :string default(""), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
|
|
|
class AccountAlias < ApplicationRecord
|
|
|
|
belongs_to :account
|
|
|
|
|
|
|
|
validates :acct, presence: true, domain: { acct: true }
|
2019-09-21 09:11:21 +02:00
|
|
|
validates :uri, uniqueness: { scope: :account_id }
|
2020-04-13 06:41:43 +02:00
|
|
|
validate :validate_target_account
|
2019-09-19 20:58:19 +02:00
|
|
|
|
|
|
|
before_validation :set_uri
|
|
|
|
after_create :add_to_account
|
|
|
|
after_destroy :remove_from_account
|
|
|
|
|
2023-10-24 12:06:10 +02:00
|
|
|
normalizes :acct, with: ->(acct) { acct.strip.delete_prefix('@') }
|
2019-09-21 09:11:21 +02:00
|
|
|
|
2022-04-09 20:11:06 +02:00
|
|
|
def pretty_acct
|
2022-11-07 16:17:55 +01:00
|
|
|
username, domain = acct.split('@', 2)
|
2022-04-09 20:11:06 +02:00
|
|
|
domain.nil? ? username : "#{username}@#{Addressable::IDNA.to_unicode(domain)}"
|
|
|
|
end
|
|
|
|
|
2019-09-19 20:58:19 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_uri
|
|
|
|
target_account = ResolveAccountService.new.call(acct)
|
|
|
|
self.uri = ActivityPub::TagManager.instance.uri_for(target_account) unless target_account.nil?
|
2020-10-08 00:34:57 +02:00
|
|
|
rescue Webfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::Error
|
2019-09-19 20:58:19 +02:00
|
|
|
# Validation will take care of it
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_to_account
|
|
|
|
account.update(also_known_as: account.also_known_as + [uri])
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_account
|
|
|
|
account.update(also_known_as: account.also_known_as.reject { |x| x == uri })
|
|
|
|
end
|
2020-04-13 06:41:43 +02:00
|
|
|
|
|
|
|
def validate_target_account
|
2020-04-15 20:33:53 +02:00
|
|
|
if uri.blank?
|
2020-04-13 06:41:43 +02:00
|
|
|
errors.add(:acct, I18n.t('migrations.errors.not_found'))
|
|
|
|
elsif ActivityPub::TagManager.instance.uri_for(account) == uri
|
|
|
|
errors.add(:acct, I18n.t('migrations.errors.move_to_self'))
|
|
|
|
end
|
|
|
|
end
|
2019-09-19 20:58:19 +02:00
|
|
|
end
|