webfinger_resource.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. else
  22. raise ActiveRecord::RecordNotFound
  23. end
  24. end
  25. def account_show_page?
  26. path_params[:controller] == 'accounts' && path_params[:action] == 'show'
  27. end
  28. def path_params
  29. Rails.application.routes.recognize_path(resource)
  30. end
  31. def username_from_acct
  32. if domain_matches_local?
  33. local_username
  34. else
  35. raise ActiveRecord::RecordNotFound
  36. end
  37. end
  38. def split_acct
  39. resource_without_acct_string.split('@')
  40. end
  41. def resource_without_acct_string
  42. resource.gsub(/\Aacct:/, '')
  43. end
  44. def local_username
  45. split_acct.first
  46. end
  47. def local_domain
  48. split_acct.last
  49. end
  50. def domain_matches_local?
  51. TagManager.instance.local_domain?(local_domain) || TagManager.instance.web_domain?(local_domain)
  52. end
  53. end