Prevent double insertions.

This commit is contained in:
Roger Braun 2017-05-07 20:13:10 +02:00
parent 4f9328c622
commit db08498a9f
2 changed files with 19 additions and 8 deletions

View File

@ -8,6 +8,9 @@ def insert(map, local \\ true) when is_map(map) do
|> Map.put_new_lazy("id", &generate_activity_id/0)
|> Map.put_new_lazy("published", &make_date/0)
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})
@ -18,6 +21,7 @@ def insert(map, local \\ true) when is_map(map) do
Repo.insert(%Activity{data: map, local: local})
end
end
def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
published = published || make_date()

View 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