Use Akkoma modification for collections
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 3 Jul 2022 18:36:30 +0000 (19:36 +0100)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 3 Jul 2022 18:36:30 +0000 (19:36 +0100)
config/description.exs
docs/configuration/cheatsheet.md
lib/pleroma/collections/fetcher.ex
lib/pleroma/web/activity_pub/activity_pub.ex

index 9401bed5c506cb60990f3b091b9310d8deeab405..3777905a36ddc0af2b412faaf8fcd63a278e7b4c 100644 (file)
@@ -1689,6 +1689,13 @@ config :pleroma, :config_description, [
         type: :integer,
         description: "Following handshake timeout",
         suggestions: [500]
+      },
+      %{
+        key: :max_collection_objects,
+        type: :integer,
+        description:
+          "The maximum number of items to fetch from a remote collections. Setting this too low can lead to only getting partial collections, but too high and you can end up fetching far too many objects.",
+        suggestions: [50]
       }
     ]
   },
index 3097f1190323d99d7f97879a1060b0c35811c846..11083e831527b0ae07b4705f77622f0b7f8e217a 100644 (file)
@@ -236,6 +236,7 @@ Notes:
 * `deny_follow_blocked`: Whether to disallow following an account that has blocked the user in question
 * `sign_object_fetches`: Sign object fetches with HTTP signatures
 * `authorized_fetch_mode`: Require HTTP signatures for AP fetches
+* `max_collection_objects`: The maximum number of objects to fetch from a remote AP collection.
 
 ## Pleroma.User
 
index 205c62b4eec89fb2686b7d4a03e92929759019e8..382defff41b815d68958a1c67e4be3c2fa0722cb 100644 (file)
@@ -14,12 +14,21 @@ defmodule Akkoma.Collections.Fetcher do
     fetch_collection(ap_id)
   end
 
-  defp fetch_collection(ap_id) do
+  def fetch_collection(ap_id) when is_binary(ap_id) do
     with {:ok, page} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id) do
       {:ok, objects_from_collection(page)}
+    else
+      e ->
+        Logger.error("Could not fetch collection #{ap_id} - #{inspect(e)}")
+        e
     end
   end
 
+  def fetch_collection(%{"type" => type} = page)
+      when type in ["Collection", "OrderedCollection"] do
+    {:ok, objects_from_collection(page)}
+  end
+
   defp items_in_page(%{"type" => type, "orderedItems" => items})
        when is_list(items) and type in ["OrderedCollection", "OrderedCollectionPage"],
        do: items
index bf766699d3fca47f3df341c804f3c78c1be77eb3..77f38f9f19bafb086bb2b4921a743d408c55f743 100644 (file)
@@ -3,6 +3,7 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.ActivityPub.ActivityPub do
+  alias Akkoma.Collections
   alias Pleroma.Activity
   alias Pleroma.Activity.Ir.Topics
   alias Pleroma.Config
@@ -1677,26 +1678,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     end
   end
 
-  def pin_data_from_featured_collection(%{
-        "type" => "Collection",
-        "first" => first
-      }) do
-    with {:ok, page} <- Fetcher.fetch_and_contain_remote_object_from_id(first) do
-      page
-      |> Map.get("items")
-      |> Map.new(fn %{"id" => object_ap_id} -> {object_ap_id, NaiveDateTime.utc_now()} end)
-    else
-      e ->
-        Logger.error("Could not decode featured collection at fetch #{first}, #{inspect(e)}")
-        {:ok, %{}}
-    end
-  end
-
-  def pin_data_from_featured_collection(%{
-        "type" => type,
-        "orderedItems" => objects
-      })
+  def pin_data_from_featured_collection(
+        %{
+          "type" => type
+        } = collection
+      )
       when type in ["OrderedCollection", "Collection"] do
+    {:ok, objects} = Collections.Fetcher.fetch_collection(collection)
     Map.new(objects, fn %{"id" => object_ap_id} -> {object_ap_id, NaiveDateTime.utc_now()} end)
   end