Browse Source

Add AP following endpoint

master
Shadowfacts 2 years ago
parent
commit
81712fb1bf
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
3 changed files with 42 additions and 1 deletions
  1. +5
    -0
      lib/clacks/actor.ex
  2. +36
    -1
      lib/clacks_web/controllers/actor_controller.ex
  3. +1
    -0
      lib/clacks_web/router.ex

+ 5
- 0
lib/clacks/actor.ex View File

@ -74,4 +74,9 @@ defmodule Clacks.Actor do
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

+ 36
- 1
lib/clacks_web/controllers/actor_controller.ex View File

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


+ 1
- 0
lib/clacks_web/router.ex View File

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


Loading…
Cancel
Save