diff options
author | Matt Jankowski <mjankowski@thoughtbot.com> | 2017-04-12 12:22:38 -0400 |
---|---|---|
committer | Eugen <eugen@zeonfederated.com> | 2017-04-12 18:22:38 +0200 |
commit | aa9079838648e9656a1bf8d10151713686e1c0dd (patch) | |
tree | badfc5eb2d26c8fb53f6457c6408508941023202 /app/lib/webfinger_resource.rb | |
parent | 0930ce5560749f5ede3aa70aef4130ad1a9dc6ba (diff) |
Webfinger resource to extract username from resource string (#1607)
* Add WebfingerResource class to extract usernames * Use WebfingerResource in xrd#webfinger
Diffstat (limited to 'app/lib/webfinger_resource.rb')
-rw-r--r-- | app/lib/webfinger_resource.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/app/lib/webfinger_resource.rb b/app/lib/webfinger_resource.rb new file mode 100644 index 000000000..8c5db795d --- /dev/null +++ b/app/lib/webfinger_resource.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +class WebfingerResource + attr_reader :resource + + def initialize(resource) + @resource = resource + end + + def username + case resource + when /\Ahttps?/i + username_from_url + when /\@/ + username_from_acct + else + raise(ActiveRecord::RecordNotFound) + end + end + + private + + def username_from_url + if account_show_page? + path_params[:username] + else + raise ActiveRecord::RecordNotFound + end + 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.gsub(/\Aacct:/, '') + 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) + end +end |