webfinger_serializer.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # frozen_string_literal: true
  2. class WebfingerSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. attributes :subject, :aliases, :links
  5. def subject
  6. object.to_webfinger_s
  7. end
  8. def aliases
  9. if object.instance_actor?
  10. [instance_actor_url]
  11. else
  12. [short_account_url(object), account_url(object)]
  13. end
  14. end
  15. def links
  16. [
  17. { rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: profile_page_href },
  18. { rel: 'self', type: 'application/activity+json', href: self_href },
  19. { rel: 'http://ostatus.org/schema/1.0/subscribe', template: "#{authorize_interaction_url}?uri={uri}" },
  20. ].tap do |x|
  21. x << { rel: 'http://webfinger.net/rel/avatar', type: object.avatar.content_type, href: full_asset_url(object.avatar_original_url) } if show_avatar?
  22. end
  23. end
  24. private
  25. def show_avatar?
  26. media_present = object.avatar.present? && object.avatar.content_type.present?
  27. # Show avatar only if an instance shows profiles to logged out users
  28. allowed_by_config = ENV['DISALLOW_UNAUTHENTICATED_API_ACCESS'] != 'true' && !Rails.configuration.x.limited_federation_mode
  29. media_present && allowed_by_config
  30. end
  31. def profile_page_href
  32. object.instance_actor? ? about_more_url(instance_actor: true) : short_account_url(object)
  33. end
  34. def self_href
  35. object.instance_actor? ? instance_actor_url : account_url(object)
  36. end
  37. end