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.Formatter
9 alias Pleroma.Web.Metadata
10 alias Pleroma.Web.MediaProxy
11 alias Pleroma.Web.Metadata.Providers.Provider
21 attachments = build_attachments(object)
22 scrubbed_content = scrub_html_and_truncate(object)
25 if scrubbed_content != "" and scrubbed_content != "\u200B" do
26 ": “" <> scrubbed_content <> "”"
31 # Most previews only show og:title which is inconvenient. Instagram
32 # hacks this by putting the description in the title and making the
33 # description longer prefixed by how many likes and shares the post
34 # has. Here we use the descriptive nickname in the title, and expand
35 # the full account & nickname in the description. We also use the cute^Wevil
36 # smart quotes around the status text like Instagram, too.
41 content: "#{user.name}" <> content
43 {:meta, [property: "og:url", content: url], []},
46 property: "og:description",
47 content: "#{user_name_string(user)}" <> content
49 {:meta, [property: "og:type", content: "website"], []}
51 if attachments == [] or Metadata.activity_nsfw?(object) do
53 {:meta, [property: "og:image", content: 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 = scrub_html_and_truncate(user.bio || "") do
69 content: 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: 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 ->
86 Enum.find(["image", "audio", "video"], fn media_type ->
87 String.starts_with?(url["mediaType"], media_type)
90 # TODO: Add additional properties to objects when we have the data available.
91 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
92 # object when a Video or GIF is attached it will display that in the Whatsapp Rich Preview.
96 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
102 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])],
104 {:meta, [property: "og:image:width", content: 150], []},
105 {:meta, [property: "og:image:height", content: 150], []}
111 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
124 defp scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
126 # html content comes from DB already encoded, decode first and scrub after
127 |> HtmlEntities.decode()
128 |> String.replace(~r/<br\s?\/?>/, " ")
129 |> HTML.get_cached_stripped_html_for_object(object, __MODULE__)
130 |> Formatter.demojify()
131 |> Formatter.truncate()
134 defp scrub_html_and_truncate(content) when is_binary(content) do
136 # html content comes from DB already encoded, decode first and scrub after
137 |> HtmlEntities.decode()
138 |> String.replace(~r/<br\s?\/?>/, " ")
140 |> Formatter.demojify()
141 |> Formatter.truncate()
144 defp attachment_url(url) do
148 defp user_name_string(user) do
151 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
153 "(@#{user.nickname})"