Merge branch 'develop' into issue/1276
[akkoma] / lib / pleroma / emoji.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 @doc "Returns all the emojos!!"
60 @spec get_all() :: list({String.t(), String.t(), String.t()})
61 def get_all do
62 :ets.tab2list(@ets)
63 end
64
65 @doc "Clear out old emojis"
66 def clear_all, do: :ets.delete_all_objects(@ets)
67
68 @doc false
69 def init(_) do
70 @ets = :ets.new(@ets, @ets_options)
71 GenServer.cast(self(), :reload)
72 {:ok, nil}
73 end
74
75 @doc false
76 def handle_cast(:reload, state) do
77 update_emojis(Loader.load())
78 {:noreply, state}
79 end
80
81 @doc false
82 def handle_call(:reload, _from, state) do
83 update_emojis(Loader.load())
84 {:reply, :ok, state}
85 end
86
87 @doc false
88 def terminate(_, _) do
89 :ok
90 end
91
92 @doc false
93 def code_change(_old_vsn, state, _extra) do
94 update_emojis(Loader.load())
95 {:ok, state}
96 end
97
98 defp update_emojis(emojis) do
99 :ets.insert(@ets, emojis)
100 end
101
102 @external_resource "lib/pleroma/emoji-data.txt"
103
104 emojis =
105 @external_resource
106 |> File.read!()
107 |> String.split("\n")
108 |> Enum.filter(fn line -> line != "" and not String.starts_with?(line, "#") end)
109 |> Enum.map(fn line ->
110 line
111 |> String.split(";", parts: 2)
112 |> hd()
113 |> String.trim()
114 |> String.split("..")
115 |> case do
116 [number] ->
117 <<String.to_integer(number, 16)::utf8>>
118
119 [first, last] ->
120 String.to_integer(first, 16)..String.to_integer(last, 16)
121 |> Enum.map(&<<&1::utf8>>)
122 end
123 end)
124 |> List.flatten()
125 |> Enum.uniq()
126
127 for emoji <- emojis do
128 def is_unicode_emoji?(unquote(emoji)), do: true
129 end
130
131 def is_unicode_emoji?(_), do: false
132 end