activitypub: represent relay actor at instance root
authorWilliam Pitcock <nenolod@dereferenced.org>
Mon, 6 Aug 2018 06:11:51 +0000 (06:11 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Mon, 6 Aug 2018 08:22:09 +0000 (08:22 +0000)
lib/pleroma/web/activity_pub/activity_pub_controller.ex
lib/pleroma/web/activity_pub/views/user_view.ex
lib/pleroma/web/router.ex

index d337532d0ef46417a45f863cf84c552f709b6bb0..a858b5df900323aa7816e33e5eddc1ebf4026039 100644 (file)
@@ -107,6 +107,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
     json(conn, "ok")
   end
 
+  def relay(conn, params) do
+    with %User{} = user <- User.get_or_create_instance_user(),
+         {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
+      conn
+      |> put_resp_header("content-type", "application/activity+json")
+      |> json(UserView.render("user.json", %{user: user}))
+    else
+      nil -> {:error, :not_found}
+    end
+  end
+
   def errors(conn, {:error, :not_found}) do
     conn
     |> put_status(404)
index 41bfe5048a886e9e3266620f73abe4e5c1a1af5d..52f09fcd57b9e81a3f72ae816c68f84f5288f3fe 100644 (file)
@@ -9,6 +9,33 @@ defmodule Pleroma.Web.ActivityPub.UserView do
   alias Pleroma.Web.ActivityPub.Utils
   import Ecto.Query
 
+  # the instance itself is not a Person, but instead an Application
+  def render("user.json", %{user: %{nickname: nil} = user}) do
+    {:ok, user} = WebFinger.ensure_keys_present(user)
+    {:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"])
+    public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
+    public_key = :public_key.pem_encode([public_key])
+
+    %{
+      "@context" => "https://www.w3.org/ns/activitystreams",
+      "id" => user.ap_id,
+      "type" => "Application",
+      "inbox" => "#{user.ap_id}/inbox",
+      "name" => "Pleroma",
+      "summary" => "Virtual actor for Pleroma relay",
+      "url" => user.ap_id,
+      "manuallyApprovesFollowers" => false,
+      "publicKey" => %{
+        "id" => "#{user.ap_id}#main-key",
+        "owner" => user.ap_id,
+        "publicKeyPem" => public_key
+      },
+      "endpoints" => %{
+        "sharedInbox" => "#{Pleroma.Web.Endpoint.url()}/inbox"
+      }
+    }
+  end
+
   def render("user.json", %{user: user}) do
     {:ok, user} = WebFinger.ensure_keys_present(user)
     {:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"])
index fc7a947aa30a2f15f18f0c63f42c3ec8b7f48216..1f9bccf552822c50bb2fcfe8944a790524c060a7 100644 (file)
@@ -318,6 +318,12 @@ defmodule Pleroma.Web.Router do
   end
 
   if @federating do
+    scope "/", Pleroma.Web.ActivityPub do
+      # XXX: not really ostatus either
+      pipe_through(:ostatus)
+      get("/", ActivityPubController, :relay)
+    end
+
     scope "/", Pleroma.Web.ActivityPub do
       pipe_through(:activitypub)
       post("/users/:nickname/inbox", ActivityPubController, :inbox)