1
0

webfinger_resource.rb 1.3 KB

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