clacks/lib/clacks_web/controllers/web_finger_controller.ex

34 lines
928 B
Elixir
Raw Normal View History

2019-10-01 16:35:27 +00:00
defmodule ClacksWeb.WebFingerController do
use ClacksWeb, :controller
alias Clacks.Actor
@acct_regex ~r/(?:acct:)?([a-z0-9_]+)(?:@.+)?/i
def get(conn, %{"resource" => resource}) do
with [_, nickname] <- Regex.run(@acct_regex, resource),
2019-10-02 15:12:34 +00:00
%Actor{local: true} = actor <- Actor.get_by_nickname(nickname) do
2019-10-01 16:35:27 +00:00
url = Application.get_env(:clacks, ClacksWeb.Endpoint)[:url]
host = url[:host]
port = url[:port]
maybe_port = if port == URI.default_port(url[:scheme]), do: "", else: ":#{port}"
conn
|> json(%{
"subject" => "acct:#{actor.nickname}@#{host}#{maybe_port}",
"links" => [
%{
"rel" => "self",
"type" => "application/activity+json",
"href" => actor.ap_id
}
]
})
else
_ ->
conn
|> put_status(404)
|> json(%{error: "No such resource"})
end
end
end