CreateChatMessageValidator: Validate object existence
authorlain <lain@soykaf.club>
Tue, 28 Apr 2020 14:45:28 +0000 (16:45 +0200)
committerlain <lain@soykaf.club>
Tue, 28 Apr 2020 14:45:28 +0000 (16:45 +0200)
lib/pleroma/web/activity_pub/object_validators/create_chat_message_validator.ex
test/web/activity_pub/object_validator_test.exs

index 21c7a5ba43b1edadeb533228e738c7724b6fe921..dfc91bf718a4cab9424343263f5249210551c127 100644 (file)
@@ -5,10 +5,10 @@
 # NOTES
 # - Can probably be a generic create validator
 # - doesn't embed, will only get the object id
-# - object has to be validated first, maybe with some meta info from the surrounding create
 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator do
   use Ecto.Schema
 
+  alias Pleroma.Object
   alias Pleroma.Web.ActivityPub.ObjectValidators.Types
 
   import Ecto.Changeset
@@ -43,6 +43,18 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator do
     |> validate_required([:id, :actor, :to, :type, :object])
     |> validate_inclusion(:type, ["Create"])
     |> validate_recipients_match(meta)
+    |> validate_object_nonexistence()
+  end
+
+  def validate_object_nonexistence(cng) do
+    cng
+    |> validate_change(:object, fn :object, object_id ->
+      if Object.get_cached_by_ap_id(object_id) do
+        [{:object, "The object to create already exists"}]
+      else
+        []
+      end
+    end)
   end
 
   def validate_recipients_match(cng, meta) do
index bc2317e557b2169eeec1ca2391c2dce601b93339..baa4b2585bfb94bcb10adf7f5da1f43bdd555d28 100644 (file)
@@ -1,6 +1,7 @@
 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
   use Pleroma.DataCase
 
+  alias Pleroma.Object
   alias Pleroma.Web.ActivityPub.Builder
   alias Pleroma.Web.ActivityPub.ObjectValidator
   alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
@@ -9,6 +10,21 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
 
   import Pleroma.Factory
 
+  describe "chat message create activities" do
+    test "it is invalid if the object already exists" do
+      user = insert(:user)
+      recipient = insert(:user)
+      {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
+      object = Object.normalize(activity, false)
+
+      {:ok, create_data, _} = Builder.create(user, object.data["id"], [recipient.ap_id])
+
+      {:error, cng} = ObjectValidator.validate(create_data, [])
+
+      assert {:object, {"The object to create already exists", []}} in cng.errors
+    end
+  end
+
   describe "chat messages" do
     setup do
       clear_config([:instance, :remote_limit])