Create activity when client posts to outbox
authorsxsdv1 <sxsdv1@gmail.com>
Sat, 29 Dec 2018 17:21:45 +0000 (18:21 +0100)
committersxsdv1 <sxsdv1@gmail.com>
Sun, 30 Dec 2018 11:01:39 +0000 (12:01 +0100)
lib/pleroma/web/activity_pub/activity_pub_controller.ex
test/web/activity_pub/activity_pub_controller_test.exs

index 9f083d0a5ba530cd12d018896040263a104f1c41..dca74db739e6356d442fc7891eb886a0475080f4 100644 (file)
@@ -4,11 +4,12 @@
 
 defmodule Pleroma.Web.ActivityPub.ActivityPubController do
   use Pleroma.Web, :controller
-  alias Pleroma.{User, Object}
+  alias Pleroma.{Activity, User, Object}
   alias Pleroma.Web.ActivityPub.{ObjectView, UserView}
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.ActivityPub.Relay
   alias Pleroma.Web.ActivityPub.Utils
+  alias Pleroma.Web.ActivityPub.Transmogrifier
   alias Pleroma.Web.Federator
 
   require Logger
@@ -166,11 +167,33 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
 
   def update_outbox(%{assigns: %{user: user}} = conn, %{"nickname" => nickname} = params) do
     if nickname == user.nickname do
-      Logger.info("update outbox #{inspect(params)}")
-
-      conn
-      |> put_status(:created)
-      |> json("ok!")
+      actor = user.ap_id()
+
+      params =
+        params
+        |> Map.drop(["id"])
+        |> Map.put("actor", actor)
+        |> Transmogrifier.fix_addressing()
+
+      object =
+        params["object"]
+        |> Map.merge(Map.take(params, ["to", "cc"]))
+        |> Map.put("attributedTo", actor)
+        |> Transmogrifier.fix_object()
+
+      with {:ok, %Activity{} = activity} <-
+             ActivityPub.create(%{
+               to: params["to"],
+               actor: user,
+               context: object["context"],
+               object: object,
+               additional: Map.take(params, ["cc"])
+             }) do
+        conn
+        |> put_status(:created)
+        |> put_resp_header("location", activity.data["id"])
+        |> json(%{"id" => activity.data["id"]})
+      end
     else
       conn
       |> put_status(:forbidden)
index 589645dd6a7e673e87d46c3d549ced74e7404676..cb95e0e09fe4f47d4960c8706bb9050915e0ab54 100644 (file)
@@ -178,6 +178,20 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
 
       assert json_response(conn, 403)
     end
+
+    test "it inserts an incoming activity into the database", %{conn: conn} do
+      data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
+      user = insert(:user)
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> put_req_header("content-type", "application/activity+json")
+        |> post("/users/#{user.nickname}/outbox", data)
+
+      result = json_response(conn, 201)
+      assert Activity.get_by_ap_id(result["id"])
+    end
   end
 
   describe "/users/:nickname/followers" do