Merge branch 'apps-api-endpoint' into 'develop'
[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 case :ets.lookup(@ets, name) do
54 [{_, path}] -> path
55 _ -> nil
56 end
57 end
58
59 @spec exist?(String.t()) :: boolean()
60 def exist?(name), do: not is_nil(get(name))
61
62 @doc "Returns all the emojos!!"
63 @spec get_all() :: list({String.t(), String.t(), String.t()})
64 def get_all do
65 :ets.tab2list(@ets)
66 end
67
68 @doc "Clear out old emojis"
69 def clear_all, do: :ets.delete_all_objects(@ets)
70
71 @doc false
72 def init(_) do
73 @ets = :ets.new(@ets, @ets_options)
74 GenServer.cast(self(), :reload)
75 {:ok, nil}
76 end
77
78 @doc false
79 def handle_cast(:reload, state) do
80 update_emojis(Loader.load())
81 {:noreply, state}
82 end
83
84 @doc false
85 def handle_call(:reload, _from, state) do
86 update_emojis(Loader.load())
87 {:reply, :ok, state}
88 end
89
90 @doc false
91 def terminate(_, _) do
92 :ok
93 end
94
95 @doc false
96 def code_change(_old_vsn, state, _extra) do
97 update_emojis(Loader.load())
98 {:ok, state}
99 end
100
101 defp update_emojis(emojis) do
102 :ets.insert(@ets, emojis)
103 end
104
105 @external_resource "lib/pleroma/emoji-test.txt"
106
107 regional_indicators =
108 Enum.map(127_462..127_487, fn codepoint ->
109 <<codepoint::utf8>>
110 end)
111
112 emojis =
113 @external_resource
114 |> File.read!()
115 |> String.split("\n")
116 |> Enum.filter(fn line ->
117 line != "" and not String.starts_with?(line, "#") and
118 String.contains?(line, "fully-qualified")
119 end)
120 |> Enum.map(fn line ->
121 line
122 |> String.split(";", parts: 2)
123 |> hd()
124 |> String.trim()
125 |> String.split()
126 |> Enum.map(fn codepoint ->
127 <<String.to_integer(codepoint, 16)::utf8>>
128 end)
129 |> Enum.join()
130 end)
131 |> Enum.uniq()
132
133 emojis = emojis ++ regional_indicators
134
135 for emoji <- emojis do
136 def is_unicode_emoji?(unquote(emoji)), do: true
137 end
138
139 def is_unicode_emoji?(_), do: false
140 end