activitypub: add outbox endpoint
authorWilliam Pitcock <nenolod@dereferenced.org>
Thu, 22 Mar 2018 05:23:05 +0000 (05:23 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Thu, 22 Mar 2018 05:26:54 +0000 (00:26 -0500)
lib/pleroma/web/activity_pub/activity_pub_controller.ex
lib/pleroma/web/activity_pub/views/user_view.ex
lib/pleroma/web/router.ex

index 419c3307dfe23ecec72a2168c39f1f34927932b2..47a8bf5ab4411e296d0358249008c3acbfb24860 100644 (file)
@@ -65,6 +65,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController 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)
index 7069c248c9df2c9b62f616ecfb9ffb9f3f864929..5266dc7bec3636d7f7d0f946052ca206f05e0a89 100644 (file)
@@ -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 @@ defmodule Pleroma.Web.ActivityPub.UserView 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
index e071a469ed5411dbdf75814927d1f3941657b8a2..ec06e2ffd64b8becfc14661bbafc20c6a5e62550 100644 (file)
@@ -255,6 +255,7 @@ defmodule Pleroma.Web.Router do
 
     get "/users/:nickname/followers", ActivityPubController, :followers
     get "/users/:nickname/following", ActivityPubController, :following
+    get "/users/:nickname/outbox", ActivityPubController, :outbox
   end
 
   if @federating do