reject activity creation
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 3 Sep 2020 10:30:39 +0000 (13:30 +0300)
committerrinpatch <rinpatch@sdf.org>
Thu, 10 Sep 2020 18:50:41 +0000 (21:50 +0300)
if passed expires_at option and expiring activities are not configured

lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/workers/purge_expired_activity.ex
test/web/activity_pub/activity_pub_test.exs

index c33848277d8b8577e1a08e8befb437808b5bc03b..ee6dcf58a7d989e4383d09f12e6f0e6fd29ff2cc 100644 (file)
@@ -110,23 +110,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   def insert(map, local \\ true, fake \\ false, bypass_actor_check \\ false) when is_map(map) do
     with nil <- Activity.normalize(map),
          map <- lazy_put_activity_defaults(map, fake),
-         true <- bypass_actor_check || check_actor_is_active(map["actor"]),
-         {_, true} <- {:remote_limit_error, check_remote_limit(map)},
+         {_, true} <- {:actor_check, bypass_actor_check || check_actor_is_active(map["actor"])},
+         {_, true} <- {:remote_limit_pass, check_remote_limit(map)},
          {:ok, map} <- MRF.filter(map),
          {recipients, _, _} = get_recipients(map),
          {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
          {:containment, :ok} <- {:containment, Containment.contain_child(map)},
-         {:ok, map, object} <- insert_full_object(map) do
-      {:ok, activity} =
-        %Activity{
-          data: map,
-          local: local,
-          actor: map["actor"],
-          recipients: recipients
-        }
-        |> Repo.insert()
-        |> maybe_create_activity_expiration()
-
+         {:ok, map, object} <- insert_full_object(map),
+         {:ok, activity} <- insert_activity_with_expiration(map, local, recipients) do
       # Splice in the child object if we have one.
       activity = Maps.put_if_present(activity, :object, object)
 
@@ -137,6 +128,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       %Activity{} = activity ->
         {:ok, activity}
 
+      {:actor_check, _} ->
+        {:error, false}
+
+      {:containment, _} = error ->
+        error
+
+      {:error, _} = error ->
+        error
+
       {:fake, true, map, recipients} ->
         activity = %Activity{
           data: map,
@@ -149,11 +149,25 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
         Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
         {:ok, activity}
 
-      error ->
-        {:error, error}
+      {:remote_limit_pass, _} ->
+        {:error, :remote_limit}
+
+      {:reject, reason} ->
+        {:error, reason}
     end
   end
 
+  defp insert_activity_with_expiration(data, local, recipients) do
+    %Activity{
+      data: data,
+      local: local,
+      actor: data["actor"],
+      recipients: recipients
+    }
+    |> Repo.insert()
+    |> maybe_create_activity_expiration()
+  end
+
   def notify_and_stream(activity) do
     Notification.create_notifications(activity)
 
index 42e2ae79c9eb62c9f23cadd55f1bcf1d4e1f81d9..c70587b47f041f02baf8a81e9cbd8b16d2c02046 100644 (file)
@@ -13,6 +13,10 @@ defmodule Pleroma.Workers.PurgeExpiredActivity do
 
   alias Pleroma.Activity
 
+  @spec enqueue(map()) ::
+          {:ok, Oban.Job.t()}
+          | {:error, :expired_activities_disabled}
+          | {:error, :expiration_too_close}
   def enqueue(args) do
     with true <- enabled?(),
          args when is_map(args) <- validate_expires_at(args) do
index 9af57392483b476cdc160ba2b0ab5e0cd719eab7..d8caa0b00b0d6162b38426f5fd93bd695a98e448 100644 (file)
@@ -239,7 +239,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
         }
       }
 
-      assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
+      assert {:error, :remote_limit} = ActivityPub.insert(data)
     end
 
     test "doesn't drop activities with content being null" do
@@ -386,9 +386,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   end
 
   describe "create activities" do
-    test "it reverts create" do
-      user = insert(:user)
+    setup do
+      [user: insert(:user)]
+    end
 
+    test "it reverts create", %{user: user} do
       with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
         assert {:error, :reverted} =
                  ActivityPub.create(%{
@@ -407,9 +409,47 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert Repo.aggregate(Object, :count, :id) == 0
     end
 
-    test "removes doubled 'to' recipients" do
-      user = insert(:user)
+    test "creates activity if expiration is not configured and expires_at is not passed", %{
+      user: user
+    } do
+      clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
+
+      assert {:ok, _} =
+               ActivityPub.create(%{
+                 to: ["user1", "user2"],
+                 actor: user,
+                 context: "",
+                 object: %{
+                   "to" => ["user1", "user2"],
+                   "type" => "Note",
+                   "content" => "testing"
+                 }
+               })
+    end
 
+    test "rejects activity if expires_at present but expiration is not configured", %{user: user} do
+      clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
+
+      assert {:error, :expired_activities_disabled} =
+               ActivityPub.create(%{
+                 to: ["user1", "user2"],
+                 actor: user,
+                 context: "",
+                 object: %{
+                   "to" => ["user1", "user2"],
+                   "type" => "Note",
+                   "content" => "testing"
+                 },
+                 additional: %{
+                   "expires_at" => DateTime.utc_now()
+                 }
+               })
+
+      assert Repo.aggregate(Activity, :count, :id) == 0
+      assert Repo.aggregate(Object, :count, :id) == 0
+    end
+
+    test "removes doubled 'to' recipients", %{user: user} do
       {:ok, activity} =
         ActivityPub.create(%{
           to: ["user1", "user1", "user2"],
@@ -427,9 +467,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert activity.recipients == ["user1", "user2", user.ap_id]
     end
 
-    test "increases user note count only for public activities" do
-      user = insert(:user)
-
+    test "increases user note count only for public activities", %{user: user} do
       {:ok, _} =
         CommonAPI.post(User.get_cached_by_id(user.id), %{
           status: "1",
@@ -458,8 +496,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert user.note_count == 2
     end
 
-    test "increases replies count" do
-      user = insert(:user)
+    test "increases replies count", %{user: user} do
       user2 = insert(:user)
 
       {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})