Merge branch 'hotfix_broken_likes' into 'develop'
[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) do
162 Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
163 end
164
165 def get_custom_emoji() do
166 @emoji
167 end
168
169 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
170
171 # IANA got a list https://www.iana.org/assignments/uri-schemes/ but
172 # Stuff like ipfs isn’t in it
173 # There is very niche stuff
174 @uri_schemes [
175 "https://",
176 "http://",
177 "dat://",
178 "dweb://",
179 "gopher://",
180 "ipfs://",
181 "ipns://",
182 "irc:",
183 "ircs:",
184 "magnet:",
185 "mailto:",
186 "mumble:",
187 "ssb://",
188 "xmpp:"
189 ]
190
191 # TODO: make it use something other than @link_regex
192 def html_escape(text) do
193 Regex.split(@link_regex, text, include_captures: true)
194 |> Enum.map_every(2, fn chunk ->
195 {:safe, part} = Phoenix.HTML.html_escape(chunk)
196 part
197 end)
198 |> Enum.join("")
199 end
200
201 @doc "changes scheme:... urls to html links"
202 def add_links({subs, text}) do
203 additionnal_schemes =
204 Application.get_env(:pleroma, :uri_schemes, [])
205 |> Keyword.get(:additionnal_schemes, [])
206
207 links =
208 text
209 |> String.split([" ", "\t", "<br>"])
210 |> Enum.filter(fn word -> String.starts_with?(word, @uri_schemes ++ additionnal_schemes) end)
211 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
212 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
213 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
214
215 uuid_text =
216 links
217 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
218
219 subs =
220 subs ++
221 Enum.map(links, fn {uuid, url} ->
222 {:safe, link} = Phoenix.HTML.Link.link(url, to: url)
223
224 link =
225 link
226 |> IO.iodata_to_binary()
227
228 {uuid, link}
229 end)
230
231 {subs, uuid_text}
232 end
233
234 @doc "Adds the links to mentioned users"
235 def add_user_links({subs, text}, mentions) do
236 mentions =
237 mentions
238 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
239 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
240
241 uuid_text =
242 mentions
243 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
244 String.replace(text, match, uuid)
245 end)
246
247 subs =
248 subs ++
249 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
250 ap_id = info["source_data"]["url"] || ap_id
251
252 short_match = String.split(match, "@") |> tl() |> hd()
253
254 {uuid,
255 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
256 end)
257
258 {subs, uuid_text}
259 end
260
261 @doc "Adds the hashtag links"
262 def add_hashtag_links({subs, text}, tags) do
263 tags =
264 tags
265 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
266 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
267
268 uuid_text =
269 tags
270 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
271 String.replace(text, match, uuid)
272 end)
273
274 subs =
275 subs ++
276 Enum.map(tags, fn {tag_text, tag, uuid} ->
277 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag_text}</a>"
278 {uuid, url}
279 end)
280
281 {subs, uuid_text}
282 end
283
284 def finalize({subs, text}) do
285 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
286 String.replace(result_text, uuid, replacement)
287 end)
288 end
289 end