clacks/lib/clacks_web/controllers/actor_controller.ex

123 lines
3.0 KiB
Elixir
Raw Normal View History

2019-10-01 02:26:08 +00:00
defmodule ClacksWeb.ActorController do
use ClacksWeb, :controller
2019-10-06 23:41:18 +00:00
alias Clacks.{Actor, User}
2019-10-01 02:26:08 +00:00
2019-10-02 15:12:34 +00:00
@context "https://www.w3.org/ns/activitystreams"
plug :get_actor
2019-10-06 23:41:18 +00:00
defp get_actor(%Plug.Conn{path_params: %{"username" => username}} = conn, _opts) do
case User.get_by_username(username) do
2019-10-02 15:12:34 +00:00
nil ->
conn
|> put_status(404)
|> halt()
2019-10-06 23:41:18 +00:00
%User{actor: actor} ->
2019-10-02 15:12:34 +00:00
assign(conn, :actor, actor)
end
end
defp get_actor(conn, _opts), do: conn
2019-10-06 23:41:18 +00:00
def get(%Plug.Conn{assigns: %{format: "activity+json"}} = conn, _params) do
actor = conn.assigns[:actor]
2019-10-01 02:26:08 +00:00
2019-10-06 23:41:18 +00:00
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(actor.data)
end
def get(%Plug.Conn{assigns: %{format: "html"}} = conn, _params) do
conn
|> put_view(ClacksWeb.FrontendView)
|> ClacksWeb.FrontendController.call(:profile)
2019-10-02 15:12:34 +00:00
end
2019-10-01 02:26:08 +00:00
2019-10-02 15:12:34 +00:00
def followers(conn, %{"page" => page}) do
{page, _} = Integer.parse(page)
followers = conn.assigns[:actor].followers
data =
collection_page(conn, followers, page)
|> Map.put("@context", @context)
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
end
def followers(conn, _params) do
%Actor{followers: followers} = conn.assigns[:actor]
data = %{
"@context" => @context,
"type" => "OrderedCollection",
"id" => current_url(conn, %{}),
"totalItems" => length(followers),
"first" => collection_page(conn, followers, 1)
}
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
end
2019-10-02 15:35:19 +00:00
def following(conn, %{"page" => page}) do
actor = conn.assigns[:actor]
following =
Actor.get_all_following(actor)
|> Enum.map(fn actor -> actor.ap_id end)
data =
collection_page(conn, following, page)
|> Map.put("@context", @context)
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
end
def following(conn, _params) do
actor = conn.assigns[:actor]
following =
Actor.get_all_following(actor)
|> Enum.map(fn actor -> actor.ap_id end)
data = %{
"@context" => @context,
"type" => "OrderedCollection",
"id" => current_url(conn, %{}),
"totalItems" => length(following),
"first" => collection_page(conn, following, 1)
}
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
end
2019-10-02 15:12:34 +00:00
defp collection_page(conn, collection, page) do
chunks = Enum.chunk_every(collection, 20)
# page is 1 indexed, so subtract 1 to get the current chunk
current_chunk = Enum.at(chunks, page - 1)
data = %{
"type" => "OrderedCollectionPage",
"totalItems" => length(collection),
"partOf" => current_url(conn, %{}),
"id" => current_url(conn, %{page: page}),
"orderedItems" => current_chunk || []
}
if page < length(chunks) do
Map.put(data, "next", current_url(conn, %{page: page + 1}))
else
data
2019-10-01 02:26:08 +00:00
end
end
end