{Answer,Question}Validator: Keep both actor and attributedTo for now but sync them
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Thu, 25 Jun 2020 22:07:43 +0000 (00:07 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Wed, 15 Jul 2020 10:32:42 +0000 (12:32 +0200)
lib/pleroma/web/activity_pub/builder.ex
lib/pleroma/web/activity_pub/object_validators/answer_validator.ex
lib/pleroma/web/activity_pub/object_validators/common_validations.ex
lib/pleroma/web/activity_pub/object_validators/create_generic_validator.ex
lib/pleroma/web/activity_pub/object_validators/question_validator.ex
lib/pleroma/web/activity_pub/transmogrifier.ex

index e973819546e0d10589273effa7b1ab437b0c6aac..49ce5a9383f9af685a7aa826fb32d2d9610d36d5 100644 (file)
@@ -120,6 +120,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
      %{
        "type" => "Answer",
        "actor" => user.ap_id,
+       "attributedTo" => user.ap_id,
        "cc" => [object.data["actor"]],
        "to" => [],
        "name" => name,
index 9861eec7f71ad03419e9a61f6136b70b13066f50..ebddd50381051768bef640f92cd58771b43a4a15 100644 (file)
@@ -26,6 +26,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do
     field(:name, :string)
     field(:inReplyTo, :string)
     field(:attributedTo, ObjectValidators.ObjectID)
+
+    # TODO: Remove actor on objects
     field(:actor, ObjectValidators.ObjectID)
   end
 
@@ -54,8 +56,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do
   def validate_data(data_cng) do
     data_cng
     |> validate_inclusion(:type, ["Answer"])
-    |> validate_required([:id, :inReplyTo, :name])
+    |> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
     |> CommonValidations.validate_any_presence([:cc, :to])
-    |> CommonValidations.validate_actor_presence()
+    |> CommonValidations.validate_fields_match([:actor, :attributedTo])
+    |> CommonValidations.validate_actor_is_active()
+    |> CommonValidations.validate_host_match()
   end
 end
index 140555a45e6ab8706f76058eeaeddbd2f636dd86..e981dacaa8e166585f1b095dce992d3eae3e7e95 100644 (file)
@@ -115,4 +115,22 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
       end)
     end
   end
+
+  def validate_fields_match(cng, fields) do
+    unique_fields =
+      fields
+      |> Enum.map(fn field -> get_field(cng, field) end)
+      |> Enum.uniq()
+      |> Enum.count()
+
+    if unique_fields == 1 do
+      cng
+    else
+      fields
+      |> Enum.reduce(cng, fn field, cng ->
+        cng
+        |> add_error(field, "Fields #{inspect(fields)} aren't matching")
+      end)
+    end
+  end
 end
index 97e2def10aea1b815edc3f80fe01a2af02e29d6d..54ea14f89dd8e9edee81158b008e400c2194fab1 100644 (file)
@@ -87,14 +87,14 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do
   end
 
   def validate_actors_match(cng, meta) do
-    object_actor = meta[:object_data]["actor"]
+    attributed_to = meta[:object_data]["attributedTo"] || meta[:object_data]["actor"]
 
     cng
     |> validate_change(:actor, fn :actor, actor ->
-      if actor == object_actor do
+      if actor == attributed_to do
         []
       else
-        [{:actor, "Actor doesn't match with object actor"}]
+        [{:actor, "Actor doesn't match with object attributedTo"}]
       end
     end)
   end
index 53cf35d40facea40e057b69c6a65a6a7d039f1e2..466b3e6c2e3ee839222a33c4717d337d9649076d 100644 (file)
@@ -28,7 +28,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
     field(:type, :string)
     field(:content, :string)
     field(:context, :string)
+
+    # TODO: Remove actor on objects
     field(:actor, ObjectValidators.ObjectID)
+
     field(:attributedTo, ObjectValidators.ObjectID)
     field(:summary, :string)
     field(:published, ObjectValidators.DateTime)
@@ -108,8 +111,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
   def validate_data(data_cng) do
     data_cng
     |> validate_inclusion(:type, ["Question"])
-    |> validate_required([:id, :actor, :type, :content, :context])
+    |> validate_required([:id, :actor, :attributedTo, :type, :content, :context])
     |> CommonValidations.validate_any_presence([:cc, :to])
+    |> CommonValidations.validate_fields_match([:actor, :attributedTo])
     |> CommonValidations.validate_actor_is_active()
     |> CommonValidations.validate_any_presence([:oneOf, :anyOf])
     |> CommonValidations.validate_host_match()
index 0ad982720d046bd125caded6768312da7a4afc79..6ab8a52c139cb617ed80910a14ef11596c487f25 100644 (file)
@@ -157,7 +157,12 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   end
 
   def fix_actor(%{"attributedTo" => actor} = object) do
-    Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
+    actor = Containment.get_actor(%{"actor" => actor})
+
+    # TODO: Remove actor field for Objects
+    object
+    |> Map.put("actor", actor)
+    |> Map.put("attributedTo", actor)
   end
 
   def fix_in_reply_to(object, options \\ [])