api/views/status: prefer 'summary' for attachment alt texts

GtS started exclusively using it and it already worked with Mastodon.
See: https://codeberg.org/superseriousbusiness/gotosocial/issues/4524

Since we used to (implicitly) strip the summary field
this will not take effect retroactively.
This commit is contained in:
Oneric 2025-11-16 00:00:00 +00:00
parent 9bbebab1a2
commit 180a6ba962
5 changed files with 84 additions and 2 deletions

View file

@ -15,6 +15,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
field(:type, :string)
field(:mediaType, :string, default: "application/octet-stream")
field(:name, :string)
field(:summary, :string)
field(:blurhash, :string)
embeds_many :url, UrlObjectValidator, primary_key: false do
@ -44,7 +45,7 @@ def changeset(struct, data) do
|> fix_url()
struct
|> cast(data, [:id, :type, :mediaType, :name, :blurhash])
|> cast(data, [:id, :type, :mediaType, :name, :summary, :blurhash])
|> cast_embed(:url, with: &url_changeset/2, required: true)
|> validate_inclusion(:type, ~w[Link Document Audio Image Video])
|> validate_required([:type, :mediaType])

View file

@ -339,6 +339,7 @@ def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachm
}
|> Maps.put_if_present("mediaType", media_type)
|> Maps.put_if_present("name", data["name"])
|> Maps.put_if_present("summary", data["summary"])
|> Maps.put_if_present("blurhash", data["blurhash"])
else
nil

View file

@ -610,7 +610,7 @@ def render("attachment.json", %{attachment: attachment}) do
preview_url: href_preview,
text_url: href,
type: type,
description: attachment["name"],
description: attachment["summary"] || attachment["name"],
pleroma: %{mime_type: media_type},
blurhash: attachment["blurhash"]
}

View file

@ -383,6 +383,32 @@ test "it correctly processes messages with weirdness in address fields" do
assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
end
test "preserves both name and summary of attachments until the end" do
name = "marvellous.png"
summary = "The most wondrous thing youve ever seen."
data =
Jason.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
|> put_in(["object", "attachment"], [
%{
"type" => "Image",
"mediaType" => "image/png",
"blurhash" => "LIN1M;~p~W%gt-RPjENI-=RiM_WE",
"name" => name,
"summary" => summary,
"url" => "https://example.org/marvellous.png"
}
])
{:ok, activity} = Transmogrifier.handle_incoming(data)
%Object{} = obj = Object.normalize(activity)
[attach] = obj.data["attachment"]
assert attach["name"] == name
assert attach["summary"] == summary
end
end
describe "`handle_incoming/2`, Mastodon format `replies` handling" do

View file

@ -667,6 +667,60 @@ test "attachment type will fallback to generic type if inconclusive full media t
assert resp[:pleroma][:mime_type] == "application/octet-stream"
end
test "attachment alt text can use the summary attribute" do
# federated like this by e.g. GtS
alt_text = "Two sloths hanging from the same branch. Its sunny."
attachment_ap = %{
"blurhash" => "L38}3{XS9EInNZtSxvxbH=ngocWT",
"mediaType" => "image/png",
"summary" => alt_text,
"type" => "Image",
"url" => [
%{
"type" => "Link",
"href" =>
"https://gts.exampleorg/fileserver/016VVVVV/attachment/original/01JSXXYZZ.png",
"mediaType" => "image/png"
}
]
}
resp = StatusView.render("attachment.json", %{attachment: attachment_ap})
api_spec = Pleroma.Web.ApiSpec.spec()
assert_schema(resp, "Attachment", api_spec)
assert resp[:description] == alt_text
end
test "attachment alt text prefers the summary attribute when name is also present" do
alt_text = "Two sloths hanging from the same branch. Its sunny."
attachment_ap = %{
"blurhash" => "L38}3{XS9EInNZtSxvxbH=ngocWT",
"mediaType" => "image/png",
"name" => "two_sloths.png",
"summary" => alt_text,
"type" => "Image",
"url" => [
%{
"type" => "Link",
"href" =>
"https://gts.exampleorg/fileserver/016VVVVV/attachment/original/01JSXXYZZ.png",
"mediaType" => "image/png"
}
]
}
resp = StatusView.render("attachment.json", %{attachment: attachment_ap})
api_spec = Pleroma.Web.ApiSpec.spec()
assert_schema(resp, "Attachment", api_spec)
assert resp[:description] == alt_text
end
test "put the url advertised in the Activity in to the url attribute" do
Pleroma.Config.put([:instance, :limit_to_local_content], false)
id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"