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))],
42 {:meta, [property: "og:image:width", content: 150], []},
43 {:meta, [property: "og:image:height", content: 150], []}
51 def build_tags(%{user: user}) do
52 with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
57 content: Utils.user_name_string(user)
59 {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
60 {:meta, [property: "og:description", content: truncated_bio], []},
61 {:meta, [property: "og:type", content: "article"], []},
62 {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
64 {:meta, [property: "og:image:width", content: 150], []},
65 {:meta, [property: "og:image:height", content: 150], []}
70 defp build_attachments(%{data: %{"attachment" => attachments}}) do
71 Enum.reduce(attachments, [], fn attachment, acc ->
73 Enum.reduce(attachment["url"], [], fn url, acc ->
74 # TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
75 # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
76 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
79 {:meta, [property: "og:audio", content: MediaProxy.url(url["href"])], []}
83 # Not using preview_url for this. It saves bandwidth, but the image dimensions will
84 # be wrong. We generate it on the fly and have no way to capture or analyze the
85 # image to get the dimensions. This can be an issue for apps/FEs rendering images
86 # in timelines too, but you can get clever with the aspect ratio metadata as a
90 {:meta, [property: "og:image", content: MediaProxy.url(url["href"])], []},
91 {:meta, [property: "og:image:alt", content: attachment["name"]], []}
94 |> maybe_add_dimensions(url)
98 {:meta, [property: "og:video", content: MediaProxy.url(url["href"])], []}
101 |> maybe_add_dimensions(url)
102 |> maybe_add_video_thumbnail(url)
113 defp build_attachments(_), do: []
115 # We can use url["mediaType"] to dynamically fill the metadata
116 defp maybe_add_dimensions(metadata, url) do
117 type = url["mediaType"] |> String.split("/") |> List.first()
120 !is_nil(url["height"]) && !is_nil(url["width"]) ->
123 {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
124 {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
132 # Media Preview Proxy makes thumbnails of videos without resizing, so we can trust the
133 # width and height of the source video.
134 defp maybe_add_video_thumbnail(metadata, url) do
136 Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
139 {:meta, [property: "og:image:width", content: "#{url["width"]}"], []},
140 {:meta, [property: "og:image:height", content: "#{url["height"]}"], []},
141 {:meta, [property: "og:image", content: MediaProxy.preview_url(url["href"])], []}