nulla/lib/nulla_web/controllers/activitypub/outbox_controller.ex
2025-07-01 10:15:06 +02:00

51 lines
1.3 KiB
Elixir

defmodule NullaWeb.ActivityPub.OutboxController do
use NullaWeb, :controller
alias Nulla.ActivityPub
alias Nulla.Models.Actor
alias Nulla.Models.Note
def outbox(conn, %{"username" => username} = params) do
domain = NullaWeb.Endpoint.host()
actor = Actor.get_actor(preferredUsername: username, domain: domain)
case Map.get(params, "page") do
"true" ->
max_id = params["max_id"] && String.to_integer(params["max_id"])
notes =
if max_id do
Note.get_before_notes(actor.id, max_id)
else
Note.get_latest_notes(actor.id)
end
items = Enum.map(notes, &ActivityPub.activity_note(&1))
next_max_id =
case List.last(notes) do
nil -> 0
last -> last.id
end
min_id =
case List.first(notes) do
nil -> 0
first -> first.id
end
conn
|> put_resp_content_type("application/activity+json")
|> send_resp(
200,
Jason.encode!(ActivityPub.outbox(actor, next_max_id, min_id || 0, items))
)
_ ->
total = Note.get_total_notes_count(actor.id)
conn
|> put_resp_content_type("application/activity+json")
|> send_resp(200, Jason.encode!(ActivityPub.outbox(actor, total)))
end
end
end