Generate ids for activities.
authorRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 16:56:49 +0000 (17:56 +0100)
committerRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 16:56:49 +0000 (17:56 +0100)
lib/pleroma/web/activity_pub/activity_pub.ex
test/web/activity_pub/activity_pub_test.exs

index 121c46c51c8e952c3c78d1e6c63aa04b6f076616..0f5a78e8a79d66724416d17abf4f60ab7da4b7b1 100644 (file)
@@ -4,9 +4,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   import Ecto.Query
 
   def insert(map) when is_map(map) do
+    map = Map.put_new_lazy(map, "id", &generate_activity_id/0)
+
     Repo.insert(%Activity{data: map})
   end
 
+  def generate_activity_id do
+    host =
+      Application.get_env(:pleroma, Pleroma.Web.Endpoint)
+      |> Keyword.fetch!(:url)
+      |> Keyword.fetch!(:host)
+    "https://#{host}/activities/#{Ecto.UUID.generate}"
+  end
+
   def fetch_public_activities(opts \\ %{}) do
     public = ["https://www.w3.org/ns/activitystreams#Public"]
     fetch_activities(public, opts)
index d220f91c38c7efbd40efd23d2cb367fc55293578..613c5a04fc3e3e4a2f084f6dd8ee9615abc29e53 100644 (file)
@@ -5,17 +5,27 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   alias Pleroma.Builders.ActivityBuilder
 
   describe "insertion" do
-    test "inserts a given map into the activity database" do
+    test "inserts a given map into the activity database, giving it an id if it has none." do
       data = %{
-        ok: true
+        "ok" => true
       }
 
       {:ok, %Activity{} = activity} = ActivityPub.insert(data)
-      assert activity.data == data
+      assert activity.data["ok"] == data["ok"]
+      assert is_binary(activity.data["id"])
+
+      given_id = "bla"
+      data = %{
+        "ok" => true,
+        "id" => given_id
+      }
+
+      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
+      assert activity.data["ok"] == data["ok"]
+      assert activity.data["id"] == given_id
     end
   end
 
-
   describe "fetch activities for recipients" do
     test "retrieve the activities for certain recipients" do
       {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})