clacks/lib/clacks_web/controllers/outbox_controller.ex

73 lines
1.7 KiB
Elixir
Raw Normal View History

2019-10-02 23:13:52 +00:00
defmodule ClacksWeb.OutboxController do
use ClacksWeb, :controller
2020-04-27 02:57:47 +00:00
alias Clacks.{Actor, Timeline}
2019-10-02 23:13:52 +00:00
@context "https://www.w3.org/ns/activitystreams"
plug :get_actor
2020-04-22 01:20:51 +00:00
defp get_actor(%Plug.Conn{path_params: %{"username" => username}} = conn, _opts) do
case Actor.get_by_nickname(username) do
%Actor{local: true} = actor ->
assign(conn, :actor, actor)
2019-10-02 23:13:52 +00:00
2020-04-22 01:20:51 +00:00
_ ->
2019-10-02 23:13:52 +00:00
conn
|> put_status(404)
2020-04-22 01:20:51 +00:00
|> json(%{error: "Not Found"})
2019-10-02 23:13:52 +00:00
end
end
2020-04-27 02:57:47 +00:00
def outbox(conn, %{} = params) do
2019-10-02 23:13:52 +00:00
actor = conn.assigns[:actor]
2020-04-27 02:57:47 +00:00
activities = Timeline.actor_timeline(actor, params, true)
2019-10-02 23:13:52 +00:00
data = %{
"@context" => @context,
"type" => "OrderedCollection",
"id" => current_url(conn, %{}),
"first" => outbox_page(conn, params, activities)
}
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
end
def outbox(conn, params) do
actor = conn.assigns[:actor]
2020-04-27 02:57:47 +00:00
activities = Timeline.actor_timeline(actor, params, true)
2019-10-02 23:13:52 +00:00
data =
outbox_page(conn, params, activities)
|> Map.put("@context", @context)
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
end
defp outbox_page(conn, pagination_params, activities) do
next =
case activities do
[] ->
# todo: is nil the right thing here?
nil
_ ->
last_id = List.last(activities).id
current_url(conn, %{max_id: last_id})
end
2019-10-02 23:13:52 +00:00
%{
"type" => "OrderedColletionPage",
"partOf" => current_url(conn, %{}),
"id" => current_url(conn, pagination_params),
"next" => next,
2019-10-02 23:13:52 +00:00
"orderedItems" => Enum.map(activities, & &1.data)
}
end
end