From f05741edb5c66edc47d6c57b130748735e9d2099 Mon Sep 17 00:00:00 2001 From: miraikumiko Date: Sat, 14 Jun 2025 19:27:09 +0200 Subject: [PATCH] Add outbox --- lib/nulla/activitypub.ex | 12 ++++++++++++ lib/nulla/models/note.ex | 5 +++++ lib/nulla_web/controllers/outbox_controller.ex | 18 ++++++++++++++++++ lib/nulla_web/router.ex | 1 + 4 files changed, 36 insertions(+) create mode 100644 lib/nulla_web/controllers/outbox_controller.ex diff --git a/lib/nulla/activitypub.ex b/lib/nulla/activitypub.ex index 019aef8..ec6332e 100644 --- a/lib/nulla/activitypub.ex +++ b/lib/nulla/activitypub.ex @@ -304,4 +304,16 @@ defmodule Nulla.ActivityPub do ) ) end + + @spec outbox(String.t(), String.t(), Integer.t()) :: Jason.OrderedObject.t() + def outbox(domain, username, total) do + Jason.OrderedObject.new( + "@context": "https://www.w3.org/ns/activitystreams", + id: "https://#{domain}/@#{username}/outbox", + type: "OrderedCollection", + totalItems: total, + first: "https://#{domain}/@#{username}/outbox?page=true", + last: "https://#{domain}/@#{username}/outbox?min_id=0&page=true" + ) + end end diff --git a/lib/nulla/models/note.ex b/lib/nulla/models/note.ex index efaf328..83e1eb5 100644 --- a/lib/nulla/models/note.ex +++ b/lib/nulla/models/note.ex @@ -33,4 +33,9 @@ defmodule Nulla.Models.Note do def get_note!(id), do: Repo.get!(Note, id) def get_all_notes!(user_id), do: Repo.all(from n in Note, where: n.user_id == ^user_id) + + def get_total_notes_count(user_id) do + from(n in Note, where: n.user_id == ^user_id) + |> Repo.aggregate(:count, :id) + end end diff --git a/lib/nulla_web/controllers/outbox_controller.ex b/lib/nulla_web/controllers/outbox_controller.ex new file mode 100644 index 0000000..55d13da --- /dev/null +++ b/lib/nulla_web/controllers/outbox_controller.ex @@ -0,0 +1,18 @@ +defmodule NullaWeb.OutboxController do + use NullaWeb, :controller + alias Nulla.ActivityPub + alias Nulla.Models.User + alias Nulla.Models.Note + alias Nulla.Models.InstanceSettings + + def show(conn, %{"username" => username}) do + instance_settings = InstanceSettings.get_instance_settings!() + domain = instance_settings.domain + user = User.get_user_by_username!(username) + total = Note.get_total_notes_count(user.id) + + conn + |> put_resp_content_type("application/activity+json") + |> send_resp(200, Jason.encode!(ActivityPub.outbox(domain, username, total))) + end +end diff --git a/lib/nulla_web/router.ex b/lib/nulla_web/router.ex index ac27911..6e50c78 100644 --- a/lib/nulla_web/router.ex +++ b/lib/nulla_web/router.ex @@ -22,6 +22,7 @@ defmodule NullaWeb.Router do get "/nodeinfo/2.0", NodeinfoController, :show get "/@:username", UserController, :show + get "/@:username/outbox", OutboxController, :show get "/@:username/following", FollowController, :following get "/@:username/followers", FollowController, :followers get "/@:username/:note_id", NoteController, :show