Transmogrifier: Fetch missing actors for chatmessages.
[akkoma] / lib / pleroma / web / activity_pub / object_validators / create_chat_message_validator.ex
index 21c7a5ba43b1edadeb533228e738c7724b6fe921..88e90318285151be0aa5395fbb872a2fd8988b2d 100644 (file)
@@ -5,13 +5,14 @@
 # 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
+  import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
 
   @primary_key false
 
@@ -42,7 +43,20 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator do
     cng
     |> validate_required([:id, :actor, :to, :type, :object])
     |> validate_inclusion(:type, ["Create"])
+    |> validate_actor_presence()
     |> 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