Add support for a `first` reference in pinned objects
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 3 Jul 2022 16:25:20 +0000 (17:25 +0100)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 3 Jul 2022 16:25:20 +0000 (17:25 +0100)
lib/pleroma/web/activity_pub/activity_pub.ex
test/fixtures/friendica/friendica_featured_collection_first.json
test/pleroma/web/activity_pub/activity_pub_test.exs

index e6548a8188a535d35cdfc29763bcce96cd63e94b..bf766699d3fca47f3df341c804f3c78c1be77eb3 100644 (file)
@@ -1662,6 +1662,36 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     end
   end
 
+  def pin_data_from_featured_collection(%{
+        "type" => "OrderedCollection",
+        "first" => first
+      }) do
+    with {:ok, page} <- Fetcher.fetch_and_contain_remote_object_from_id(first) do
+      page
+      |> Map.get("orderedItems")
+      |> 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" => "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
index 7d450e42f529ba0164058b3c51ce25ea70672faf..1f9dce42066a008c48b72e35d8515a8c7648a577 100644 (file)
@@ -26,5 +26,9 @@
     "type": "OrderedCollectionPage",
     "totalItems": 0,
     "partOf": "https://friendica.example.com/featured/raha",
-    "orderedItems": []
+    "orderedItems": [
+        {
+            "id": "http://inserted"
+        }
+     ]
 }
index b21b9ee283969b893bb92f58f80d0663890b5e8c..2b65f59e0ac9335d54f83d6a6bafb04f79d69d82 100644 (file)
@@ -315,16 +315,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   end
 
   test "fetches user featured collection using the first property" do
-    ap_id = "https://friendica.example.com/raha"
     featured_url = "https://friendica.example.com/raha/collections/featured"
-    first_url = "https://friendica.mnementh.co.uk/featured/spyro?page=1"
+    first_url = "https://friendica.example.com/featured/raha?page=1"
 
     featured_data =
       "test/fixtures/friendica/friendica_featured_collection.json"
       |> File.read!()
 
     page_data =
-      "test/fixtures/friendica/friendica_featured_collection.json"
+      "test/fixtures/friendica/friendica_featured_collection_first.json"
       |> File.read!()
 
     Tesla.Mock.mock(fn
@@ -337,9 +336,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
           body: featured_data,
           headers: [{"content-type", "application/activity+json"}]
         }
-    end)
 
-    Tesla.Mock.mock_global(fn
       %{
         method: :get,
         url: ^first_url
@@ -352,6 +349,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end)
 
     {:ok, data} = ActivityPub.fetch_and_prepare_featured_from_ap_id(featured_url)
+    assert Map.has_key?(data, "http://inserted")
   end
 
   test "it fetches the appropriate tag-restricted posts" do