TwApi ActivityView: Add Like rendering.
[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 -> if data["sensitive"] in [true, "True", "true", "1"], do: [{"#nsfw", "nsfw"}] ++ map, else: map end).()
10 end
11
12 def parse_mentions(text) do
13 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
14 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
15
16 Regex.scan(regex, text)
17 |> List.flatten
18 |> Enum.uniq
19 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
20 |> Enum.filter(fn ({_match, user}) -> user end)
21 end
22
23 @finmoji [
24 "a_trusted_friend",
25 "alandislands",
26 "association",
27 "auroraborealis",
28 "baby_in_a_box",
29 "bear",
30 "black_gold",
31 "christmasparty",
32 "crosscountryskiing",
33 "cupofcoffee",
34 "education",
35 "fashionista_finns",
36 "finnishlove",
37 "flag",
38 "forest",
39 "four_seasons_of_bbq",
40 "girlpower",
41 "handshake",
42 "happiness",
43 "headbanger",
44 "icebreaker",
45 "iceman",
46 "joulutorttu",
47 "kaamos",
48 "kalsarikannit_f",
49 "kalsarikannit_m",
50 "karjalanpiirakka",
51 "kicksled",
52 "kokko",
53 "lavatanssit",
54 "losthopes_f",
55 "losthopes_m",
56 "mattinykanen",
57 "meanwhileinfinland",
58 "moominmamma",
59 "nordicfamily",
60 "out_of_office",
61 "peacemaker",
62 "perkele",
63 "pesapallo",
64 "polarbear",
65 "pusa_hispida_saimensis",
66 "reindeer",
67 "sami",
68 "sauna_f",
69 "sauna_m",
70 "sauna_whisk",
71 "sisu",
72 "stuck",
73 "suomimainittu",
74 "superfood",
75 "swan",
76 "the_cap",
77 "the_conductor",
78 "the_king",
79 "the_voice",
80 "theoriginalsanta",
81 "tomoffinland",
82 "torillatavataan",
83 "unbreakable",
84 "waiting",
85 "white_nights",
86 "woollysocks"
87 ]
88
89 @finmoji_with_filenames Enum.map(@finmoji, fn (finmoji) ->
90 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
91 end)
92
93 @emoji_from_file (with {:ok, default} <- File.read("config/emoji.txt") do
94 custom =
95 with {:ok, custom} <- File.read("config/custom_emoji.txt") do
96 custom
97 else
98 _e -> ""
99 end
100 (default <> "\n" <> custom)
101 |> String.trim()
102 |> String.split(~r/\n+/)
103 |> Enum.map(fn(line) ->
104 [name, file] = String.split(line, ~r/,\s*/)
105 {name, file}
106 end)
107 else
108 _ -> []
109 end)
110
111 @emoji @finmoji_with_filenames ++ @emoji_from_file
112
113 def emojify(text, emoji \\ @emoji)
114 def emojify(text, nil), do: text
115 def emojify(text, emoji) do
116 Enum.reduce(emoji, text, fn ({emoji, file}, text) ->
117 emoji = HtmlSanitizeEx.strip_tags(emoji)
118 file = HtmlSanitizeEx.strip_tags(file)
119 String.replace(text, ":#{emoji}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{MediaProxy.url(file)}' />")
120 end)
121 end
122
123 def get_emoji(text) do
124 Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end)
125 end
126
127 def get_custom_emoji() do
128 @emoji
129 end
130
131 @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u
132
133 def html_escape(text) do
134 Regex.split(@link_regex, text, include_captures: true)
135 |> Enum.map_every(2, fn chunk ->
136 {:safe, part} = Phoenix.HTML.html_escape(chunk)
137 part
138 end)
139 |> Enum.join("")
140 end
141
142 @doc "changes http:... links to html links"
143 def add_links({subs, text}) do
144 links = Regex.scan(@link_regex, text)
145 |> Enum.map(fn ([url]) -> {Ecto.UUID.generate, url} end)
146
147 uuid_text = links
148 |> Enum.reduce(text, fn({uuid, url}, acc) -> String.replace(acc, url, uuid) end)
149
150 subs = subs ++ Enum.map(links, fn({uuid, url}) ->
151 {uuid, "<a href='#{url}'>#{url}</a>"}
152 end)
153
154 {subs, uuid_text}
155 end
156
157 @doc "Adds the links to mentioned users"
158 def add_user_links({subs, text}, mentions) do
159 mentions = mentions
160 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
161 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
162
163 uuid_text = mentions
164 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
165 String.replace(text, match, uuid)
166 end)
167
168 subs = subs ++ Enum.map(mentions, fn ({match, %User{ap_id: ap_id}, uuid}) ->
169 short_match = String.split(match, "@") |> tl() |> hd()
170 {uuid, "<span><a href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
171 end)
172
173 {subs, uuid_text}
174 end
175
176 @doc "Adds the hashtag links"
177 def add_hashtag_links({subs, text}, tags) do
178 tags = tags
179 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
180 |> Enum.map(fn({name, short}) -> {name, short, Ecto.UUID.generate} end)
181
182 uuid_text = tags
183 |> Enum.reduce(text, fn ({match, _short, uuid}, text) ->
184 String.replace(text, match, uuid)
185 end)
186
187 subs = subs ++ Enum.map(tags, fn ({_, tag, uuid}) ->
188 url = "<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>##{tag}</a>"
189 {uuid, url}
190 end)
191
192 {subs, uuid_text}
193 end
194
195 def finalize({subs, text}) do
196 Enum.reduce(subs, text, fn({uuid, replacement}, result_text) ->
197 String.replace(result_text, uuid, replacement)
198 end)
199 end
200 end