summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMark Felder <feld@feld.me>2021-06-08 16:58:33 -0500
committerMark Felder <feld@feld.me>2021-06-08 16:58:33 -0500
commitd70db63084449e48e90288bc7484733171246625 (patch)
tree310a2fe28b19e2ba01dc12efd719c0ce4accea2c /lib
parentaa8cc4e86e5c7a53fa8bc606dbce6c6b3a0a8c02 (diff)
Set the correct height/width if the data is available when generating opengraph metadata
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/metadata/providers/open_graph.ex22
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/pleroma/web/metadata/providers/open_graph.ex b/lib/pleroma/web/metadata/providers/open_graph.ex
index 18ddde84b..78cef1525 100644
--- a/lib/pleroma/web/metadata/providers/open_graph.ex
+++ b/lib/pleroma/web/metadata/providers/open_graph.ex
@@ -69,8 +69,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
Enum.reduce(attachments, [], fn attachment, acc ->
rendered_tags =
Enum.reduce(attachment["url"], [], fn url, acc ->
- # TODO: Add additional properties to objects when we have the data available.
- # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
+ # TODO: 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.
case Utils.fetch_media_type(@media_types, url["mediaType"]) do
"audio" ->
@@ -85,12 +84,14 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
{:meta, [property: "og:image:alt", content: attachment["name"]], []}
| acc
]
+ |> maybe_add_dimensions(url)
"video" ->
[
{:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
| acc
]
+ |> maybe_add_dimensions(url)
_ ->
acc
@@ -102,4 +103,21 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
end
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