Merge branch 'image-description-summary' into 'develop'

Add support for Honk "summary" + "name"

See merge request pleroma/pleroma!3854
This commit is contained in:
lain 2024-05-27 16:51:07 +00:00
commit 07b7a8d697
6 changed files with 97 additions and 42 deletions

View file

@ -0,0 +1 @@
Support honk-style attachment summaries as alt-text.

View file

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

View file

@ -50,7 +50,11 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Attachment do
pleroma: %Schema{ pleroma: %Schema{
type: :object, type: :object,
properties: %{ properties: %{
mime_type: %Schema{type: :string, description: "mime type of the attachment"} mime_type: %Schema{type: :string, description: "mime type of the attachment"},
name: %Schema{
type: :string,
description: "Name of the attachment, typically the filename"
}
} }
} }
}, },

View file

@ -624,6 +624,19 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
to_string(attachment["id"] || hash_id) to_string(attachment["id"] || hash_id)
end end
description =
if attachment["summary"] do
HTML.strip_tags(attachment["summary"])
else
attachment["name"]
end
name = if attachment["summary"], do: attachment["name"]
pleroma =
%{mime_type: media_type}
|> Maps.put_if_present(:name, name)
%{ %{
id: attachment_id, id: attachment_id,
url: href, url: href,
@ -631,8 +644,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
preview_url: href_preview, preview_url: href_preview,
text_url: href, text_url: href,
type: type, type: type,
description: attachment["name"], description: description,
pleroma: %{mime_type: media_type}, pleroma: pleroma,
blurhash: attachment["blurhash"] blurhash: attachment["blurhash"]
} }
|> Maps.put_if_present(:meta, meta) |> Maps.put_if_present(:meta, meta)

View file

@ -27,19 +27,22 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do
end end
test "works with honkerific attachments" do test "works with honkerific attachments" do
attachment = %{ honk = %{
"mediaType" => "", "mediaType" => "",
"name" => "", "summary" => "Select your spirit chonk",
"summary" => "298p3RG7j27tfsZ9RQ.jpg", "name" => "298p3RG7j27tfsZ9RQ.jpg",
"type" => "Document", "type" => "Document",
"url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg" "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
} }
assert {:ok, attachment} = assert {:ok, attachment} =
AttachmentValidator.cast_and_validate(attachment) honk
|> AttachmentValidator.cast_and_validate()
|> Ecto.Changeset.apply_action(:insert) |> Ecto.Changeset.apply_action(:insert)
assert attachment.mediaType == "application/octet-stream" assert attachment.mediaType == "application/octet-stream"
assert attachment.summary == "Select your spirit chonk"
assert attachment.name == "298p3RG7j27tfsZ9RQ.jpg"
end end
test "works with an unknown but valid mime type" do test "works with an unknown but valid mime type" do

View file

@ -591,45 +591,78 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
assert mention.url == recipient.ap_id assert mention.url == recipient.ap_id
end end
test "attachments" do describe "attachments" do
object = %{ test "Complete Mastodon style" do
"type" => "Image", object = %{
"url" => [ "type" => "Image",
%{ "url" => [
"mediaType" => "image/png", %{
"href" => "someurl", "mediaType" => "image/png",
"width" => 200, "href" => "someurl",
"height" => 100 "width" => 200,
} "height" => 100
], }
"blurhash" => "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn", ],
"uuid" => 6 "blurhash" => "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn",
} "uuid" => 6
}
expected = %{ expected = %{
id: "1638338801", id: "1638338801",
type: "image", type: "image",
url: "someurl", url: "someurl",
remote_url: "someurl", remote_url: "someurl",
preview_url: "someurl", preview_url: "someurl",
text_url: "someurl", text_url: "someurl",
description: nil, description: nil,
pleroma: %{mime_type: "image/png"}, pleroma: %{mime_type: "image/png"},
meta: %{original: %{width: 200, height: 100, aspect: 2}}, meta: %{original: %{width: 200, height: 100, aspect: 2}},
blurhash: "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn" blurhash: "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn"
} }
api_spec = Pleroma.Web.ApiSpec.spec() api_spec = Pleroma.Web.ApiSpec.spec()
assert expected == StatusView.render("attachment.json", %{attachment: object}) assert expected == StatusView.render("attachment.json", %{attachment: object})
assert_schema(expected, "Attachment", api_spec) assert_schema(expected, "Attachment", api_spec)
# If theres a "id", use that instead of the generated one # If theres a "id", use that instead of the generated one
object = Map.put(object, "id", 2) object = Map.put(object, "id", 2)
result = StatusView.render("attachment.json", %{attachment: object}) result = StatusView.render("attachment.json", %{attachment: object})
assert %{id: "2"} = result assert %{id: "2"} = result
assert_schema(result, "Attachment", api_spec) assert_schema(result, "Attachment", api_spec)
end
test "Honkerific" do
object = %{
"type" => "Image",
"url" => [
%{
"mediaType" => "image/png",
"href" => "someurl"
}
],
"name" => "fool.jpeg",
"summary" => "they have played us for absolute fools."
}
expected = %{
blurhash: nil,
description: "they have played us for absolute fools.",
id: "1638338801",
pleroma: %{mime_type: "image/png", name: "fool.jpeg"},
preview_url: "someurl",
remote_url: "someurl",
text_url: "someurl",
type: "image",
url: "someurl"
}
api_spec = Pleroma.Web.ApiSpec.spec()
assert expected == StatusView.render("attachment.json", %{attachment: object})
assert_schema(expected, "Attachment", api_spec)
end
end end
test "put the url advertised in the Activity in to the url attribute" do test "put the url advertised in the Activity in to the url attribute" do