From db08498a9ff9166afc5cf4df1afe55e96a74b4a3 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 7 May 2017 20:13:10 +0200 Subject: [PATCH] Prevent double insertions. --- lib/pleroma/web/activity_pub/activity_pub.ex | 20 ++++++++++++-------- test/web/activity_pub/activity_pub_test.exs | 7 +++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index e25555adf..45a2fbfc3 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -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 diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index a9a6e1364..2e361ad38 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -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 -- 2.45.2