~cytrogen/masto-fe

ref: 53f5b27bd10e5f471b59cd0597d67ea12587c95a masto-fe/app/lib/webfinger_resource.rb -rw-r--r-- 1.7 KiB
53f5b27b — Claire Merge commit '640421f661ee4d7e76a2aab607e7b15687940b6f' into glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

class WebfingerResource
  attr_reader :resource

  class InvalidRequest < StandardError; end

  def initialize(resource)
    @resource = resource
  end

  def username
    case resource
    when %r{\A(https?://)?#{instance_actor_regexp}/?\Z}
      Rails.configuration.x.local_domain
    when /\Ahttps?/i
      username_from_url
    when /@/
      username_from_acct
    else
      raise InvalidRequest
    end
  end

  private

  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

  def username_from_url
    if account_show_page?
      path_params[:username]
    elsif instance_actor_page?
      Rails.configuration.x.local_domain
    else
      raise ActiveRecord::RecordNotFound
    end
  end

  def instance_actor_page?
    path_params[:controller] == 'instance_actors'
  end

  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
    resource.delete_prefix('acct:')
  end

  def local_username
    split_acct.first
  end

  def local_domain
    split_acct.last
  end

  def domain_matches_local?
    TagManager.instance.local_domain?(local_domain) || TagManager.instance.web_domain?(local_domain)
  end
end