Resolve merge conflict
[akkoma] / lib / pleroma / formatter.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.Formatter do
6 alias Pleroma.User
7 alias Pleroma.Web.MediaProxy
8 alias Pleroma.HTML
9 alias Pleroma.Emoji
10
11 @tag_regex ~r/((?<=[^&])|\A)(\#)(\w+)/u
12 @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
13
14 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
15 @mentions_regex ~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
16
17 def parse_tags(text, data \\ %{}) do
18 Regex.scan(@tag_regex, text)
19 |> Enum.map(fn ["#" <> tag = full_tag | _] -> {full_tag, String.downcase(tag)} end)
20 |> (fn map ->
21 if data["sensitive"] in [true, "True", "true", "1"],
22 do: [{"#nsfw", "nsfw"}] ++ map,
23 else: map
24 end).()
25 end
26
27 @doc "Parses mentions text and returns list {nickname, user}."
28 @spec parse_mentions(binary()) :: list({binary(), User.t()})
29 def parse_mentions(text) do
30 Regex.scan(@mentions_regex, text)
31 |> List.flatten()
32 |> Enum.uniq()
33 |> Enum.map(fn nickname ->
34 with nickname <- String.trim_leading(nickname, "@"),
35 do: {"@" <> nickname, User.get_cached_by_nickname(nickname)}
36 end)
37 |> Enum.filter(fn {_match, user} -> user end)
38 end
39
40 def emojify(text) do
41 emojify(text, Emoji.get_all())
42 end
43
44 def emojify(text, nil), do: text
45
46 def emojify(text, emoji) do
47 Enum.reduce(emoji, text, fn {emoji, file}, text ->
48 emoji = HTML.strip_tags(emoji)
49 file = HTML.strip_tags(file)
50
51 String.replace(
52 text,
53 ":#{emoji}:",
54 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
55 MediaProxy.url(file)
56 }' />"
57 )
58 |> HTML.filter_tags()
59 end)
60 end
61
62 def get_emoji(text) when is_binary(text) do
63 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
64 end
65
66 def get_emoji(_), do: []
67
68 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
69
70 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
71 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
72
73 # TODO: make it use something other than @link_regex
74 def html_escape(text, "text/html") do
75 HTML.filter_tags(text)
76 end
77
78 def html_escape(text, "text/plain") do
79 Regex.split(@link_regex, text, include_captures: true)
80 |> Enum.map_every(2, fn chunk ->
81 {:safe, part} = Phoenix.HTML.html_escape(chunk)
82 part
83 end)
84 |> Enum.join("")
85 end
86
87 @doc """
88 Escapes a special characters in mention names.
89 """
90 @spec mentions_escape(String.t(), list({String.t(), any()})) :: String.t()
91 def mentions_escape(text, mentions) do
92 mentions
93 |> Enum.reduce(text, fn {name, _}, acc ->
94 escape_name = String.replace(name, @markdown_characters_regex, "\\\\\\1")
95 String.replace(acc, name, escape_name)
96 end)
97 end
98
99 @doc "changes scheme:... urls to html links"
100 def add_links({subs, text}) do
101 links =
102 text
103 |> String.split([" ", "\t", "<br>"])
104 |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
105 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
106 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
107 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
108
109 uuid_text =
110 links
111 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
112
113 subs =
114 subs ++
115 Enum.map(links, fn {uuid, url} ->
116 {uuid, "<a href=\"#{url}\">#{url}</a>"}
117 end)
118
119 {subs, uuid_text}
120 end
121
122 @doc "Adds the links to mentioned users"
123 def add_user_links({subs, text}, mentions) do
124 mentions =
125 mentions
126 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
127 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
128
129 uuid_text =
130 mentions
131 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
132 String.replace(text, match, uuid)
133 end)
134
135 subs =
136 subs ++
137 Enum.map(mentions, fn {match, %User{id: id, ap_id: ap_id, info: info}, uuid} ->
138 ap_id =
139 if is_binary(info.source_data["url"]) do
140 info.source_data["url"]
141 else
142 ap_id
143 end
144
145 short_match = String.split(match, "@") |> tl() |> hd()
146
147 {uuid,
148 "<span><a data-user='#{id}' class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
149 end)
150
151 {subs, uuid_text}
152 end
153
154 @doc "Adds the hashtag links"
155 def add_hashtag_links({subs, text}, tags) do
156 tags =
157 tags
158 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
159 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
160
161 uuid_text =
162 tags
163 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
164 String.replace(text, ~r/((?<=[^&])|(\A))#{match}/, uuid)
165 end)
166
167 subs =
168 subs ++
169 Enum.map(tags, fn {tag_text, tag, uuid} ->
170 url =
171 "<a data-tag='#{tag}' href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{
172 tag_text
173 }</a>"
174
175 {uuid, url}
176 end)
177
178 {subs, uuid_text}
179 end
180
181 def finalize({subs, text}) do
182 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
183 String.replace(result_text, uuid, replacement)
184 end)
185 end
186
187 def truncate(text, opts \\ []) do
188 max_length = opts[:max_length] || 200
189 omission = opts[:omission] || "..."
190
191 cond do
192 not String.valid?(text) ->
193 text
194
195 String.length(text) < max_length ->
196 text
197
198 true ->
199 length_with_omission = max_length - String.length(omission)
200
201 "#{String.slice(text, 0, length_with_omission)}#{omission}"
202 end
203 end
204 end