Prevent double insertions.
authorRoger Braun <roger@rogerbraun.net>
Sun, 7 May 2017 18:13:10 +0000 (20:13 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sun, 7 May 2017 18:13:10 +0000 (20:13 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
test/web/activity_pub/activity_pub_test.exs

index e25555adf822e1506f0408f19a1b479c33b0e2e1..45a2fbfc351e41b6052518f7edc3e2ee40dcaf16 100644 (file)
@@ -8,15 +8,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> Map.put_new_lazy("id", &generate_activity_id/0)
     |> Map.put_new_lazy("published", &make_date/0)
 
-    map = if is_map(map["object"]) do
-      object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
-      Repo.insert!(%Object{data: object})
-      Map.put(map, "object", object)
-    else
-      map
-    end
+    with %Activity{} = activity <- Activity.get_by_ap_id(map["id"]) do
+      {:ok, activity}
+    else _e ->
+      map = if is_map(map["object"]) do
+        object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
+        Repo.insert!(%Object{data: object})
+        Map.put(map, "object", object)
+      else
+        map
+      end
 
-    Repo.insert(%Activity{data: map, local: local})
+      Repo.insert(%Activity{data: map, local: local})
+    end
   end
 
   def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
index a9a6e13647cca2ac78fa680bbb740f044bf98a2a..2e361ad387a7bbd4b73d37257c5b377e1ecf8f99 100644 (file)
@@ -7,6 +7,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   import Pleroma.Factory
 
   describe "insertion" do
+    test "returns the activity if one with the same id is already in" do
+      activity = insert(:note_activity)
+      {:ok, new_activity}= ActivityPub.insert(activity.data)
+
+      assert activity == new_activity
+    end
+
     test "inserts a given map into the activity database, giving it an id if it has none." do
       data = %{
         "ok" => true