Add id to objects, too.
authorRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 20:22:49 +0000 (21:22 +0100)
committerRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 20:22:49 +0000 (21:22 +0100)
lib/pleroma/web/activity_pub/activity_pub.ex
test/web/activity_pub/activity_pub_test.exs

index 0f5a78e8a79d66724416d17abf4f60ab7da4b7b1..e7427affbf58eccbacf75889290b1bfb0b2c8c3d 100644 (file)
@@ -6,6 +6,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   def insert(map) when is_map(map) do
     map = Map.put_new_lazy(map, "id", &generate_activity_id/0)
 
+    map = if map["object"] do
+      object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
+      Map.put(map, "object", object)
+    else
+      map
+    end
+
     Repo.insert(%Activity{data: map})
   end
 
@@ -17,6 +24,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     "https://#{host}/activities/#{Ecto.UUID.generate}"
   end
 
+  def generate_object_id do
+    host =
+      Application.get_env(:pleroma, Pleroma.Web.Endpoint)
+      |> Keyword.fetch!(:url)
+      |> Keyword.fetch!(:host)
+    "https://#{host}/objects/#{Ecto.UUID.generate}"
+  end
+
   def fetch_public_activities(opts \\ %{}) do
     public = ["https://www.w3.org/ns/activitystreams#Public"]
     fetch_activities(public, opts)
index 613c5a04fc3e3e4a2f084f6dd8ee9615abc29e53..6a5138012bb7712e2a7d86c251b91d182324277e 100644 (file)
@@ -24,6 +24,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert activity.data["ok"] == data["ok"]
       assert activity.data["id"] == given_id
     end
+
+    test "adds an id to a given object if it lacks one" do
+      data = %{
+        "object" => %{
+          "ok" => true
+        }
+      }
+
+      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
+      assert is_binary(activity.data["object"]["id"])
+    end
   end
 
   describe "fetch activities for recipients" do