Add user route

This commit is contained in:
Shadowfacts 2019-09-30 22:26:08 -04:00
parent 6feba31ff3
commit 541e329dc4
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 28 additions and 2 deletions

View File

@ -27,6 +27,11 @@ defmodule Clacks.Actor do
|> validate_required([:ap_id, :nickname, :local, :data]) |> validate_required([:ap_id, :nickname, :local, :data])
end end
@spec get_by_nickanme(nickname :: String.t()) :: t() | nil
def get_by_nickanme(nickname) do
Repo.one(from a in __MODULE__, where: a.nickname == ^nickname)
end
@spec get_by_ap_id(ap_id :: String.t(), force_refetch :: boolean()) :: t() | nil @spec get_by_ap_id(ap_id :: String.t(), force_refetch :: boolean()) :: t() | nil
def get_by_ap_id(ap_id, force_refetch \\ false) do def get_by_ap_id(ap_id, force_refetch \\ false) do
if force_refetch do if force_refetch do

View File

@ -0,0 +1,22 @@
defmodule ClacksWeb.ActorController do
use ClacksWeb, :controller
alias Clacks.{Repo, Actor}
import Ecto.Query
def get(conn, %{"nickname" => nickname}) do
case Actor.get_by_nickanme(nickname) do
%Actor{local: true, data: data} ->
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
%Actor{local: false, ap_id: ap_id} ->
conn
|> redirect(external: ap_id)
_ ->
conn
|> put_status(404)
end
end
end

View File

@ -15,14 +15,13 @@ defmodule ClacksWeb.Router do
scope "/", ClacksWeb do scope "/", ClacksWeb do
pipe_through :browser pipe_through :browser
get "/", PageController, :index
end end
scope "/", ClacksWeb do scope "/", ClacksWeb do
pipe_through :activitypub pipe_through :activitypub
get "/objects/:id", ObjectsController, :get get "/objects/:id", ObjectsController, :get
get "/users/:nickname", ActorController, :get
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.