2017-04-12 16:22:38 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class WebfingerResource
|
|
|
|
attr_reader :resource
|
|
|
|
|
2020-05-14 21:28:06 +00:00
|
|
|
class InvalidRequest < StandardError; end
|
|
|
|
|
2017-04-12 16:22:38 +00:00
|
|
|
def initialize(resource)
|
|
|
|
@resource = resource
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
case resource
|
2023-07-27 14:11:17 +00:00
|
|
|
when %r{\A(https?://)?#{instance_actor_regexp}/?\Z}
|
|
|
|
Rails.configuration.x.local_domain
|
2017-04-12 16:22:38 +00:00
|
|
|
when /\Ahttps?/i
|
|
|
|
username_from_url
|
2023-06-06 12:50:51 +00:00
|
|
|
when /@/
|
2017-04-12 16:22:38 +00:00
|
|
|
username_from_acct
|
|
|
|
else
|
2020-05-14 21:28:06 +00:00
|
|
|
raise InvalidRequest
|
2017-04-12 16:22:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-07-27 14:11:17 +00:00
|
|
|
def instance_actor_regexp
|
|
|
|
hosts = [Rails.configuration.x.local_domain, Rails.configuration.x.web_domain]
|
|
|
|
hosts.concat(Rails.configuration.x.alternate_domains) if Rails.configuration.x.alternate_domains.present?
|
|
|
|
|
|
|
|
Regexp.union(hosts)
|
|
|
|
end
|
|
|
|
|
2017-04-12 16:22:38 +00:00
|
|
|
def username_from_url
|
|
|
|
if account_show_page?
|
|
|
|
path_params[:username]
|
2019-07-18 23:44:42 +00:00
|
|
|
elsif instance_actor_page?
|
|
|
|
Rails.configuration.x.local_domain
|
2017-04-12 16:22:38 +00:00
|
|
|
else
|
|
|
|
raise ActiveRecord::RecordNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-18 23:44:42 +00:00
|
|
|
def instance_actor_page?
|
|
|
|
path_params[:controller] == 'instance_actors'
|
|
|
|
end
|
|
|
|
|
2017-04-12 16:22:38 +00:00
|
|
|
def account_show_page?
|
|
|
|
path_params[:controller] == 'accounts' && path_params[:action] == 'show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def path_params
|
|
|
|
Rails.application.routes.recognize_path(resource)
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_from_acct
|
|
|
|
if domain_matches_local?
|
|
|
|
local_username
|
|
|
|
else
|
|
|
|
raise ActiveRecord::RecordNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def split_acct
|
|
|
|
resource_without_acct_string.split('@')
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_without_acct_string
|
2023-05-02 19:07:45 +00:00
|
|
|
resource.delete_prefix('acct:')
|
2017-04-12 16:22:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_username
|
|
|
|
split_acct.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def local_domain
|
|
|
|
split_acct.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def domain_matches_local?
|
2017-04-23 12:19:54 +00:00
|
|
|
TagManager.instance.local_domain?(local_domain) || TagManager.instance.web_domain?(local_domain)
|
2017-04-12 16:22:38 +00:00
|
|
|
end
|
|
|
|
end
|