Pipeline: Unify, refactor, DRY.
authorlain <lain@soykaf.club>
Tue, 28 Apr 2020 15:29:54 +0000 (17:29 +0200)
committerlain <lain@soykaf.club>
Tue, 28 Apr 2020 15:29:54 +0000 (17:29 +0200)
lib/pleroma/web/activity_pub/builder.ex
lib/pleroma/web/activity_pub/object_validator.ex
lib/pleroma/web/activity_pub/transmogrifier/chat_message_handling.ex
lib/pleroma/web/common_api/common_api.ex
test/web/activity_pub/object_validator_test.exs

index 7576ed2782f689c2a05bcdc523f3bd77f50f6646..7f9c071b33727abb852038a918f13d809ff8498a 100644 (file)
@@ -11,13 +11,13 @@ defmodule Pleroma.Web.ActivityPub.Builder do
   alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.Web.ActivityPub.Visibility
 
-  def create(actor, object_id, recipients) do
+  def create(actor, object, recipients) do
     {:ok,
      %{
        "id" => Utils.generate_activity_id(),
        "actor" => actor.ap_id,
        "to" => recipients,
-       "object" => object_id,
+       "object" => object,
        "type" => "Create",
        "published" => DateTime.utc_now() |> DateTime.to_iso8601()
      }, []}
index a4da9242a03528e4485c949b5ada495fd155e590..bada3509dfa82a25c677fdf0070c10acde722939 100644 (file)
@@ -38,16 +38,24 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
     end
   end
 
-  def validate(%{"type" => "Create"} = object, meta) do
-    with {:ok, object} <-
-           object
+  def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
+    with {:ok, object_data} <- cast_and_apply(object),
+         meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
+         {:ok, create_activity} <-
+           create_activity
            |> CreateChatMessageValidator.cast_and_validate(meta)
            |> Ecto.Changeset.apply_action(:insert) do
-      object = stringify_keys(object)
-      {:ok, object, meta}
+      create_activity = stringify_keys(create_activity)
+      {:ok, create_activity, meta}
     end
   end
 
+  def cast_and_apply(%{"type" => "ChatMessage"} = object) do
+    ChatMessageValidator.cast_and_apply(object)
+  end
+
+  def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
+
   def stringify_keys(%{__struct__: _} = object) do
     object
     |> Map.from_struct()
index 043d847d1ce4bdcc5c7ca33955643e2ee3cb7d57..d9c36e3136b679862cc0942e91ada1a700f56f9f 100644 (file)
@@ -4,9 +4,6 @@
 
 defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageHandling do
   alias Pleroma.Repo
-  alias Pleroma.Web.ActivityPub.ObjectValidator
-  alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
-  alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
   alias Pleroma.Web.ActivityPub.Pipeline
 
   def handle_incoming(
@@ -15,30 +12,14 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageHandling do
       ) do
     # Create has to be run inside a transaction because the object is created as a side effect.
     # If this does not work, we need to roll back creating the activity.
-    case Repo.transaction(fn -> do_handle_incoming(data) end) do
-      {:ok, value} ->
-        value
+    case Repo.transaction(fn -> Pipeline.common_pipeline(data, local: false) end) do
+      {:ok, {:ok, activity, _}} ->
+        {:ok, activity}
 
-      {:error, e} ->
-        {:error, e}
-    end
-  end
+      {:ok, e} ->
+        e
 
-  def do_handle_incoming(
-        %{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object_data} = data
-      ) do
-    with {_, {:ok, cast_data_sym}} <-
-           {:casting_data, data |> CreateChatMessageValidator.cast_and_apply()},
-         cast_data = ObjectValidator.stringify_keys(cast_data_sym),
-         {_, {:ok, object_cast_data_sym}} <-
-           {:casting_object_data, object_data |> ChatMessageValidator.cast_and_apply()},
-         object_cast_data = ObjectValidator.stringify_keys(object_cast_data_sym),
-         {_, {:ok, activity, _meta}} <-
-           {:common_pipeline,
-            Pipeline.common_pipeline(cast_data, local: false, object_data: object_cast_data)} do
-      {:ok, activity}
-    else
-      e ->
+      {:error, e} ->
         {:error, e}
     end
   end
index c39d1cee695da77519bac9a001f12c8972d46a3c..ef86ec1e4c88b199590481314e473d57c78b589c 100644 (file)
@@ -40,12 +40,11 @@ defmodule Pleroma.Web.CommonAPI do
                 )},
              {_, {:ok, create_activity_data, _meta}} <-
                {:build_create_activity,
-                Builder.create(user, chat_message_data["id"], [recipient.ap_id])},
+                Builder.create(user, chat_message_data, [recipient.ap_id])},
              {_, {:ok, %Activity{} = activity, _meta}} <-
                {:common_pipeline,
                 Pipeline.common_pipeline(create_activity_data,
-                  local: true,
-                  object_data: chat_message_data
+                  local: true
                 )} do
           {:ok, activity}
         else
index baa4b2585bfb94bcb10adf7f5da1f43bdd555d28..41f67964a7d2cc928fc9ffb7d0a54ed25a367e13 100644 (file)
@@ -17,7 +17,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
       {: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])
+      {:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
 
       {:error, cng} = ObjectValidator.validate(create_data, [])