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: "website"], []}
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: "website"], []},
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: Add additional properties to objects when we have the data available.
73 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
74 # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
75 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
78 {:meta, [property: "og:audio", content: Utils.attachment_url(url["href"])], []}
84 {:meta, [property: "og:image", content: Utils.attachment_url(url["href"])], []},
85 {:meta, [property: "og:image:alt", content: attachment["name"]], []}
91 {:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
104 defp build_attachments(_), do: []