f95e2442af8e0b26281a3dc0d7bffe256e4a8cf3
[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, Web}
7 alias Pleroma.Web.MediaProxy
8
9 @behaviour Provider
10
11 @impl Provider
12 def build_tags(%{activity: 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:
26 "#{user.name}: " <>
27 "“" <>
28 scrub_html_and_truncate(activity) <>
29 "”"
30 ], []},
31 {:meta, [property: "og:url", content: "#{Web.base_url()}/notice/#{activity.id}"], []},
32 {:meta,
33 [
34 property: "og:description",
35 content:
36 "#{user_name_string(user)}: " <>
37 "“" <>
38 scrub_html_and_truncate(activity) <>
39 "”"
40 ], []},
41 {:meta, [property: "og:type", content: "website"], []}
42 ] ++
43 if attachments == [] do
44 [
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], []}
48 ]
49 else
50 attachments
51 end
52 end
53
54 @impl Provider
55 def build_tags(%{user: user}) do
56 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
57 [
58 {:meta,
59 [
60 property: "og:title",
61 content: user_name_string(user)
62 ], []},
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], []}
69 ]
70 end
71 end
72
73 defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do
74 Enum.reduce(attachments, [], fn attachment, acc ->
75 rendered_tags =
76 Enum.reduce(attachment["url"], [], fn url, acc ->
77 media_type =
78 Enum.find(["image", "audio", "video"], fn media_type ->
79 String.starts_with?(url["mediaType"], media_type)
80 end)
81
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.
85 case media_type do
86 "audio" ->
87 [
88 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
89 | acc
90 ]
91
92 "image" ->
93 [
94 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])],
95 []},
96 {:meta, [property: "og:image:width", content: 150], []},
97 {:meta, [property: "og:image:height", content: 150], []}
98 | acc
99 ]
100
101 "video" ->
102 [
103 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
104 | acc
105 ]
106
107 _ ->
108 acc
109 end
110 end)
111
112 acc ++ rendered_tags
113 end)
114 end
115
116 defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do
117 content
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()
123 end
124
125 defp scrub_html_and_truncate(content) when is_binary(content) do
126 content
127 # html content comes from DB already encoded, decode first and scrub after
128 |> HtmlEntities.decode()
129 |> String.replace(~r/<br\s?\/?>/, " ")
130 |> HTML.strip_tags()
131 |> Formatter.truncate()
132 end
133
134 defp attachment_url(url) do
135 MediaProxy.url(url)
136 end
137
138 defp user_name_string(user) do
139 "#{user.name} " <>
140 if user.local do
141 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
142 else
143 "(@#{user.nickname})"
144 end
145 end
146 end