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.Metadata
8 alias Pleroma.Web.Metadata.Providers.Provider
9 alias Pleroma.Web.Metadata.Utils
12 @media_types ["image", "audio", "video"]
20 attachments = build_attachments(object)
21 scrubbed_content = Utils.scrub_html_and_truncate(object)
27 content: Utils.user_name_string(user)
29 {:meta, [property: "og:url", content: url], []},
32 property: "og:description",
33 content: scrubbed_content
35 {:meta, [property: "og:type", content: "article"], []}
37 if attachments == [] or Metadata.activity_nsfw?(object) do
39 {:meta, [property: "og:image", content: Utils.attachment_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: Utils.attachment_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: Utils.attachment_url(url["href"])], []}
83 {:meta, [property: "og:image", content: Utils.attachment_url(url["href"])], []},
84 {:meta, [property: "og:image:alt", content: attachment["name"]], []}
87 |> maybe_add_dimensions(url)
91 {:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
94 |> maybe_add_dimensions(url)
105 defp build_attachments(_), do: []
107 # We can use url["mediaType"] to dynamically fill the metadata
108 defp maybe_add_dimensions(metadata, url) do
109 type = url["mediaType"] |> String.split("/") |> List.first()
112 !is_nil(url["height"]) && !is_nil(url["width"]) ->
115 {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
116 {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}