webfinger_controller.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. module WellKnown
  3. class WebfingerController < ActionController::Base
  4. include RoutingHelper
  5. before_action { response.headers['Vary'] = 'Accept' }
  6. def show
  7. @account = Account.find_local!(username_from_resource)
  8. respond_to do |format|
  9. format.any(:json, :html) do
  10. render json: @account, serializer: WebfingerSerializer, content_type: 'application/jrd+json'
  11. end
  12. format.xml do
  13. render content_type: 'application/xrd+xml'
  14. end
  15. end
  16. expires_in(3.days, public: true)
  17. rescue ActiveRecord::RecordNotFound
  18. head 404
  19. end
  20. private
  21. def username_from_resource
  22. resource_user = resource_param
  23. username, domain = resource_user.split('@')
  24. if Rails.configuration.x.alternate_domains.include?(domain)
  25. resource_user = "#{username}@#{Rails.configuration.x.local_domain}"
  26. end
  27. WebfingerResource.new(resource_user).username
  28. end
  29. def resource_param
  30. params.require(:resource)
  31. end
  32. end
  33. end