expanding AddRemoveValidator
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Fri, 19 Mar 2021 14:25:12 +0000 (17:25 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 25 Mar 2021 10:03:42 +0000 (13:03 +0300)
lib/pleroma/web/activity_pub/object_validator.ex
lib/pleroma/web/activity_pub/object_validators/add_remove_validator.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/common_api.ex

index 3ca9136aae43843575e974470fc426a7bca613bb..14c3e853180bb910af1c16fd3a3b2885b5ff9da8 100644 (file)
@@ -238,7 +238,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
   def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
     with {:ok, object} <-
            object
-           |> AddRemoveValidator.cast_and_validate(meta)
+           |> AddRemoveValidator.cast_and_validate()
            |> Ecto.Changeset.apply_action(:insert) do
       object = stringify_keys(object)
       {:ok, object, meta}
index 885282f32817c1907e37f610451e6d3d27fb45a7..c38f86a0ef22cdda5368832721e49a0276c23f65 100644 (file)
@@ -8,6 +8,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AddRemoveValidator do
   import Ecto.Changeset
   import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
 
+  require Pleroma.Constants
+
   alias Pleroma.EctoType.ActivityPub.ObjectValidators
 
   @primary_key false
@@ -22,28 +24,40 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AddRemoveValidator do
     field(:cc, ObjectValidators.Recipients, default: [])
   end
 
-  def cast_and_validate(data, meta) do
+  def cast_and_validate(data) do
     data
+    |> maybe_fix_data_for_mastodon()
     |> cast_data()
-    |> validate_data(meta)
+    |> validate_data()
+  end
+
+  defp maybe_fix_data_for_mastodon(data) do
+    {:ok, actor} = Pleroma.User.get_or_fetch_by_ap_id(data["actor"])
+    # Mastodon sends pin/unpin objects without id, to, cc fields
+    data
+    |> Map.put_new("id", Pleroma.Web.ActivityPub.Utils.generate_activity_id())
+    |> Map.put_new("to", [Pleroma.Constants.as_public()])
+    |> Map.put_new("cc", [actor.follower_address])
   end
 
   defp cast_data(data) do
     cast(%__MODULE__{}, data, __schema__(:fields))
   end
 
-  defp validate_data(changeset, meta) do
+  defp validate_data(changeset) do
     changeset
     |> validate_required([:id, :target, :object, :actor, :type, :to, :cc])
     |> validate_inclusion(:type, ~w(Add Remove))
     |> validate_actor_presence()
-    |> validate_collection_belongs_to_actor(meta)
+    |> validate_collection_belongs_to_actor()
     |> validate_object_presence()
   end
 
-  defp validate_collection_belongs_to_actor(changeset, meta) do
+  defp validate_collection_belongs_to_actor(changeset) do
+    {:ok, actor} = Pleroma.User.get_or_fetch_by_ap_id(changeset.changes[:actor])
+
     validate_change(changeset, :target, fn :target, target ->
-      if target == meta[:featured_address] do
+      if target == actor.featured_address do
         []
       else
         [target: "collection doesn't belong to actor"]
index c4b11a65540ecf923a81768b3cbbaf4cefb2f17a..2172e773681fda8e5628e7bd03ef5c4fd12b05fb 100644 (file)
@@ -557,24 +557,12 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   end
 
   def handle_incoming(%{"type" => type} = data, _options) when type in ~w(Add Remove) do
-    with {:ok, %User{} = user} <- ObjectValidator.fetch_actor(data),
+    with :ok <- ObjectValidator.fetch_actor_and_object(data),
+         {:ok, actor} <- Pleroma.User.get_or_fetch_by_ap_id(data["actor"]),
          # maybe locally user doesn't have featured_address
-         {:ok, user} <- maybe_refetch_user(user),
-         %Object{} <- Object.normalize(data["object"], fetch: true) do
-      # Mastodon sends pin/unpin objects without id, to, cc fields
-      data =
-        data
-        |> Map.put_new("id", Utils.generate_activity_id())
-        |> Map.put_new("to", [Pleroma.Constants.as_public()])
-        |> Map.put_new("cc", [user.follower_address])
-
-      case Pipeline.common_pipeline(data,
-             local: false,
-             featured_address: user.featured_address
-           ) do
-        {:ok, activity, _meta} -> {:ok, activity}
-        error -> error
-      end
+         {:ok, _} <- maybe_refetch_user(actor),
+         {:ok, activity, _meta} <- Pipeline.common_pipeline(data, local: false) do
+      {:ok, activity}
     end
   end
 
index 175d690cca93f66616f9c1919436efc216253193..b36be4d2ae81005b936d107f5cad0000232d7bee 100644 (file)
@@ -421,8 +421,7 @@ defmodule Pleroma.Web.CommonAPI do
          {:ok, _pin, _} <-
            Pipeline.common_pipeline(pin_data,
              local: true,
-             activity_id: id,
-             featured_address: user.featured_address
+             activity_id: id
            ) do
       {:ok, activity}
     else