Object Validators: Extract common validations.
authorlain <lain@soykaf.club>
Tue, 5 Nov 2019 14:02:09 +0000 (15:02 +0100)
committerlain <lain@soykaf.club>
Tue, 5 Nov 2019 14:02:09 +0000 (15:02 +0100)
lib/pleroma/web/activity_pub/object_validator.ex
lib/pleroma/web/activity_pub/object_validators/common_validations.ex [new file with mode: 0644]
lib/pleroma/web/activity_pub/object_validators/like_validator.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
test/web/activity_pub/side_effects_test.exs

index 27a8dd8522152c3d02c7010a73210be311237bfe..539be11437f133b60b04cfa4ec50d8810e7dece3 100644 (file)
@@ -17,9 +17,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
   def validate(object, meta)
 
   def validate(%{"type" => "Like"} = object, meta) do
-    with {_, %{valid?: true, changes: object}} <-
-           {:validate_object, LikeValidator.cast_and_validate(object)} do
-      object = stringify_keys(object)
+    with {_, {:ok, object}} <-
+           {:validate_object,
+            object |> LikeValidator.cast_and_validate() |> Ecto.Changeset.apply_action(:insert)} do
+      object = stringify_keys(object |> Map.from_struct())
       {:ok, object, meta}
     else
       e -> {:error, e}
diff --git a/lib/pleroma/web/activity_pub/object_validators/common_validations.ex b/lib/pleroma/web/activity_pub/object_validators/common_validations.ex
new file mode 100644 (file)
index 0000000..db0e207
--- /dev/null
@@ -0,0 +1,32 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
+  import Ecto.Changeset
+
+  alias Pleroma.Object
+  alias Pleroma.User
+
+  def validate_actor_presence(cng, field_name \\ :actor) do
+    cng
+    |> validate_change(field_name, fn field_name, actor ->
+      if User.get_cached_by_ap_id(actor) do
+        []
+      else
+        [{field_name, "can't find user"}]
+      end
+    end)
+  end
+
+  def validate_object_presence(cng, field_name \\ :object) do
+    cng
+    |> validate_change(field_name, fn field_name, actor ->
+      if Object.get_cached_by_ap_id(actor) do
+        []
+      else
+        [{field_name, "can't find user"}]
+      end
+    end)
+  end
+end
index 5fa486653333f58ed920021e2ed14f8c98676c65..ccbc7d071761557fe449617da78bb679036af04d 100644 (file)
@@ -4,13 +4,13 @@
 
 defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
   use Ecto.Schema
-  import Ecto.Changeset
 
-  alias Pleroma.Object
-  alias Pleroma.User
   alias Pleroma.Web.ActivityPub.ObjectValidators.Types
   alias Pleroma.Web.ActivityPub.Utils
 
+  import Ecto.Changeset
+  import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
+
   @primary_key false
 
   embedded_schema do
@@ -38,8 +38,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
     data_cng
     |> validate_inclusion(:type, ["Like"])
     |> validate_required([:id, :type, :object, :actor, :context, :to, :cc])
-    |> validate_change(:actor, &actor_valid?/2)
-    |> validate_change(:object, &object_valid?/2)
+    |> validate_actor_presence()
+    |> validate_object_presence()
     |> validate_existing_like()
   end
 
@@ -54,20 +54,4 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
   end
 
   def validate_existing_like(cng), do: cng
-
-  def actor_valid?(field_name, actor) do
-    if User.get_cached_by_ap_id(actor) do
-      []
-    else
-      [{field_name, "can't find user"}]
-    end
-  end
-
-  def object_valid?(field_name, object) do
-    if Object.get_cached_by_ap_id(object) do
-      []
-    else
-      [{field_name, "can't find object"}]
-    end
-  end
 end
index 4dd884ce99d8cb5e9c1d3969f7e04957baeaf0d6..9a0c37e13590e404cf13c15132e83a389d00c1cd 100644 (file)
@@ -566,8 +566,11 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   end
 
   def handle_incoming(%{"type" => "Like"} = data, _options) do
-    with {_, %{changes: cast_data}} <- {:casting_data, LikeValidator.cast_data(data)},
-         cast_data <- ObjectValidator.stringify_keys(cast_data),
+    with {_, {:ok, cast_data_sym}} <-
+           {:casting_data,
+            data |> LikeValidator.cast_data() |> Ecto.Changeset.apply_action(:insert)},
+         {_, cast_data} <-
+           {:stringify_keys, ObjectValidator.stringify_keys(cast_data_sym |> Map.from_struct())},
          :ok <- ObjectValidator.fetch_actor_and_object(cast_data),
          {_, {:ok, cast_data}} <- {:maybe_add_context, maybe_add_context_from_object(cast_data)},
          {_, {:ok, cast_data}} <-
index b34e45a7f4c36d9fd1ae51329430025db2e52e61..ef91954ae8dcd4a02d082f95bb6dd70b0c616d06 100644 (file)
@@ -19,7 +19,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
       {:ok, post} = CommonAPI.post(user, %{"status" => "hey"})
 
       {:ok, like_data, _meta} = Builder.like(user, post.object)
-      {:ok, like, _meta} = ActivityPub.persist(like_data, [local: true])
+      {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
 
       %{like: like, user: user}
     end