a48788969431349cb341e7f27038831e51c9986a
[akkoma] / lib / pleroma / web / metadata / opengraph.ex
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
8
9 @behaviour Provider
10
11 @impl Provider
12 def build_tags(%{activity: %{data: %{"object" => %{"id" => object_id}}} = activity, user: user}) do
13 attachments = build_attachments(activity)
14
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.
21 [
22 {:meta,
23 [
24 property: "og:title",
25 content: "#{user.name}: " <> "“" <> scrub_html_and_truncate(activity) <> "”"
26 ], []},
27 {:meta, [property: "og:url", content: object_id], []},
28 {:meta,
29 [
30 property: "og:description",
31 content: "#{user_name_string(user)}: " <> "“" <> scrub_html_and_truncate(activity) <> "”"
32 ], []},
33 {:meta, [property: "og:type", content: "website"], []}
34 ] ++
35 if attachments == [] do
36 [
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], []}
40 ]
41 else
42 attachments
43 end
44 end
45
46 @impl Provider
47 def build_tags(%{user: user}) do
48 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
49 [
50 {:meta,
51 [
52 property: "og:title",
53 content: user_name_string(user)
54 ], []},
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], []}
61 ]
62 end
63 end
64
65 defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do
66 Enum.reduce(attachments, [], fn attachment, acc ->
67 rendered_tags =
68 Enum.reduce(attachment["url"], [], fn url, acc ->
69 media_type =
70 Enum.find(["image", "audio", "video"], fn media_type ->
71 String.starts_with?(url["mediaType"], media_type)
72 end)
73
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.
77 case media_type do
78 "audio" ->
79 [
80 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
81 | acc
82 ]
83
84 "image" ->
85 [
86 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])],
87 []},
88 {:meta, [property: "og:image:width", content: 150], []},
89 {:meta, [property: "og:image:height", content: 150], []}
90 | acc
91 ]
92
93 "video" ->
94 [
95 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
96 | acc
97 ]
98
99 _ ->
100 acc
101 end
102 end)
103
104 acc ++ rendered_tags
105 end)
106 end
107
108 defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do
109 content
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()
115 end
116
117 defp scrub_html_and_truncate(content) when is_binary(content) do
118 content
119 # html content comes from DB already encoded, decode first and scrub after
120 |> HtmlEntities.decode()
121 |> String.replace(~r/<br\s?\/?>/, " ")
122 |> HTML.strip_tags()
123 |> Formatter.truncate()
124 end
125
126 defp attachment_url(url) do
127 MediaProxy.url(url)
128 end
129
130 defp user_name_string(user) do
131 "#{user.name} " <>
132 if user.local do
133 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
134 else
135 "(@#{user.nickname})"
136 end
137 end
138 end