webfinger_resource.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. class WebfingerResource
  3. attr_reader :resource
  4. def initialize(resource)
  5. @resource = resource
  6. end
  7. def username
  8. case resource
  9. when /\Ahttps?/i
  10. username_from_url
  11. when /\@/
  12. username_from_acct
  13. else
  14. raise(ActiveRecord::RecordNotFound)
  15. end
  16. end
  17. private
  18. def username_from_url
  19. if account_show_page?
  20. path_params[:username]
  21. elsif instance_actor_page?
  22. Rails.configuration.x.local_domain
  23. else
  24. raise ActiveRecord::RecordNotFound
  25. end
  26. end
  27. def instance_actor_page?
  28. path_params[:controller] == 'instance_actors'
  29. end
  30. def account_show_page?
  31. path_params[:controller] == 'accounts' && path_params[:action] == 'show'
  32. end
  33. def path_params
  34. Rails.application.routes.recognize_path(resource)
  35. end
  36. def username_from_acct
  37. if domain_matches_local?
  38. local_username
  39. else
  40. raise ActiveRecord::RecordNotFound
  41. end
  42. end
  43. def split_acct
  44. resource_without_acct_string.split('@')
  45. end
  46. def resource_without_acct_string
  47. resource.gsub(/\Aacct:/, '')
  48. end
  49. def local_username
  50. split_acct.first
  51. end
  52. def local_domain
  53. split_acct.last
  54. end
  55. def domain_matches_local?
  56. TagManager.instance.local_domain?(local_domain) || TagManager.instance.web_domain?(local_domain)
  57. end
  58. end