1 defmodule Pleroma.Emoji do
3 The emojis are loaded from:
5 * the built-in Finmojis (if enabled in configuration),
6 * the files: `config/emoji.txt` and `config/custom_emoji.txt`
9 This GenServer stores in an ETS table the list of the loaded emojis, and also allows to reload the list at runtime.
13 @ets_options [:set, :protected, :named_table, {:read_concurrency, true}]
17 GenServer.start_link(__MODULE__, [], name: __MODULE__)
20 @doc "Reloads the emojis from disk."
23 GenServer.call(__MODULE__, :reload)
26 @doc "Returns the path of the emoji `name`."
27 @spec get(String.t()) :: String.t() | nil
29 case :ets.lookup(@ets, name) do
35 @doc "Returns all the emojos!!"
36 @spec get_all() :: [{String.t(), String.t()}, ...]
43 @ets = :ets.new(@ets, @ets_options)
44 {:ok, nil, {:continue, :reload}}
48 def handle_continue(:reload, state) do
54 def handle_call(:reload, _from, state) do
60 def terminate(_, _) do
65 def code_change(_old_vsn, state, _extra) do
72 (load_finmoji(Keyword.get(Application.get_env(:pleroma, :instance), :finmoji_enabled)) ++
73 load_from_file("config/emoji.txt") ++
74 load_from_file("config/custom_emoji.txt") ++
76 Keyword.get(Application.get_env(:pleroma, :emoji, []), :shortcode_globs, [])
78 |> Enum.reject(fn value -> value == nil end)
80 true = :ets.insert(@ets, emojis)
100 "four_seasons_of_bbq",
118 "meanwhileinfinland",
126 "pusa_hispida_saimensis",
149 defp load_finmoji(true) do
150 Enum.map(@finmoji, fn finmoji ->
151 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
155 defp load_finmoji(_), do: :ok
157 defp load_from_file(file) do
158 if File.exists?(file) do
159 load_from_file_stream(File.stream!(file))
165 defp load_from_file_stream(stream) do
167 |> Stream.map(&String.strip/1)
168 |> Stream.map(fn line ->
169 case String.split(line, ~r/,\s*/) do
170 [name, file] -> {name, file}
177 defp load_from_globs(globs) do
178 static_path = Path.join(:code.priv_dir(:pleroma), "static")
181 Enum.map(globs, fn glob ->
182 Path.join(static_path, glob)
187 Enum.map(paths, fn path ->
188 shortcode = Path.basename(path, Path.extname(path))
189 external_path = Path.join("/", Path.relative_to(path, static_path))
190 {shortcode, external_path}