Fix delete activities not federating
[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.Emoji
7 alias Pleroma.HTML
8 alias Pleroma.User
9 alias Pleroma.Web.MediaProxy
10
11 @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
12 @link_regex ~r{((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+}ui
13
14 @auto_linker_config hashtag: true,
15 hashtag_handler: &Pleroma.Formatter.hashtag_handler/4,
16 mention: true,
17 mention_handler: &Pleroma.Formatter.mention_handler/4
18
19 def mention_handler("@" <> nickname, buffer, opts, acc) do
20 case User.get_cached_by_nickname(nickname) do
21 %User{id: id} = user ->
22 ap_id = get_ap_id(user)
23 nickname_text = get_nickname_text(nickname, opts) |> maybe_escape(opts)
24
25 link =
26 "<span class='h-card'><a data-user='#{id}' class='u-url mention' href='#{ap_id}'>@<span>#{
27 nickname_text
28 }</span></a></span>"
29
30 {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
31
32 _ ->
33 {buffer, acc}
34 end
35 end
36
37 def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
38 tag = String.downcase(tag)
39 url = "#{Pleroma.Web.base_url()}/tag/#{tag}"
40 link = "<a class='hashtag' data-tag='#{tag}' href='#{url}' rel='tag'>#{tag_text}</a>"
41
42 {link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}
43 end
44
45 @doc """
46 Parses a text and replace plain text links with HTML. Returns a tuple with a result text, mentions, and hashtags.
47 """
48 @spec linkify(String.t(), keyword()) ::
49 {String.t(), [{String.t(), User.t()}], [{String.t(), String.t()}]}
50 def linkify(text, options \\ []) do
51 options = options ++ @auto_linker_config
52 acc = %{mentions: MapSet.new(), tags: MapSet.new()}
53 {text, %{mentions: mentions, tags: tags}} = AutoLinker.link_map(text, acc, options)
54
55 {text, MapSet.to_list(mentions), MapSet.to_list(tags)}
56 end
57
58 def emojify(text) do
59 emojify(text, Emoji.get_all())
60 end
61
62 def emojify(text, nil), do: text
63
64 def emojify(text, emoji, strip \\ false) do
65 Enum.reduce(emoji, text, fn {emoji, file}, text ->
66 emoji = HTML.strip_tags(emoji)
67 file = HTML.strip_tags(file)
68
69 html =
70 if not strip do
71 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
72 MediaProxy.url(file)
73 }' />"
74 else
75 ""
76 end
77
78 String.replace(text, ":#{emoji}:", html) |> HTML.filter_tags()
79 end)
80 end
81
82 def demojify(text) do
83 emojify(text, Emoji.get_all(), true)
84 end
85
86 def demojify(text, nil), do: text
87
88 def get_emoji(text) when is_binary(text) do
89 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
90 end
91
92 def get_emoji(_), do: []
93
94 def html_escape({text, mentions, hashtags}, type) do
95 {html_escape(text, type), mentions, hashtags}
96 end
97
98 def html_escape(text, "text/html") do
99 HTML.filter_tags(text)
100 end
101
102 def html_escape(text, "text/plain") do
103 Regex.split(@link_regex, text, include_captures: true)
104 |> Enum.map_every(2, fn chunk ->
105 {:safe, part} = Phoenix.HTML.html_escape(chunk)
106 part
107 end)
108 |> Enum.join("")
109 end
110
111 def truncate(text, max_length \\ 200, omission \\ "...") do
112 # Remove trailing whitespace
113 text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
114
115 if String.length(text) < max_length do
116 text
117 else
118 length_with_omission = max_length - String.length(omission)
119 String.slice(text, 0, length_with_omission) <> omission
120 end
121 end
122
123 defp get_ap_id(%User{info: %{source_data: %{"url" => url}}}) when is_binary(url), do: url
124 defp get_ap_id(%User{ap_id: ap_id}), do: ap_id
125
126 defp get_nickname_text(nickname, %{mentions_format: :full}), do: User.full_nickname(nickname)
127 defp get_nickname_text(nickname, _), do: User.local_nickname(nickname)
128
129 defp maybe_escape(str, %{mentions_escape: true}) do
130 String.replace(str, @markdown_characters_regex, "\\\\\\1")
131 end
132
133 defp maybe_escape(str, _), do: str
134 end