@ -18,7 +18,12 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do
@@ -18,7 +18,12 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do
accept = conn |> get_req_header ( " accept " ) |> Enum . join ( " , " )
if String . contains? ( accept , " application/nostr+json " ) do
relay_info = Application . get_env ( :gc_index_relay , :relay_info , [ ] ) |> Map . new ( )
base_url = build_base_url ( conn )
relay_info =
Application . get_env ( :gc_index_relay , :relay_info , [ ] )
|> Map . new ( )
|> resolve_image_urls ( base_url )
conn
|> put_resp_content_type ( " application/nostr+json " )
@ -30,4 +35,39 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do
@@ -30,4 +35,39 @@ defmodule GcIndexRelayWeb.Plugs.RelayInfo do
end
def call ( conn , _opts ) , do : conn
# Build "scheme://host[:port]" from the incoming request.
# Standard ports (80 for http, 443 for https) are omitted.
defp build_base_url ( conn ) do
port_suffix =
case { conn . scheme , conn . port } do
{ :http , 80 } -> " "
{ :https , 443 } -> " "
{ _ , port } -> " : #{ port } "
end
" #{ conn . scheme } :// #{ conn . host } #{ port_suffix } "
end
# Prepend base_url to any relative (non-absolute) value for :icon and :banner.
defp resolve_image_urls ( relay_info , base_url ) do
relay_info
|> resolve_field ( :icon , base_url )
|> resolve_field ( :banner , base_url )
end
defp resolve_field ( map , key , base_url ) do
case Map . get ( map , key ) do
nil -> map
" " -> map
url when is_binary ( url ) ->
if String . starts_with? ( url , [ " http:// " , " https:// " ] ) do
map
else
path = if String . starts_with? ( url , " / " ) , do : url , else : " / #{ url } "
Map . put ( map , key , " #{ base_url } #{ path } " )
end
_ -> map
end
end
end