1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Emoji do
7 The emojis are loaded from:
9 * the built-in Finmojis (if enabled in configuration),
10 * the files: `config/emoji.txt` and `config/custom_emoji.txt`
13 This GenServer stores in an ETS table the list of the loaded emojis, and also allows to reload the list at runtime.
17 @ets_options [:ordered_set, :protected, :named_table, {:read_concurrency, true}]
21 GenServer.start_link(__MODULE__, [], name: __MODULE__)
24 @doc "Reloads the emojis from disk."
27 GenServer.call(__MODULE__, :reload)
30 @doc "Returns the path of the emoji `name`."
31 @spec get(String.t()) :: String.t() | nil
33 case :ets.lookup(@ets, name) do
39 @doc "Returns all the emojos!!"
40 @spec get_all() :: [{String.t(), String.t()}, ...]
47 @ets = :ets.new(@ets, @ets_options)
48 GenServer.cast(self(), :reload)
53 def handle_cast(:reload, state) do
59 def handle_call(:reload, _from, state) do
65 def terminate(_, _) do
70 def code_change(_old_vsn, state, _extra) do
77 (load_finmoji(Keyword.get(Application.get_env(:pleroma, :instance), :finmoji_enabled)) ++
78 load_from_file("config/emoji.txt") ++
79 load_from_file("config/custom_emoji.txt") ++
81 Keyword.get(Application.get_env(:pleroma, :emoji, []), :shortcode_globs, [])
83 |> Enum.reject(fn value -> value == nil end)
85 true = :ets.insert(@ets, emojis)
105 "four_seasons_of_bbq",
123 "meanwhileinfinland",
131 "pusa_hispida_saimensis",
154 defp load_finmoji(true) do
155 Enum.map(@finmoji, fn finmoji ->
156 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
160 defp load_finmoji(_), do: []
162 defp load_from_file(file) do
163 if File.exists?(file) do
164 load_from_file_stream(File.stream!(file))
170 defp load_from_file_stream(stream) do
172 |> Stream.map(&String.trim/1)
173 |> Stream.map(fn line ->
174 case String.split(line, ~r/,\s*/) do
175 [name, file] -> {name, file}
182 defp load_from_globs(globs) do
183 static_path = Path.join(:code.priv_dir(:pleroma), "static")
186 Enum.map(globs, fn glob ->
187 Path.join(static_path, glob)
192 Enum.map(paths, fn path ->
193 shortcode = Path.basename(path, Path.extname(path))
194 external_path = Path.join("/", Path.relative_to(path, static_path))
195 {shortcode, external_path}