transmogrifier: Drop incoming create early if it already exists

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-09-23 11:45:32 +02:00
parent 02f12ec625
commit 5e86a2809e
No known key found for this signature in database
GPG key ID: D5B7A8E43C997DEE
2 changed files with 7 additions and 3 deletions

View file

@ -515,15 +515,19 @@ def handle_incoming(
end end
def handle_incoming( def handle_incoming(
%{"type" => "Create", "object" => %{"type" => objtype}} = data, %{"type" => "Create", "object" => %{"type" => objtype, "id" => obj_id}} = data,
_options _options
) )
when objtype in ~w{Question Answer ChatMessage Audio Video Event Article} do when objtype in ~w{Question Answer ChatMessage Audio Video Event Article} do
data = Map.put(data, "object", strip_internal_fields(data["object"])) data = Map.put(data, "object", strip_internal_fields(data["object"]))
with {:ok, %User{}} <- ObjectValidator.fetch_actor(data), with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
nil <- Activity.get_create_by_object_ap_id(obj_id),
{:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
{:ok, activity} {:ok, activity}
else
%Activity{} = activity -> {:ok, activity}
e -> e
end end
end end

View file

@ -157,12 +157,12 @@ test "Mastodon Question activity with custom emojis" do
} }
end end
test "returns an error if received a second time" do test "returns same activity if received a second time" do
data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!() data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
assert {:error, {:validate_object, {:error, _}}} = Transmogrifier.handle_incoming(data) assert {:ok, ^activity} = Transmogrifier.handle_incoming(data)
end end
test "accepts a Question with no content" do test "accepts a Question with no content" do