Only add local posts to index in activity_pub
[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.Loader
13
14 require Logger
15
16 @ets __MODULE__.Ets
17 @ets_options [
18 :ordered_set,
19 :protected,
20 :named_table,
21 {:read_concurrency, true}
22 ]
23
24 defstruct [:code, :file, :tags, :safe_code, :safe_file]
25
26 @doc "Build emoji struct"
27 def build({code, file, tags}) do
28 %__MODULE__{
29 code: code,
30 file: file,
31 tags: tags,
32 safe_code: Pleroma.HTML.strip_tags(code),
33 safe_file: Pleroma.HTML.strip_tags(file)
34 }
35 end
36
37 def build({code, file}), do: build({code, file, []})
38
39 @doc false
40 def start_link(_) do
41 GenServer.start_link(__MODULE__, [], name: __MODULE__)
42 end
43
44 @doc "Reloads the emojis from disk."
45 @spec reload() :: :ok
46 def reload do
47 GenServer.call(__MODULE__, :reload)
48 end
49
50 @doc "Returns the path of the emoji `name`."
51 @spec get(String.t()) :: String.t() | nil
52 def get(name) do
53 name =
54 if String.starts_with?(name, ":") do
55 name
56 |> String.replace_leading(":", "")
57 |> String.replace_trailing(":", "")
58 else
59 name
60 end
61
62 case :ets.lookup(@ets, name) do
63 [{_, path}] -> path
64 _ -> nil
65 end
66 end
67
68 @spec exist?(String.t()) :: boolean()
69 def exist?(name), do: not is_nil(get(name))
70
71 @doc "Returns all the emojos!!"
72 @spec get_all() :: list({String.t(), String.t(), String.t()})
73 def get_all do
74 :ets.tab2list(@ets)
75 end
76
77 @doc "Clear out old emojis"
78 def clear_all, do: :ets.delete_all_objects(@ets)
79
80 @doc false
81 def init(_) do
82 @ets = :ets.new(@ets, @ets_options)
83 GenServer.cast(self(), :reload)
84 {:ok, nil}
85 end
86
87 @doc false
88 def handle_cast(:reload, state) do
89 update_emojis(Loader.load())
90 {:noreply, state}
91 end
92
93 @doc false
94 def handle_call(:reload, _from, state) do
95 update_emojis(Loader.load())
96 {:reply, :ok, state}
97 end
98
99 @doc false
100 def terminate(_, _) do
101 :ok
102 end
103
104 @doc false
105 def code_change(_old_vsn, state, _extra) do
106 update_emojis(Loader.load())
107 {:ok, state}
108 end
109
110 defp update_emojis(emojis) do
111 :ets.insert(@ets, emojis)
112 end
113
114 @external_resource "lib/pleroma/emoji-test.txt"
115
116 regional_indicators =
117 Enum.map(127_462..127_487, fn codepoint ->
118 <<codepoint::utf8>>
119 end)
120
121 emojis =
122 @external_resource
123 |> File.read!()
124 |> String.split("\n")
125 |> Enum.filter(fn line ->
126 line != "" and not String.starts_with?(line, "#") and
127 String.contains?(line, "qualified")
128 end)
129 |> Enum.map(fn line ->
130 line
131 |> String.split(";", parts: 2)
132 |> hd()
133 |> String.trim()
134 |> String.split()
135 |> Enum.map(fn codepoint ->
136 <<String.to_integer(codepoint, 16)::utf8>>
137 end)
138 |> Enum.join()
139 end)
140 |> Enum.uniq()
141
142 emojis = emojis ++ regional_indicators
143
144 for emoji <- emojis do
145 def is_unicode_emoji?(unquote(emoji)), do: true
146 end
147
148 def is_unicode_emoji?(_), do: false
149
150 def stripped_name(name) when is_binary(name) do
151 name
152 |> String.replace_leading(":", "")
153 |> String.replace_trailing(":", "")
154 end
155
156 def stripped_name(name), do: name
157
158 def maybe_quote(name) when is_binary(name) do
159 if is_unicode_emoji?(name) do
160 name
161 else
162 if String.starts_with?(name, ":") do
163 name
164 else
165 ":#{name}:"
166 end
167 end
168 end
169
170 def maybe_quote(name), do: name
171
172 def emoji_url(%{"type" => "EmojiReact", "content" => _, "tag" => []}), do: nil
173
174 def emoji_url(%{"type" => "EmojiReact", "content" => emoji, "tag" => tags}) do
175 tag =
176 tags
177 |> Enum.find(fn tag -> tag["type"] == "Emoji" && tag["name"] == stripped_name(emoji) end)
178
179 if is_nil(tag) do
180 nil
181 else
182 tag
183 |> Map.get("icon")
184 |> Map.get("url")
185 end
186 end
187
188 def emoji_url(_), do: nil
189 end