AttachmentValidator: Add support for Honk "summary" + "name"

As used by Honk and supported by Mastodon
This commit is contained in:
Haelwenn (lanodan) Monnier 2023-03-09 10:21:11 +01:00
parent 714bf0cb23
commit 2ae1b802f2
2 changed files with 9 additions and 5 deletions

View File

@ -15,6 +15,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
field(:type, :string) field(:type, :string)
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 @@ def changeset(struct, data) 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

@ -25,19 +25,22 @@ test "fails without url" 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