2017-06-01 03:28:45 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-01 20:00:41 +09:00
|
|
|
module Account::FinderConcern
|
2017-06-01 03:28:45 +09:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def find_local!(username)
|
|
|
|
find_local(username) || raise(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_remote!(username, domain)
|
|
|
|
find_remote(username, domain) || raise(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
2019-01-05 15:17:12 +09:00
|
|
|
def representative
|
2024-02-19 20:09:43 +09:00
|
|
|
actor = Account.find(Account::INSTANCE_ACTOR_ID).tap(&:ensure_keys!)
|
2023-01-19 00:33:03 +09:00
|
|
|
actor.update!(username: 'mastodon.internal') if actor.username.include?(':')
|
|
|
|
actor
|
2021-02-10 02:12:54 +09:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2024-02-19 20:09:43 +09:00
|
|
|
Account.create!(id: Account::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: 'mastodon.internal')
|
2019-01-05 15:17:12 +09:00
|
|
|
end
|
|
|
|
|
2017-06-01 03:28:45 +09:00
|
|
|
def find_local(username)
|
|
|
|
find_remote(username, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_remote(username, domain)
|
|
|
|
AccountFinder.new(username, domain).account
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AccountFinder
|
|
|
|
attr_reader :username, :domain
|
|
|
|
|
|
|
|
def initialize(username, domain)
|
|
|
|
@username = username
|
|
|
|
@domain = domain
|
|
|
|
end
|
|
|
|
|
|
|
|
def account
|
2018-03-27 11:33:57 +09:00
|
|
|
scoped_accounts.order(id: :asc).take
|
2017-06-01 03:28:45 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def scoped_accounts
|
|
|
|
Account.unscoped.tap do |scope|
|
2017-06-01 21:20:36 +09:00
|
|
|
scope.merge! with_usernames
|
2017-06-01 03:28:45 +09:00
|
|
|
scope.merge! matching_username
|
|
|
|
scope.merge! matching_domain
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-01 21:20:36 +09:00
|
|
|
def with_usernames
|
2020-02-01 23:42:24 +09:00
|
|
|
Account.where.not(Account.arel_table[:username].lower.eq '')
|
2017-06-01 21:20:36 +09:00
|
|
|
end
|
|
|
|
|
2017-06-01 03:28:45 +09:00
|
|
|
def matching_username
|
2017-06-01 21:20:36 +09:00
|
|
|
Account.where(Account.arel_table[:username].lower.eq username.to_s.downcase)
|
2017-06-01 03:28:45 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def matching_domain
|
2020-02-01 23:42:24 +09:00
|
|
|
Account.where(Account.arel_table[:domain].lower.eq(domain.nil? ? nil : domain.to_s.downcase))
|
2017-06-01 03:28:45 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|