Transmogrifier: Move Rejects to the Pipeline
authorlain <lain@soykaf.club>
Wed, 12 Aug 2020 12:48:51 +0000 (14:48 +0200)
committerlain <lain@soykaf.club>
Wed, 12 Aug 2020 12:48:51 +0000 (14:48 +0200)
lib/pleroma/web/activity_pub/builder.ex
lib/pleroma/web/activity_pub/object_validator.ex
lib/pleroma/web/activity_pub/object_validators/accept_reject_validator.ex [moved from lib/pleroma/web/activity_pub/object_validators/accept_validator.ex with 82% similarity]
lib/pleroma/web/activity_pub/side_effects.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
test/web/activity_pub/object_validators/reject_validation_test.exs [new file with mode: 0644]
test/web/activity_pub/transmogrifier/reject_handling_test.exs

index e1f88e6cc926c7b0ab864663ec25173eee66faf6..f2392ce79ba832036fdf5dc148a5c5d0d1e29b9b 100644 (file)
@@ -14,19 +14,28 @@ defmodule Pleroma.Web.ActivityPub.Builder do
 
   require Pleroma.Constants
 
-  @spec accept(User.t(), Activity.t()) :: {:ok, map(), keyword()}
-  def accept(actor, accepted_activity) do
+  def accept_or_reject(actor, activity, type) do
     data = %{
       "id" => Utils.generate_activity_id(),
       "actor" => actor.ap_id,
-      "type" => "Accept",
-      "object" => accepted_activity.data["id"],
-      "to" => [accepted_activity.actor]
+      "type" => type,
+      "object" => activity.data["id"],
+      "to" => [activity.actor]
     }
 
     {:ok, data, []}
   end
 
+  @spec reject(User.t(), Activity.t()) :: {:ok, map(), keyword()}
+  def reject(actor, rejected_activity) do
+    accept_or_reject(actor, rejected_activity, "Reject")
+  end
+
+  @spec accept(User.t(), Activity.t()) :: {:ok, map(), keyword()}
+  def accept(actor, accepted_activity) do
+    accept_or_reject(actor, accepted_activity, "Accept")
+  end
+
   @spec follow(User.t(), User.t()) :: {:ok, map(), keyword()}
   def follow(follower, followed) do
     data = %{
index d9dd2bc309cdd44ff06c677133b93f234d390638..3f1dffe2b3700ccf82ff6c47a2ffd5b28e5ce486 100644 (file)
@@ -13,7 +13,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
   alias Pleroma.EctoType.ActivityPub.ObjectValidators
   alias Pleroma.Object
   alias Pleroma.User
-  alias Pleroma.Web.ActivityPub.ObjectValidators.AcceptValidator
+  alias Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator
   alias Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator
   alias Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator
   alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
@@ -31,10 +31,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
   @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
   def validate(object, meta)
 
-  def validate(%{"type" => "Accept"} = object, meta) do
+  def validate(%{"type" => type} = object, meta)
+      when type in ~w[Accept Reject] do
     with {:ok, object} <-
            object
-           |> AcceptValidator.cast_and_validate()
+           |> AcceptRejectValidator.cast_and_validate()
            |> Ecto.Changeset.apply_action(:insert) do
       object = stringify_keys(object)
       {:ok, object, meta}
similarity index 82%
rename from lib/pleroma/web/activity_pub/object_validators/accept_validator.ex
rename to lib/pleroma/web/activity_pub/object_validators/accept_reject_validator.ex
index fd75f4b6e433b74537dfc48f93fee20f4e12d882..179beda581df0a4cd224e9d7228e0f2d65893fc5 100644 (file)
@@ -2,7 +2,7 @@
 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
-defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptValidator do
+defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator do
   use Ecto.Schema
 
   alias Pleroma.Activity
@@ -30,10 +30,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptValidator do
   def validate_data(cng) do
     cng
     |> validate_required([:id, :type, :actor, :to, :cc, :object])
-    |> validate_inclusion(:type, ["Accept"])
+    |> validate_inclusion(:type, ["Accept", "Reject"])
     |> validate_actor_presence()
     |> validate_object_presence(allowed_types: ["Follow"])
-    |> validate_accept_rights()
+    |> validate_accept_reject_rights()
   end
 
   def cast_and_validate(data) do
@@ -42,7 +42,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptValidator do
     |> validate_data
   end
 
-  def validate_accept_rights(cng) do
+  def validate_accept_reject_rights(cng) do
     with object_id when is_binary(object_id) <- get_field(cng, :object),
          %Activity{data: %{"object" => followed_actor}} <- Activity.get_by_ap_id(object_id),
          true <- followed_actor == get_field(cng, :actor) do
@@ -50,7 +50,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptValidator do
     else
       _e ->
         cng
-        |> add_error(:actor, "can't accept the given activity")
+        |> add_error(:actor, "can't accept or reject the given activity")
     end
   end
 end
index e1fa75e1c49308bec166e6abe7d03c028c91a044..a4ad12d5372f494a537e90c6b21fcff6f25c74de 100644 (file)
@@ -52,6 +52,30 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
     {:ok, object, meta}
   end
 
+  # Task this handles
+  # - Rejects all existing follow activities for this person
+  # - Updates the follow state
+  def handle(
+        %{
+          data: %{
+            "actor" => actor,
+            "type" => "Reject",
+            "object" => follow_activity_id
+          }
+        } = object,
+        meta
+      ) do
+    with %Activity{actor: follower_id} = follow_activity <-
+           Activity.get_by_ap_id(follow_activity_id),
+         %User{} = followed <- User.get_cached_by_ap_id(actor),
+         %User{} = follower <- User.get_cached_by_ap_id(follower_id),
+         {:ok, _follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject") do
+      FollowingRelationship.update(follower, followed, :follow_reject)
+    end
+
+    {:ok, object, meta}
+  end
+
   # Tasks this handle
   # - Follows if possible
   # - Sends a notification
index 24da1ef9c453df69e3749a6afc7d5aea5304b6c1..544f3f3b67c880ba2e4cb972a15edf704de7f91a 100644 (file)
@@ -9,7 +9,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   alias Pleroma.Activity
   alias Pleroma.EarmarkRenderer
   alias Pleroma.EctoType.ActivityPub.ObjectValidators
-  alias Pleroma.FollowingRelationship
   alias Pleroma.Maps
   alias Pleroma.Object
   alias Pleroma.Object.Containment
@@ -390,16 +389,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
 
   defp fix_content(object), do: object
 
-  defp get_follow_activity(follow_object, _followed) do
-    with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
-         {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
-      {:ok, activity}
-    else
-      _ ->
-        {:error, nil}
-    end
-  end
-
   # Reduce the object list to find the reported user.
   defp get_reported(objects) do
     Enum.reduce_while(objects, nil, fn ap_id, _ ->
@@ -534,31 +523,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     end
   end
 
-  def handle_incoming(
-        %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
-        _options
-      ) do
-    with actor <- Containment.get_actor(data),
-         {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
-         {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
-         {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
-         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
-         {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_reject),
-         {:ok, activity} <-
-           ActivityPub.reject(%{
-             to: follow_activity.data["to"],
-             type: "Reject",
-             actor: followed,
-             object: follow_activity.data["id"],
-             local: false,
-             activity_id: id
-           }) do
-      {:ok, activity}
-    else
-      _e -> :error
-    end
-  end
-
   @misskey_reactions %{
     "like" => "👍",
     "love" => "❤️",
@@ -613,7 +577,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
         %{"type" => type} = data,
         _options
       )
-      when type in ~w{Update Block Follow Accept} do
+      when type in ~w{Update Block Follow Accept Reject} do
     with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
          {:ok, activity, _} <-
            Pipeline.common_pipeline(data, local: false) do
diff --git a/test/web/activity_pub/object_validators/reject_validation_test.exs b/test/web/activity_pub/object_validators/reject_validation_test.exs
new file mode 100644 (file)
index 0000000..370bb6e
--- /dev/null
@@ -0,0 +1,56 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.ObjectValidators.RejectValidationTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.Web.ActivityPub.Builder
+  alias Pleroma.Web.ActivityPub.ObjectValidator
+  alias Pleroma.Web.ActivityPub.Pipeline
+
+  import Pleroma.Factory
+
+  setup do
+    follower = insert(:user)
+    followed = insert(:user, local: false)
+
+    {:ok, follow_data, _} = Builder.follow(follower, followed)
+    {:ok, follow_activity, _} = Pipeline.common_pipeline(follow_data, local: true)
+
+    {:ok, reject_data, _} = Builder.reject(followed, follow_activity)
+
+    %{reject_data: reject_data, followed: followed}
+  end
+
+  test "it validates a basic 'reject'", %{reject_data: reject_data} do
+    assert {:ok, _, _} = ObjectValidator.validate(reject_data, [])
+  end
+
+  test "it fails when the actor doesn't exist", %{reject_data: reject_data} do
+    reject_data =
+      reject_data
+      |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
+
+    assert {:error, _} = ObjectValidator.validate(reject_data, [])
+  end
+
+  test "it fails when the rejected activity doesn't exist", %{reject_data: reject_data} do
+    reject_data =
+      reject_data
+      |> Map.put("object", "https://gensokyo.2hu/users/raymoo/follows/1")
+
+    assert {:error, _} = ObjectValidator.validate(reject_data, [])
+  end
+
+  test "for an rejected follow, it only validates if the actor of the reject is the followed actor",
+       %{reject_data: reject_data} do
+    stranger = insert(:user)
+
+    reject_data =
+      reject_data
+      |> Map.put("actor", stranger.ap_id)
+
+    assert {:error, _} = ObjectValidator.validate(reject_data, [])
+  end
+end
index 5e524864149a9029f04880dcb210ece2ae41c643..7592fbe1c6c23126e7fddb6fca76f34d2e1648dd 100644 (file)
@@ -24,7 +24,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.RejectHandlingTest do
     accept_data =
       Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
 
-    :error = Transmogrifier.handle_incoming(accept_data)
+    {:error, _} = Transmogrifier.handle_incoming(accept_data)
 
     follower = User.get_cached_by_id(follower.id)