Merge branch 'documentation/mastalab' into 'develop'
[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
5 defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
6 alias Pleroma.Web.Metadata.Providers.Provider
7 alias Pleroma.Web.Metadata
8 alias Pleroma.{HTML, Formatter, User}
9 alias Pleroma.Web.MediaProxy
10
11 @behaviour Provider
12
13 @impl Provider
14 def build_tags(%{
15 object: object,
16 url: url,
17 user: user
18 }) do
19 attachments = build_attachments(object)
20 scrubbed_content = scrub_html_and_truncate(object)
21 # Zero width space
22 content =
23 if scrubbed_content != "" and scrubbed_content != "\u200B" do
24 ": “" <> scrubbed_content <> "”"
25 else
26 ""
27 end
28
29 # Most previews only show og:title which is inconvenient. Instagram
30 # hacks this by putting the description in the title and making the
31 # description longer prefixed by how many likes and shares the post
32 # has. Here we use the descriptive nickname in the title, and expand
33 # the full account & nickname in the description. We also use the cute^Wevil
34 # smart quotes around the status text like Instagram, too.
35 [
36 {:meta,
37 [
38 property: "og:title",
39 content: "#{user.name}" <> content
40 ], []},
41 {:meta, [property: "og:url", content: url], []},
42 {:meta,
43 [
44 property: "og:description",
45 content: "#{user_name_string(user)}" <> content
46 ], []},
47 {:meta, [property: "og:type", content: "website"], []}
48 ] ++
49 if attachments == [] or Metadata.activity_nsfw?(object) do
50 [
51 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
52 {:meta, [property: "og:image:width", content: 150], []},
53 {:meta, [property: "og:image:height", content: 150], []}
54 ]
55 else
56 attachments
57 end
58 end
59
60 @impl Provider
61 def build_tags(%{user: user}) do
62 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
63 [
64 {:meta,
65 [
66 property: "og:title",
67 content: user_name_string(user)
68 ], []},
69 {:meta, [property: "og:url", content: User.profile_url(user)], []},
70 {:meta, [property: "og:description", content: truncated_bio], []},
71 {:meta, [property: "og:type", content: "website"], []},
72 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
73 {:meta, [property: "og:image:width", content: 150], []},
74 {:meta, [property: "og:image:height", content: 150], []}
75 ]
76 end
77 end
78
79 defp build_attachments(%{data: %{"attachment" => attachments}}) do
80 Enum.reduce(attachments, [], fn attachment, acc ->
81 rendered_tags =
82 Enum.reduce(attachment["url"], [], fn url, acc ->
83 media_type =
84 Enum.find(["image", "audio", "video"], fn media_type ->
85 String.starts_with?(url["mediaType"], media_type)
86 end)
87
88 # TODO: Add additional properties to objects when we have the data available.
89 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
90 # object when a Video or GIF is attached it will display that in the Whatsapp Rich Preview.
91 case media_type do
92 "audio" ->
93 [
94 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
95 | acc
96 ]
97
98 "image" ->
99 [
100 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])],
101 []},
102 {:meta, [property: "og:image:width", content: 150], []},
103 {:meta, [property: "og:image:height", content: 150], []}
104 | acc
105 ]
106
107 "video" ->
108 [
109 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
110 | acc
111 ]
112
113 _ ->
114 acc
115 end
116 end)
117
118 acc ++ rendered_tags
119 end)
120 end
121
122 defp scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
123 content
124 # html content comes from DB already encoded, decode first and scrub after
125 |> HtmlEntities.decode()
126 |> String.replace(~r/<br\s?\/?>/, " ")
127 |> HTML.get_cached_stripped_html_for_object(object, __MODULE__)
128 |> Formatter.demojify()
129 |> Formatter.truncate()
130 end
131
132 defp scrub_html_and_truncate(content) when is_binary(content) do
133 content
134 # html content comes from DB already encoded, decode first and scrub after
135 |> HtmlEntities.decode()
136 |> String.replace(~r/<br\s?\/?>/, " ")
137 |> HTML.strip_tags()
138 |> Formatter.demojify()
139 |> Formatter.truncate()
140 end
141
142 defp attachment_url(url) do
143 MediaProxy.url(url)
144 end
145
146 defp user_name_string(user) do
147 "#{user.name} " <>
148 if user.local do
149 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
150 else
151 "(@#{user.nickname})"
152 end
153 end
154 end