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, Web}
7 alias Pleroma.Web.MediaProxy
12 def build_tags(%{activity: 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.
28 scrub_html_and_truncate(activity) <>
31 {:meta, [property: "og:url", content: "#{Web.base_url()}/notice/#{activity.id}"], []},
34 property: "og:description",
36 "#{user_name_string(user)}: " <>
38 scrub_html_and_truncate(activity) <>
41 {:meta, [property: "og:type", content: "website"], []}
43 if attachments == [] do
45 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
46 {:meta, [property: "og:image:width", content: 150], []},
47 {:meta, [property: "og:image:height", content: 150], []}
55 def build_tags(%{user: user}) do
56 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
61 content: user_name_string(user)
63 {:meta, [property: "og:url", content: User.profile_url(user)], []},
64 {:meta, [property: "og:description", content: truncated_bio], []},
65 {:meta, [property: "og:type", content: "website"], []},
66 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
67 {:meta, [property: "og:image:width", content: 150], []},
68 {:meta, [property: "og:image:height", content: 150], []}
73 defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do
74 Enum.reduce(attachments, [], fn attachment, acc ->
76 Enum.reduce(attachment["url"], [], fn url, acc ->
78 Enum.find(["image", "audio", "video"], fn media_type ->
79 String.starts_with?(url["mediaType"], media_type)
82 # TODO: Add additional properties to objects when we have the data available.
83 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
84 # object when a Video or GIF is attached it will display that in the Whatsapp Rich Preview.
88 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
94 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])],
96 {:meta, [property: "og:image:width", content: 150], []},
97 {:meta, [property: "og:image:height", content: 150], []}
103 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
116 defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do
118 # html content comes from DB already encoded, decode first and scrub after
119 |> HtmlEntities.decode()
120 |> String.replace(~r/<br\s?\/?>/, " ")
121 |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
122 |> Formatter.truncate()
125 defp scrub_html_and_truncate(content) when is_binary(content) do
127 # html content comes from DB already encoded, decode first and scrub after
128 |> HtmlEntities.decode()
129 |> String.replace(~r/<br\s?\/?>/, " ")
131 |> Formatter.truncate()
134 defp attachment_url(url) do
138 defp user_name_string(user) do
141 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
143 "(@#{user.nickname})"