From 4985902b029bf0ea6afd7951d3050c5b6d5e13c4 Mon Sep 17 00:00:00 2001 From: Phantasm Date: Sun, 14 Dec 2025 22:33:59 +0100 Subject: [PATCH] Add Actor images normalization from array of urls to string --- changelog.d/normalize-actor-image-hrefs.fix | 1 + lib/pleroma/web/activity_pub/activity_pub.ex | 9 +++- test/fixtures/users_mock/href_as_array.json | 41 +++++++++++++++++++ .../web/activity_pub/activity_pub_test.exs | 34 +++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 changelog.d/normalize-actor-image-hrefs.fix create mode 100644 test/fixtures/users_mock/href_as_array.json diff --git a/changelog.d/normalize-actor-image-hrefs.fix b/changelog.d/normalize-actor-image-hrefs.fix new file mode 100644 index 000000000..33d222391 --- /dev/null +++ b/changelog.d/normalize-actor-image-hrefs.fix @@ -0,0 +1 @@ +Add Actor images normalization from array of urls to string diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 3e5239a28..edf2e1fa7 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1569,7 +1569,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp get_actor_url(_url), do: nil - defp normalize_image(%{"url" => url} = data) do + defp normalize_image(%{"url" => url} = data) when is_binary(url) do %{ "type" => "Image", "url" => [%{"href" => url}] @@ -1577,6 +1577,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> maybe_put_description(data) end + defp normalize_image(%{"url" => urls}) when is_list(urls) do + url = urls |> List.first() + + %{"url" => url} + |> normalize_image() + end + defp normalize_image(urls) when is_list(urls), do: urls |> List.first() |> normalize_image() defp normalize_image(_), do: nil diff --git a/test/fixtures/users_mock/href_as_array.json b/test/fixtures/users_mock/href_as_array.json new file mode 100644 index 000000000..624612bce --- /dev/null +++ b/test/fixtures/users_mock/href_as_array.json @@ -0,0 +1,41 @@ +{ + "alsoKnownAs": [], + "attachment": [], + "capabilities": {}, + "discoverable": true, + "endpoints": {}, + "featured": "https://queef.in/cute_cat/collections/featured", + "followers": "https://queef.in/cute_cat/followers", + "following": "https://queef.in/cute_cat/following", + "icon": { + "type": "Image", + "url": [ + "https://queef.in/storage/profile.webp", + "https://example.com/image" + ] + }, + "id": "https://queef.in/cute_cat", + "image": { + "type": "Image", + "url": [ + "https://queef.in/storage/banner.gif", + "https://example.com/image" + ] + }, + "inbox": "https://queef.in/cute_cat/inbox", + "manuallyApprovesFollowers": false, + "name": "cute_cat", + "outbox": "https://queef.in/cute_cat/outbox", + "preferredUsername": "cute_cat", + "publicKey": { + "id": "https://queef.in/cute_cat#main-key", + "owner": "https://queef.in/cute_cat" + }, + "published": "2025-08-18T01:16:10.000Z", + "summary": "A cute cat", + "tag": [], + "type": "Person", + "url": "https://queef.in/cute_cat", + "vcard:bday": null, + "webfinger": "acct:cute_cat@queef.in" +} diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index 7fc4f47cd..c10735d45 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -465,6 +465,40 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end end + test "works with avatar/banner href as list" do + user_id = "https://queef.in/cute_cat" + + user_data = + "test/fixtures/users_mock/href_as_array.json" + |> File.read!() + |> Jason.decode!() + |> Map.delete("featured") + |> Jason.encode!() + + Tesla.Mock.mock(fn + %{ + method: :get, + url: ^user_id + } -> + %Tesla.Env{ + status: 200, + body: user_data, + headers: [{"content-type", "application/activity+json"}] + } + end) + + {:ok, user} = ActivityPub.make_user_from_ap_id(user_id) + + assert length(user.avatar["url"]) == 1 + assert length(user.banner["url"]) == 1 + + assert user.avatar["url"] |> List.first() |> Map.fetch!("href") == + "https://queef.in/storage/profile.webp" + + assert user.banner["url"] |> List.first() |> Map.fetch!("href") == + "https://queef.in/storage/banner.gif" + end + test "it fetches the appropriate tag-restricted posts" do user = insert(:user)