Remove _misskey_reaction matching (#500)
[akkoma] / lib / pleroma / emoji.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Emoji do
6 @moduledoc """
7 This GenServer stores in an ETS table the list of the loaded emojis,
8 and also allows to reload the list at runtime.
9 """
10 use GenServer
11
12 alias Pleroma.Emoji.Combinations
13 alias Pleroma.Emoji.Loader
14
15 require Logger
16
17 @ets __MODULE__.Ets
18 @ets_options [
19 :ordered_set,
20 :protected,
21 :named_table,
22 {:read_concurrency, true}
23 ]
24 @emoji_regex ~r/:[A-Za-z0-9_-]+(@.+)?:/
25
26 defstruct [:code, :file, :tags, :safe_code, :safe_file]
27
28 @doc "Build emoji struct"
29 def build({code, file, tags}) do
30 %__MODULE__{
31 code: code,
32 file: file,
33 tags: tags,
34 safe_code: Pleroma.HTML.strip_tags(code),
35 safe_file: Pleroma.HTML.strip_tags(file)
36 }
37 end
38
39 def build({code, file}), do: build({code, file, []})
40
41 @doc false
42 def start_link(_) do
43 GenServer.start_link(__MODULE__, [], name: __MODULE__)
44 end
45
46 @doc "Reloads the emojis from disk."
47 @spec reload() :: :ok
48 def reload do
49 GenServer.call(__MODULE__, :reload)
50 end
51
52 @doc "Returns the path of the emoji `name`."
53 @spec get(String.t()) :: String.t() | nil
54 def get(name) do
55 name =
56 if String.starts_with?(name, ":") do
57 name
58 |> String.replace_leading(":", "")
59 |> String.replace_trailing(":", "")
60 else
61 name
62 end
63
64 case :ets.lookup(@ets, name) do
65 [{_, path}] -> path
66 _ -> nil
67 end
68 end
69
70 @spec exist?(String.t()) :: boolean()
71 def exist?(name), do: not is_nil(get(name))
72
73 @doc "Returns all the emojos!!"
74 @spec get_all() :: list({String.t(), String.t(), String.t()})
75 def get_all do
76 :ets.tab2list(@ets)
77 end
78
79 @doc "Clear out old emojis"
80 def clear_all, do: :ets.delete_all_objects(@ets)
81
82 @doc false
83 def init(_) do
84 @ets = :ets.new(@ets, @ets_options)
85 GenServer.cast(self(), :reload)
86 {:ok, nil}
87 end
88
89 @doc false
90 def handle_cast(:reload, state) do
91 update_emojis(Loader.load())
92 {:noreply, state}
93 end
94
95 @doc false
96 def handle_call(:reload, _from, state) do
97 update_emojis(Loader.load())
98 {:reply, :ok, state}
99 end
100
101 @doc false
102 def terminate(_, _) do
103 :ok
104 end
105
106 @doc false
107 def code_change(_old_vsn, state, _extra) do
108 update_emojis(Loader.load())
109 {:ok, state}
110 end
111
112 defp update_emojis(emojis) do
113 :ets.insert(@ets, emojis)
114 end
115
116 @external_resource "lib/pleroma/emoji-test.txt"
117
118 regional_indicators =
119 Enum.map(127_462..127_487, fn codepoint ->
120 <<codepoint::utf8>>
121 end)
122
123 emojis =
124 @external_resource
125 |> File.read!()
126 |> String.split("\n")
127 |> Enum.filter(fn line ->
128 line != "" and not String.starts_with?(line, "#") and
129 String.contains?(line, "fully-qualified")
130 end)
131 |> Enum.map(fn line ->
132 line
133 |> String.split(";", parts: 2)
134 |> hd()
135 |> String.trim()
136 |> String.split()
137 |> Enum.map(fn codepoint ->
138 <<String.to_integer(codepoint, 16)::utf8>>
139 end)
140 |> Enum.join()
141 end)
142 |> Enum.uniq()
143
144 emojis = emojis ++ regional_indicators
145
146 for emoji <- emojis do
147 def is_unicode_emoji?(unquote(emoji)), do: true
148 end
149
150 def is_unicode_emoji?(_), do: false
151
152 def stripped_name(name) when is_binary(name) do
153 name
154 |> String.replace_leading(":", "")
155 |> String.replace_trailing(":", "")
156 end
157
158 def stripped_name(name), do: name
159
160 def maybe_quote(name) when is_binary(name) do
161 if is_unicode_emoji?(name) do
162 name
163 else
164 if String.starts_with?(name, ":") do
165 name
166 else
167 ":#{name}:"
168 end
169 end
170 end
171
172 def maybe_quote(name), do: name
173
174 def emoji_url(%{"type" => "EmojiReact", "content" => _, "tag" => []}), do: nil
175
176 def emoji_url(%{"type" => "EmojiReact", "content" => emoji, "tag" => tags}) do
177 tag =
178 tags
179 |> Enum.find(fn tag -> tag["type"] == "Emoji" && tag["name"] == stripped_name(emoji) end)
180
181 if is_nil(tag) do
182 nil
183 else
184 tag
185 |> Map.get("icon")
186 |> Map.get("url")
187 end
188 end
189
190 def emoji_url(_), do: nil
191
192 def emoji_name_with_instance(name, url) do
193 url = url |> URI.parse() |> Map.get(:host)
194 "#{name}@#{url}"
195 end
196
197 emoji_qualification_map =
198 emojis
199 |> Enum.filter(&String.contains?(&1, "\uFE0F"))
200 |> Combinations.variate_emoji_qualification()
201
202 for {qualified, unqualified_list} <- emoji_qualification_map do
203 for unqualified <- unqualified_list do
204 def fully_qualify_emoji(unquote(unqualified)), do: unquote(qualified)
205 end
206 end
207
208 def fully_qualify_emoji(emoji), do: emoji
209
210 def matches_shortcode?(nil), do: false
211 def matches_shortcode?(s), do: Regex.match?(@emoji_regex, s)
212 end