1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
7 alias Pleroma.Web.MediaProxy
8 alias Pleroma.Web.Metadata
9 alias Pleroma.Web.Metadata.Providers.Provider
10 alias Pleroma.Web.Metadata.Utils
13 @media_types ["image", "audio", "video"]
21 attachments = build_attachments(object)
22 scrubbed_content = Utils.scrub_html_and_truncate(object)
28 content: Utils.user_name_string(user)
30 {:meta, [property: "og:url", content: url], []},
33 property: "og:description",
34 content: scrubbed_content
36 {:meta, [property: "og:type", content: "article"], []}
38 if attachments == [] or Metadata.activity_nsfw?(object) do
40 {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))], []},
41 {:meta, [property: "og:image:width", content: 150], []},
42 {:meta, [property: "og:image:height", content: 150], []}
50 def build_tags(%{user: user}) do
51 with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
56 content: Utils.user_name_string(user)
58 {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
59 {:meta, [property: "og:description", content: truncated_bio], []},
60 {:meta, [property: "og:type", content: "article"], []},
61 {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))], []},
62 {:meta, [property: "og:image:width", content: 150], []},
63 {:meta, [property: "og:image:height", content: 150], []}
68 defp build_attachments(%{data: %{"attachment" => attachments}}) do
69 Enum.reduce(attachments, [], fn attachment, acc ->
71 Enum.reduce(attachment["url"], [], fn url, acc ->
72 # TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
73 # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
74 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
77 {:meta, [property: "og:audio", content: MediaProxy.url(url["href"])], []}
81 # Not using preview_url for this. It saves bandwidth, but the image dimensions will be wrong.
82 # We generate it on the fly and have no way to capture or analyze the image to get the dimensions.
83 # This can be an issue for apps/FEs rendering images in timelines too, but you can get clever with
84 # the aspect ratio metadata as a workaround.
87 {:meta, [property: "og:image", content: MediaProxy.url(url["href"])], []},
88 {:meta, [property: "og:image:alt", content: attachment["name"]], []}
91 |> maybe_add_dimensions(url)
95 {:meta, [property: "og:video", content: MediaProxy.url(url["href"])], []}
98 |> maybe_add_dimensions(url)
99 |> maybe_add_video_thumbnail(url)
110 defp build_attachments(_), do: []
112 # We can use url["mediaType"] to dynamically fill the metadata
113 defp maybe_add_dimensions(metadata, url) do
114 type = url["mediaType"] |> String.split("/") |> List.first()
117 !is_nil(url["height"]) && !is_nil(url["width"]) ->
120 {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
121 {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
129 defp maybe_add_video_thumbnail(url, metadata) do
131 Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
133 {:meta, [property: "og:image:width", content: "#{url["width"]}"], []},
134 {:meta, [property: "og:image:height", content: "#{url["height"]}"], []},
135 {:meta, [property: "og:image", content: MediaProxy.preview_url(url["href"])], []}