Add support for a first reference in pinned objects

This commit is contained in:
FloatingGhost 2022-07-03 17:25:20 +01:00
parent 4da9a12bf8
commit 0a3a552696
3 changed files with 38 additions and 6 deletions

View file

@ -1662,6 +1662,36 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
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(%{ def pin_data_from_featured_collection(%{
"type" => type, "type" => type,
"orderedItems" => objects "orderedItems" => objects

View file

@ -26,5 +26,9 @@
"type": "OrderedCollectionPage", "type": "OrderedCollectionPage",
"totalItems": 0, "totalItems": 0,
"partOf": "https://friendica.example.com/featured/raha", "partOf": "https://friendica.example.com/featured/raha",
"orderedItems": [] "orderedItems": [
{
"id": "http://inserted"
}
]
} }

View file

@ -315,16 +315,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
end end
test "fetches user featured collection using the first property" do 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" 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 = featured_data =
"test/fixtures/friendica/friendica_featured_collection.json" "test/fixtures/friendica/friendica_featured_collection.json"
|> File.read!() |> File.read!()
page_data = page_data =
"test/fixtures/friendica/friendica_featured_collection.json" "test/fixtures/friendica/friendica_featured_collection_first.json"
|> File.read!() |> File.read!()
Tesla.Mock.mock(fn Tesla.Mock.mock(fn
@ -337,9 +336,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
body: featured_data, body: featured_data,
headers: [{"content-type", "application/activity+json"}] headers: [{"content-type", "application/activity+json"}]
} }
end)
Tesla.Mock.mock_global(fn
%{ %{
method: :get, method: :get,
url: ^first_url url: ^first_url
@ -352,6 +349,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
end) end)
{:ok, data} = ActivityPub.fetch_and_prepare_featured_from_ap_id(featured_url) {:ok, data} = ActivityPub.fetch_and_prepare_featured_from_ap_id(featured_url)
assert Map.has_key?(data, "http://inserted")
end end
test "it fetches the appropriate tag-restricted posts" do test "it fetches the appropriate tag-restricted posts" do