Merge remote-tracking branch 'remotes/origin/develop' into 1505-threads-federation
[akkoma] / test / web / activity_pub / transmogrifier_test.exs
index 1b12ee3a9dfd118072cdf1eef234b0863b8da98c..3720dda2a5de10c28da9dac2b693f1d8f8dc5a3b 100644 (file)
@@ -3,7 +3,9 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
+  use Oban.Testing, repo: Pleroma.Repo
   use Pleroma.DataCase
+
   alias Pleroma.Activity
   alias Pleroma.Object
   alias Pleroma.Object.Fetcher
@@ -1348,6 +1350,52 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
     end
   end
 
+  describe "`replies` handling in handle_incoming/2" do
+    setup do
+      data =
+        File.read!("test/fixtures/mastodon-post-activity.json")
+        |> Poison.decode!()
+
+      items = ["https://shitposter.club/notice/2827873", "https://shitposter.club/notice/7387606"]
+      collection = %{"items" => items}
+      %{data: data, items: items, collection: collection}
+    end
+
+    test "with wrapped `replies` collection, it schedules background fetching of items", %{
+      data: data,
+      items: items,
+      collection: collection
+    } do
+      replies = %{"first" => collection}
+
+      object = Map.put(data["object"], "replies", replies)
+      data = Map.put(data, "object", object)
+      {:ok, _activity} = Transmogrifier.handle_incoming(data)
+
+      for id <- items do
+        job_args = %{"op" => "fetch_remote", "id" => id}
+        assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
+      end
+    end
+
+    test "it schedules background fetching of unwrapped `replies` collection items", %{
+      data: data,
+      items: items,
+      collection: collection
+    } do
+      replies = collection
+
+      object = Map.put(data["object"], "replies", replies)
+      data = Map.put(data, "object", object)
+      {:ok, _activity} = Transmogrifier.handle_incoming(data)
+
+      for id <- items do
+        job_args = %{"op" => "fetch_remote", "id" => id}
+        assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
+      end
+    end
+  end
+
   describe "prepare outgoing" do
     test "it inlines private announced objects" do
       user = insert(:user)
@@ -2046,4 +2094,51 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
              }
     end
   end
+
+  describe "set_replies/1" do
+    clear_config([:activitypub, :note_replies_output_limit]) do
+      Pleroma.Config.put([:activitypub, :note_replies_output_limit], 2)
+    end
+
+    test "returns unmodified object if activity doesn't have self-replies" do
+      data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
+      assert Transmogrifier.set_replies(data) == data
+    end
+
+    test "sets `replies` collection with a limited number of self-replies" do
+      [user, another_user] = insert_list(2, :user)
+
+      {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"})
+
+      {:ok, %{id: id2} = self_reply1} =
+        CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1})
+
+      {:ok, self_reply2} =
+        CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1})
+
+      # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2
+      {:ok, _} =
+        CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1})
+
+      {:ok, _} =
+        CommonAPI.post(user, %{
+          "status" => "self-reply to self-reply",
+          "in_reply_to_status_id" => id2
+        })
+
+      {:ok, _} =
+        CommonAPI.post(another_user, %{
+          "status" => "another user's reply",
+          "in_reply_to_status_id" => id1
+        })
+
+      object = Object.normalize(activity)
+      replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.data["id"] end)
+
+      assert %{
+               "type" => "Collection",
+               "first" => %{"type" => "Collection", "items" => ^replies_uris}
+             } = Transmogrifier.set_replies(object.data)["replies"]
+    end
+  end
 end