Merge branch 'develop' into 'feature/staff-discovery-api'
[akkoma] / lib / pleroma / formatter.ex
1 defmodule Pleroma.Formatter do
2 alias Pleroma.User
3 alias Pleroma.Web.MediaProxy
4
5 @tag_regex ~r/\#\w+/u
6 def parse_tags(text, data \\ %{}) do
7 Regex.scan(@tag_regex, text)
8 |> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end)
9 |> (fn map ->
10 if data["sensitive"] in [true, "True", "true", "1"],
11 do: [{"#nsfw", "nsfw"}] ++ map,
12 else: map
13 end).()
14 end
15
16 def parse_mentions(text) do
17 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
18 regex =
19 ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/u
20
21 Regex.scan(regex, text)
22 |> List.flatten()
23 |> Enum.uniq()
24 |> Enum.map(fn "@" <> match = full_match ->
25 {full_match, User.get_cached_by_nickname(match)}
26 end)
27 |> Enum.filter(fn {_match, user} -> user end)
28 end
29
30 @finmoji [
31 "a_trusted_friend",
32 "alandislands",
33 "association",
34 "auroraborealis",
35 "baby_in_a_box",
36 "bear",
37 "black_gold",
38 "christmasparty",
39 "crosscountryskiing",
40 "cupofcoffee",
41 "education",
42 "fashionista_finns",
43 "finnishlove",
44 "flag",
45 "forest",
46 "four_seasons_of_bbq",
47 "girlpower",
48 "handshake",
49 "happiness",
50 "headbanger",
51 "icebreaker",
52 "iceman",
53 "joulutorttu",
54 "kaamos",
55 "kalsarikannit_f",
56 "kalsarikannit_m",
57 "karjalanpiirakka",
58 "kicksled",
59 "kokko",
60 "lavatanssit",
61 "losthopes_f",
62 "losthopes_m",
63 "mattinykanen",
64 "meanwhileinfinland",
65 "moominmamma",
66 "nordicfamily",
67 "out_of_office",
68 "peacemaker",
69 "perkele",
70 "pesapallo",
71 "polarbear",
72 "pusa_hispida_saimensis",
73 "reindeer",
74 "sami",
75 "sauna_f",
76 "sauna_m",
77 "sauna_whisk",
78 "sisu",
79 "stuck",
80 "suomimainittu",
81 "superfood",
82 "swan",
83 "the_cap",
84 "the_conductor",
85 "the_king",
86 "the_voice",
87 "theoriginalsanta",
88 "tomoffinland",
89 "torillatavataan",
90 "unbreakable",
91 "waiting",
92 "white_nights",
93 "woollysocks"
94 ]
95
96 @finmoji_with_filenames Enum.map(@finmoji, fn finmoji ->
97 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
98 end)
99
100 @emoji_from_file (with {:ok, default} <- File.read("config/emoji.txt") do
101 custom =
102 with {:ok, custom} <- File.read("config/custom_emoji.txt") do
103 custom
104 else
105 _e -> ""
106 end
107
108 (default <> "\n" <> custom)
109 |> String.trim()
110 |> String.split(~r/\n+/)
111 |> Enum.map(fn line ->
112 [name, file] = String.split(line, ~r/,\s*/)
113 {name, file}
114 end)
115 else
116 _ -> []
117 end)
118
119 @emoji_from_globs (
120 static_path = Path.join(:code.priv_dir(:pleroma), "static")
121
122 globs =
123 Application.get_env(:pleroma, :emoji, [])
124 |> Keyword.get(:shortcode_globs, [])
125
126 paths =
127 Enum.map(globs, fn glob ->
128 Path.join(static_path, glob)
129 |> Path.wildcard()
130 end)
131 |> Enum.concat()
132
133 Enum.map(paths, fn path ->
134 shortcode = Path.basename(path, Path.extname(path))
135 external_path = Path.join("/", Path.relative_to(path, static_path))
136 {shortcode, external_path}
137 end)
138 )
139
140 @emoji @finmoji_with_filenames ++ @emoji_from_globs ++ @emoji_from_file
141
142 def emojify(text, emoji \\ @emoji)
143 def emojify(text, nil), do: text
144
145 def emojify(text, emoji) do
146 Enum.reduce(emoji, text, fn {emoji, file}, text ->
147 emoji = HtmlSanitizeEx.strip_tags(emoji)
148 file = HtmlSanitizeEx.strip_tags(file)
149
150 String.replace(
151 text,
152 ":#{emoji}:",
153 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
154 MediaProxy.url(file)
155 }' />"
156 )
157 |> HtmlSanitizeEx.basic_html()
158 end)
159 end
160
161 def get_emoji(text) when is_binary(text) do
162 Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
163 end
164
165 def get_emoji(_), do: []
166
167 def get_custom_emoji() do
168 @emoji
169 end
170
171 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
172
173 # IANA got a list https://www.iana.org/assignments/uri-schemes/ but
174 # Stuff like ipfs isn’t in it
175 # There is very niche stuff
176 @uri_schemes [
177 "https://",
178 "http://",
179 "dat://",
180 "dweb://",
181 "gopher://",
182 "ipfs://",
183 "ipns://",
184 "irc:",
185 "ircs:",
186 "magnet:",
187 "mailto:",
188 "mumble:",
189 "ssb://",
190 "xmpp:"
191 ]
192
193 # TODO: make it use something other than @link_regex
194 def html_escape(text) do
195 Regex.split(@link_regex, text, include_captures: true)
196 |> Enum.map_every(2, fn chunk ->
197 {:safe, part} = Phoenix.HTML.html_escape(chunk)
198 part
199 end)
200 |> Enum.join("")
201 end
202
203 @doc "changes scheme:... urls to html links"
204 def add_links({subs, text}) do
205 additionnal_schemes =
206 Application.get_env(:pleroma, :uri_schemes, [])
207 |> Keyword.get(:additionnal_schemes, [])
208
209 links =
210 text
211 |> String.split([" ", "\t", "<br>"])
212 |> Enum.filter(fn word -> String.starts_with?(word, @uri_schemes ++ additionnal_schemes) end)
213 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
214 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
215 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
216
217 uuid_text =
218 links
219 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
220
221 subs =
222 subs ++
223 Enum.map(links, fn {uuid, url} ->
224 {:safe, link} = Phoenix.HTML.Link.link(url, to: url)
225
226 link =
227 link
228 |> IO.iodata_to_binary()
229
230 {uuid, link}
231 end)
232
233 {subs, uuid_text}
234 end
235
236 @doc "Adds the links to mentioned users"
237 def add_user_links({subs, text}, mentions) do
238 mentions =
239 mentions
240 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
241 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
242
243 uuid_text =
244 mentions
245 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
246 String.replace(text, match, uuid)
247 end)
248
249 subs =
250 subs ++
251 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
252 ap_id = info["source_data"]["url"] || ap_id
253
254 short_match = String.split(match, "@") |> tl() |> hd()
255
256 {uuid,
257 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
258 end)
259
260 {subs, uuid_text}
261 end
262
263 @doc "Adds the hashtag links"
264 def add_hashtag_links({subs, text}, tags) do
265 tags =
266 tags
267 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
268 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
269
270 uuid_text =
271 tags
272 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
273 String.replace(text, match, uuid)
274 end)
275
276 subs =
277 subs ++
278 Enum.map(tags, fn {tag_text, tag, uuid} ->
279 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag_text}</a>"
280 {uuid, url}
281 end)
282
283 {subs, uuid_text}
284 end
285
286 def finalize({subs, text}) do
287 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
288 String.replace(result_text, uuid, replacement)
289 end)
290 end
291 end