From 3fcdfb75d03efa27903ba5fdd5c78f2b93a4d55b Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 22 Mar 2018 05:23:05 +0000 Subject: [PATCH] activitypub: add outbox endpoint --- .../activity_pub/activity_pub_controller.ex | 11 ++++ .../web/activity_pub/views/user_view.ex | 51 +++++++++++++++++++ lib/pleroma/web/router.ex | 1 + 3 files changed, 63 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex index 419c3307d..47a8bf5ab 100644 --- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex +++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex @@ -65,6 +65,17 @@ def followers(conn, %{"nickname" => nickname}) do end end + def outbox(conn, %{"nickname" => nickname, "max_id" => max_id}) do + with %User{} = user <- User.get_cached_by_nickname(nickname), + {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do + conn + |> put_resp_header("content-type", "application/activity+json") + |> json(UserView.render("outbox.json", %{user: user, max_id: max_id})) + end + end + + def outbox(conn, %{"nickname" => nickname}) do outbox(conn, %{"nickname" => nickname, "max_id" => nil}) end + # TODO: Ensure that this inbox is a recipient of the message def inbox(%{assigns: %{valid_signature: true}} = conn, params) do Federator.enqueue(:incoming_ap_doc, params) diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 7069c248c..5266dc7be 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -3,6 +3,8 @@ defmodule Pleroma.Web.ActivityPub.UserView do alias Pleroma.Web.Salmon alias Pleroma.Web.WebFinger alias Pleroma.User + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Utils def render("user.json", %{user: user}) do @@ -91,4 +93,53 @@ def render("followers.json", %{user: user}) do } |> Map.merge(Utils.make_json_ld_header()) end + + def render("outbox.json", %{user: user, max_id: max_qid}) do + # XXX: technically note_count is wrong for this, but it's better than nothing + info = User.user_info(user) + + params = %{ + "type" => ["Create", "Announce"], + "actor_id" => user.ap_id, + "whole_db" => true, + "limit" => "10" + } + + if max_qid != nil do + params = Map.put(params, "max_id", max_qid) + end + + activities = ActivityPub.fetch_public_activities(params) + min_id = Enum.at(activities, 0).id + + activities = Enum.reverse(activities) + max_id = Enum.at(activities, 0).id + + collection = Enum.map(activities, fn (act) -> + {:ok, data} = Transmogrifier.prepare_outgoing(act.data) + data + end) + + iri = "#{user.ap_id}/outbox" + page = %{ + "id" => "#{iri}?max_id=#{max_id}", + "type" => "OrderedCollectionPage", + "partOf" => iri, + "totalItems" => info.note_count, + "orderedItems" => collection, + "next" => "#{iri}?max_id=#{min_id-1}", + } + + if max_qid == nil do + %{ + "id" => iri, + "type" => "OrderedCollection", + "totalItems" => info.note_count, + "first" => page + } + |> Map.merge(Utils.make_json_ld_header()) + else + page |> Map.merge(Utils.make_json_ld_header()) + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index e071a469e..ec06e2ffd 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -255,6 +255,7 @@ def user_fetcher(username) do get "/users/:nickname/followers", ActivityPubController, :followers get "/users/:nickname/following", ActivityPubController, :following + get "/users/:nickname/outbox", ActivityPubController, :outbox end if @federating do