1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4 defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
5 alias Pleroma.Web.Metadata.Providers.Provider
6 alias Pleroma.{HTML, Formatter, User}
7 alias Pleroma.Web.MediaProxy
12 def build_tags(%{activity: %{data: %{"object" => %{"id" => object_id}}} = activity, user: user}) do
13 attachments = build_attachments(activity)
15 # Most previews only show og:title which is inconvenient. Instagram
16 # hacks this by putting the description in the title and making the
17 # description longer prefixed by how many likes and shares the post
18 # has. Here we use the descriptive nickname in the title, and expand
19 # the full account & nickname in the description. We also use the cute^Wevil
20 # smart quotes around the status text like Instagram, too.
25 content: "#{user.name}: " <> "“" <> scrub_html_and_truncate(activity) <> "”"
27 {:meta, [property: "og:url", content: object_id], []},
30 property: "og:description",
31 content: "#{user_name_string(user)}: " <> "“" <> scrub_html_and_truncate(activity) <> "”"
33 {:meta, [property: "og:type", content: "website"], []}
35 if attachments == [] do
37 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
38 {:meta, [property: "og:image:width", content: 150], []},
39 {:meta, [property: "og:image:height", content: 150], []}
47 def build_tags(%{user: user}) do
48 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
53 content: user_name_string(user)
55 {:meta, [property: "og:url", content: User.profile_url(user)], []},
56 {:meta, [property: "og:description", content: truncated_bio], []},
57 {:meta, [property: "og:type", content: "website"], []},
58 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
59 {:meta, [property: "og:image:width", content: 150], []},
60 {:meta, [property: "og:image:height", content: 150], []}
65 defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do
66 Enum.reduce(attachments, [], fn attachment, acc ->
68 Enum.reduce(attachment["url"], [], fn url, acc ->
70 Enum.find(["image", "audio", "video"], fn media_type ->
71 String.starts_with?(url["mediaType"], media_type)
74 # TODO: Add additional properties to objects when we have the data available.
75 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
76 # object when a Video or GIF is attached it will display that in the Whatsapp Rich Preview.
80 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
86 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])],
88 {:meta, [property: "og:image:width", content: 150], []},
89 {:meta, [property: "og:image:height", content: 150], []}
95 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
108 defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do
110 # html content comes from DB already encoded, decode first and scrub after
111 |> HtmlEntities.decode()
112 |> String.replace(~r/<br\s?\/?>/, " ")
113 |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
114 |> Formatter.truncate()
117 defp scrub_html_and_truncate(content) when is_binary(content) do
119 # html content comes from DB already encoded, decode first and scrub after
120 |> HtmlEntities.decode()
121 |> String.replace(~r/<br\s?\/?>/, " ")
123 |> Formatter.truncate()
126 defp attachment_url(url) do
130 defp user_name_string(user) do
133 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
135 "(@#{user.nickname})"