1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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)
24 if scrubbed_content != "" and scrubbed_content != "\u200B" do
25 ": “" <> scrubbed_content <> "”"
30 # Most previews only show og:title which is inconvenient. Instagram
31 # hacks this by putting the description in the title and making the
32 # description longer prefixed by how many likes and shares the post
33 # has. Here we use the descriptive nickname in the title, and expand
34 # the full account & nickname in the description. We also use the cute^Wevil
35 # smart quotes around the status text like Instagram, too.
40 content: "#{user.name}" <> content
42 {:meta, [property: "og:url", content: url], []},
45 property: "og:description",
46 content: "#{Utils.user_name_string(user)}" <> content
48 {:meta, [property: "og:type", content: "website"], []}
50 if attachments == [] or Metadata.activity_nsfw?(object) do
52 {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))],
54 {:meta, [property: "og:image:width", content: 150], []},
55 {:meta, [property: "og:image:height", content: 150], []}
63 def build_tags(%{user: user}) do
64 with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
69 content: Utils.user_name_string(user)
71 {:meta, [property: "og:url", content: User.profile_url(user)], []},
72 {:meta, [property: "og:description", content: truncated_bio], []},
73 {:meta, [property: "og:type", content: "website"], []},
74 {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))], []},
75 {:meta, [property: "og:image:width", content: 150], []},
76 {:meta, [property: "og:image:height", content: 150], []}
81 defp build_attachments(%{data: %{"attachment" => attachments}}) do
82 Enum.reduce(attachments, [], fn attachment, acc ->
84 Enum.reduce(attachment["url"], [], fn url, acc ->
85 # TODO: Add additional properties to objects when we have the data available.
86 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
87 # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
88 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
91 {:meta, [property: "og:audio", content: Utils.attachment_url(url["href"])], []}
97 {:meta, [property: "og:image", content: Utils.attachment_url(url["href"])], []},
98 {:meta, [property: "og:image:width", content: 150], []},
99 {:meta, [property: "og:image:height", content: 150], []}
105 {:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
118 defp build_attachments(_), do: []