webfinger_controller.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. module WellKnown
  3. class WebfingerController < ActionController::Base # rubocop:disable Rails/ApplicationController
  4. include RoutingHelper
  5. before_action :set_account
  6. before_action :check_account_suspension
  7. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  8. rescue_from ActionController::ParameterMissing, WebfingerResource::InvalidRequest, with: :bad_request
  9. def show
  10. expires_in 3.days, public: true
  11. render json: @account, serializer: WebfingerSerializer, content_type: 'application/jrd+json'
  12. end
  13. private
  14. def set_account
  15. username = username_from_resource
  16. @account = begin
  17. if username == Rails.configuration.x.local_domain
  18. Account.representative
  19. else
  20. Account.find_local!(username)
  21. end
  22. end
  23. end
  24. def username_from_resource
  25. resource_user = resource_param
  26. username, domain = resource_user.split('@')
  27. resource_user = "#{username}@#{Rails.configuration.x.local_domain}" if Rails.configuration.x.alternate_domains.include?(domain)
  28. WebfingerResource.new(resource_user).username
  29. end
  30. def resource_param
  31. params.require(:resource)
  32. end
  33. def check_account_suspension
  34. gone if @account.suspended_permanently?
  35. end
  36. def gone
  37. expires_in(3.minutes, public: true)
  38. head 410
  39. end
  40. def bad_request
  41. expires_in(3.minutes, public: true)
  42. head 400
  43. end
  44. def not_found
  45. expires_in(3.minutes, public: true)
  46. head 404
  47. end
  48. end
  49. end