forked from AkkomaGang/akkoma
Set the correct height/width if the data is available when generating opengraph metadata
This commit is contained in:
parent
aa8cc4e86e
commit
d70db63084
2 changed files with 37 additions and 5 deletions
|
@ -69,8 +69,7 @@ defp build_attachments(%{data: %{"attachment" => attachments}}) do
|
||||||
Enum.reduce(attachments, [], fn attachment, acc ->
|
Enum.reduce(attachments, [], fn attachment, acc ->
|
||||||
rendered_tags =
|
rendered_tags =
|
||||||
Enum.reduce(attachment["url"], [], fn url, acc ->
|
Enum.reduce(attachment["url"], [], fn url, acc ->
|
||||||
# TODO: Add additional properties to objects when we have the data available.
|
# TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
|
||||||
# Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
|
|
||||||
# object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
|
# object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
|
||||||
case Utils.fetch_media_type(@media_types, url["mediaType"]) do
|
case Utils.fetch_media_type(@media_types, url["mediaType"]) do
|
||||||
"audio" ->
|
"audio" ->
|
||||||
|
@ -85,12 +84,14 @@ defp build_attachments(%{data: %{"attachment" => attachments}}) do
|
||||||
{:meta, [property: "og:image:alt", content: attachment["name"]], []}
|
{:meta, [property: "og:image:alt", content: attachment["name"]], []}
|
||||||
| acc
|
| acc
|
||||||
]
|
]
|
||||||
|
|> maybe_add_dimensions(url)
|
||||||
|
|
||||||
"video" ->
|
"video" ->
|
||||||
[
|
[
|
||||||
{:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
|
{:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
|
||||||
| acc
|
| acc
|
||||||
]
|
]
|
||||||
|
|> maybe_add_dimensions(url)
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
acc
|
acc
|
||||||
|
@ -102,4 +103,21 @@ defp build_attachments(%{data: %{"attachment" => attachments}}) do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp build_attachments(_), do: []
|
defp build_attachments(_), do: []
|
||||||
|
|
||||||
|
# We can use url["mediaType"] to dynamically fill the metadata
|
||||||
|
defp maybe_add_dimensions(metadata, url) do
|
||||||
|
type = url["mediaType"] |> String.split("/") |> List.first()
|
||||||
|
|
||||||
|
cond do
|
||||||
|
!is_nil(url["height"]) && !is_nil(url["width"]) ->
|
||||||
|
metadata ++
|
||||||
|
[
|
||||||
|
{:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
|
||||||
|
{:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
|
||||||
|
]
|
||||||
|
|
||||||
|
true ->
|
||||||
|
metadata
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,12 @@ test "it renders all supported types of attachments and skips unknown types" do
|
||||||
"attachment" => [
|
"attachment" => [
|
||||||
%{
|
%{
|
||||||
"url" => [
|
"url" => [
|
||||||
%{"mediaType" => "image/png", "href" => "https://pleroma.gov/tenshi.png"}
|
%{
|
||||||
|
"mediaType" => "image/png",
|
||||||
|
"href" => "https://pleroma.gov/tenshi.png",
|
||||||
|
"height" => 1024,
|
||||||
|
"width" => 1280
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
|
@ -35,7 +40,12 @@ test "it renders all supported types of attachments and skips unknown types" do
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
"url" => [
|
"url" => [
|
||||||
%{"mediaType" => "video/webm", "href" => "https://pleroma.gov/about/juche.webm"}
|
%{
|
||||||
|
"mediaType" => "video/webm",
|
||||||
|
"href" => "https://pleroma.gov/about/juche.webm",
|
||||||
|
"height" => 600,
|
||||||
|
"width" => 800
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
%{
|
%{
|
||||||
|
@ -55,11 +65,15 @@ test "it renders all supported types of attachments and skips unknown types" do
|
||||||
assert Enum.all?(
|
assert Enum.all?(
|
||||||
[
|
[
|
||||||
{:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []},
|
{:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []},
|
||||||
|
{:meta, [property: "og:image:width", content: "1280"], []},
|
||||||
|
{:meta, [property: "og:image:height", content: "1024"], []},
|
||||||
{:meta,
|
{:meta,
|
||||||
[property: "og:audio", content: "http://www.gnu.org/music/free-software-song.au"],
|
[property: "og:audio", content: "http://www.gnu.org/music/free-software-song.au"],
|
||||||
[]},
|
[]},
|
||||||
{:meta, [property: "og:video", content: "https://pleroma.gov/about/juche.webm"],
|
{:meta, [property: "og:video", content: "https://pleroma.gov/about/juche.webm"],
|
||||||
[]}
|
[]},
|
||||||
|
{:meta, [property: "og:video:width", content: "800"], []},
|
||||||
|
{:meta, [property: "og:video:height", content: "600"], []}
|
||||||
],
|
],
|
||||||
fn element -> element in result end
|
fn element -> element in result end
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue