Add AP following endpoint

This commit is contained in:
Shadowfacts 2019-10-02 11:35:19 -04:00
parent 7c03d2627f
commit 81712fb1bf
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 42 additions and 1 deletions

View File

@ -74,4 +74,9 @@ defmodule Clacks.Actor do
end end
end end
end end
@spec get_all_following(actor :: t()) :: [t()]
def get_all_following(actor) do
Repo.all(from a in __MODULE__, where: ^actor.ap_id in a.followers)
end
end end

View File

@ -1,7 +1,6 @@
defmodule ClacksWeb.ActorController do defmodule ClacksWeb.ActorController do
use ClacksWeb, :controller use ClacksWeb, :controller
alias Clacks.Actor alias Clacks.Actor
import Ecto.Query
@context "https://www.w3.org/ns/activitystreams" @context "https://www.w3.org/ns/activitystreams"
@ -64,6 +63,42 @@ defmodule ClacksWeb.ActorController do
|> json(data) |> json(data)
end end
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
defp collection_page(conn, collection, page) do defp collection_page(conn, collection, page) do
chunks = Enum.chunk_every(collection, 20) chunks = Enum.chunk_every(collection, 20)
# page is 1 indexed, so subtract 1 to get the current chunk # page is 1 indexed, so subtract 1 to get the current chunk

View File

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