LikeValidator: Fix up missing recipients.
authorlain <lain@soykaf.club>
Mon, 4 May 2020 15:08:31 +0000 (17:08 +0200)
committerlain <lain@soykaf.club>
Mon, 4 May 2020 15:08:31 +0000 (17:08 +0200)
lib/pleroma/web/activity_pub/object_validators/like_validator.ex
test/web/activity_pub/object_validator_test.exs

index 49546ceaaa22c33d331e5c5f9cb9818fa147da1c..d9ee079957afa7fb4928fe800b6e6658d7e3b55c 100644 (file)
@@ -5,6 +5,7 @@
 defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
   use Ecto.Schema
 
+  alias Pleroma.Object
   alias Pleroma.Web.ActivityPub.ObjectValidators.Types
   alias Pleroma.Web.ActivityPub.Utils
 
@@ -19,8 +20,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
     field(:object, Types.ObjectID)
     field(:actor, Types.ObjectID)
     field(:context, :string)
-    field(:to, {:array, :string})
-    field(:cc, {:array, :string})
+    field(:to, {:array, :string}, default: [])
+    field(:cc, {:array, :string}, default: [])
   end
 
   def cast_and_validate(data) do
@@ -31,7 +32,34 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
 
   def cast_data(data) do
     %__MODULE__{}
-    |> cast(data, [:id, :type, :object, :actor, :context, :to, :cc])
+    |> changeset(data)
+  end
+
+  def changeset(struct, data) do
+    struct
+    |> cast(data, __schema__(:fields))
+    |> fix_after_cast()
+  end
+
+  def fix_after_cast(cng) do
+    cng
+    |> fix_recipients()
+  end
+
+  def fix_recipients(cng) do
+    to = get_field(cng, :to) || []
+    cc = get_field(cng, :cc) || []
+    object = get_field(cng, :object)
+
+    with {[], []} <- {to, cc},
+         %Object{data: %{"actor" => actor}} <- Object.get_cached_by_ap_id(object),
+         {:ok, actor} <- Types.ObjectID.cast(actor) do
+      cng
+      |> put_change(:to, [actor])
+    else
+      _ ->
+        cng
+    end
   end
 
   def validate_data(data_cng) do
index 3c5c3696e299c14e08febf8d52caf83f26ca58b6..9e9e41c6be58afa8614fc1e1b7234aa6538f5cb6 100644 (file)
@@ -36,6 +36,19 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
       assert LikeValidator.cast_and_validate(valid_like).valid?
     end
 
+    test "sets the 'to' field to the object actor if no recipients are given", %{
+      valid_like: valid_like,
+      user: user
+    } do
+      without_recipients =
+        valid_like
+        |> Map.delete("to")
+
+      {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
+
+      assert object["to"] == [user.ap_id]
+    end
+
     test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
       without_actor = Map.delete(valid_like, "actor")