1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
19 attachments = build_attachments(object)
20 scrubbed_content = Utils.scrub_html_and_truncate(object)
23 if scrubbed_content != "" and scrubbed_content != "\u200B" do
24 ": “" <> scrubbed_content <> "”"
29 # Most previews only show og:title which is inconvenient. Instagram
30 # hacks this by putting the description in the title and making the
31 # description longer prefixed by how many likes and shares the post
32 # has. Here we use the descriptive nickname in the title, and expand
33 # the full account & nickname in the description. We also use the cute^Wevil
34 # smart quotes around the status text like Instagram, too.
39 content: "#{user.name}" <> content
41 {:meta, [property: "og:url", content: url], []},
44 property: "og:description",
45 content: "#{Utils.user_name_string(user)}" <> content
47 {:meta, [property: "og:type", content: "website"], []}
49 if attachments == [] or Metadata.activity_nsfw?(object) do
51 {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))],
53 {:meta, [property: "og:image:width", content: 150], []},
54 {:meta, [property: "og:image:height", content: 150], []}
62 def build_tags(%{user: user}) do
63 with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
68 content: Utils.user_name_string(user)
70 {:meta, [property: "og:url", content: User.profile_url(user)], []},
71 {:meta, [property: "og:description", content: truncated_bio], []},
72 {:meta, [property: "og:type", content: "website"], []},
73 {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))], []},
74 {:meta, [property: "og:image:width", content: 150], []},
75 {:meta, [property: "og:image:height", content: 150], []}
80 defp build_attachments(%{data: %{"attachment" => attachments}}) do
81 Enum.reduce(attachments, [], fn attachment, acc ->
83 Enum.reduce(attachment["url"], [], fn url, acc ->
85 Enum.find(["image", "audio", "video"], fn media_type ->
86 String.starts_with?(url["mediaType"], media_type)
89 # TODO: Add additional properties to objects when we have the data available.
90 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
91 # object when a Video or GIF is attached it will display that in the Whatsapp Rich Preview.
96 [property: "og:" <> media_type, content: Utils.attachment_url(url["href"])], []}
102 {:meta, [property: "og:" <> media_type, content: Utils.attachment_url(url["href"])],
104 {:meta, [property: "og:image:width", content: 150], []},
105 {:meta, [property: "og:image:height", content: 150], []}
112 [property: "og:" <> media_type, content: Utils.attachment_url(url["href"])], []}