Add user outboxes

This commit is contained in:
Shadowfacts 2019-10-02 19:13:52 -04:00
parent 13d7f832fe
commit a12ba25f18
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 103 additions and 0 deletions

26
lib/clacks/paginator.ex Normal file
View File

@ -0,0 +1,26 @@
defmodule Clacks.Paginator do
import Ecto.Query
def paginate(query, %{"max_id" => max_id}) do
query
|> where([a], a.id < ^max_id)
|> order_by(desc: :id)
end
def paginate(query, %{"since_id" => since_id}) do
query
|> where([a], a.id > ^since_id)
|> order_by(desc: :id)
end
def paginate(query, %{"min_id" => min_id}) do
query
|> where([a], a.id > ^min_id)
|> order_by(asc: :id)
end
def paginate(query, _params) do
query
|> order_by(desc: :id)
end
end

View File

@ -0,0 +1,76 @@
defmodule ClacksWeb.OutboxController do
use ClacksWeb, :controller
alias Clacks.{Repo, Actor, Activity}
import Ecto.Query
@context "https://www.w3.org/ns/activitystreams"
@outbox_types ["Create", "Announce"]
plug :get_actor
defp get_actor(%Plug.Conn{path_params: %{"nickname" => nickname}} = conn, _opts) do
case Actor.get_by_nickname(nickname) do
nil ->
conn
|> put_status(404)
%Actor{local: false} ->
conn
|> put_status(404)
actor ->
assign(conn, :actor, actor)
end
end
def outbox(conn, params) when params == %{} do
actor = conn.assigns[:actor]
activities = Repo.all(outbox_query(params, actor))
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]
activities = Repo.all(outbox_query(params, actor))
data =
outbox_page(conn, params, activities)
|> Map.put("@context", @context)
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(data)
end
defp outbox_query(params, %Actor{ap_id: ap_id}) do
Activity
|> where([a], a.actor == ^ap_id)
|> where([a], fragment("?->>'type'", a.data) in @outbox_types)
|> Clacks.Paginator.paginate(params)
|> limit(^Map.get(params, "limit", 20))
end
defp outbox_page(conn, pagination_params, activities) do
last_id = List.last(activities).id
%{
"type" => "OrderedColletionPage",
"partOf" => current_url(conn, %{}),
"id" => current_url(conn, pagination_params),
"next" => current_url(conn, %{max_id: last_id}),
"orderedItems" => Enum.map(activities, & &1.data)
}
end
end

View File

@ -39,6 +39,7 @@ defmodule ClacksWeb.Router do
get "/users/:nickname", ActorController, :get
get "/users/:nickname/followers", ActorController, :followers
get "/users/:nickname/following", ActorController, :following
get "/users/:nickname/outbox", OutboxController, :outbox
post "/inbox", InboxController, :shared
post "/users/:nickname/inbox", InboxController, :user_specific