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`
11 * glob paths, nested folder is used as tag name for grouping e.g. priv/static/emoji/custom/nested_folder
13 This GenServer stores in an ETS table the list of the loaded emojis, and also allows to reload the list at runtime.
17 @type pattern :: Regex.t() | module() | String.t()
18 @type patterns :: pattern | [pattern]
19 @type group_patterns :: keyword(patterns)
22 @ets_options [:ordered_set, :protected, :named_table, {:read_concurrency, true}]
23 @groups Application.get_env(:pleroma, :emoji)[:groups]
27 GenServer.start_link(__MODULE__, [], name: __MODULE__)
30 @doc "Reloads the emojis from disk."
33 GenServer.call(__MODULE__, :reload)
36 @doc "Returns the path of the emoji `name`."
37 @spec get(String.t()) :: String.t() | nil
39 case :ets.lookup(@ets, name) do
45 @doc "Returns all the emojos!!"
46 @spec get_all() :: [{String.t(), String.t()}, ...]
53 @ets = :ets.new(@ets, @ets_options)
54 GenServer.cast(self(), :reload)
59 def handle_cast(:reload, state) do
65 def handle_call(:reload, _from, state) do
71 def terminate(_, _) do
76 def code_change(_old_vsn, state, _extra) do
82 finmoji_enabled = Keyword.get(Application.get_env(:pleroma, :instance), :finmoji_enabled)
83 shortcode_globs = Keyword.get(Application.get_env(:pleroma, :emoji, []), :shortcode_globs, [])
86 (load_finmoji(finmoji_enabled) ++
87 load_from_file("config/emoji.txt") ++
88 load_from_file("config/custom_emoji.txt") ++
89 load_from_globs(shortcode_globs))
90 |> Enum.reject(fn value -> value == nil end)
92 true = :ets.insert(@ets, emojis)
105 "crosscountryskiing",
112 "four_seasons_of_bbq",
130 "meanwhileinfinland",
138 "pusa_hispida_saimensis",
162 defp load_finmoji(true) do
163 Enum.map(@finmoji, fn finmoji ->
164 file_name = "/finmoji/128px/#{finmoji}-128.png"
165 group = match_extra(@groups, file_name)
166 {finmoji, file_name, to_string(group)}
170 defp load_finmoji(_), do: []
172 defp load_from_file(file) do
173 if File.exists?(file) do
174 load_from_file_stream(File.stream!(file))
180 defp load_from_file_stream(stream) do
182 |> Stream.map(&String.trim/1)
183 |> Stream.map(fn line ->
184 case String.split(line, ~r/,\s*/) do
185 [name, file, tags] ->
189 {name, file, to_string(match_extra(@groups, file))}
198 defp load_from_globs(globs) do
199 static_path = Path.join(:code.priv_dir(:pleroma), "static")
202 Enum.map(globs, fn glob ->
203 Path.join(static_path, glob)
208 Enum.map(paths, fn path ->
209 tag = match_extra(@groups, Path.join("/", Path.relative_to(path, static_path)))
210 shortcode = Path.basename(path, Path.extname(path))
211 external_path = Path.join("/", Path.relative_to(path, static_path))
212 {shortcode, external_path, to_string(tag)}
217 Finds a matching group for the given extra filename
219 @spec match_extra(group_patterns(), String.t()) :: atom() | nil
220 def match_extra(group_patterns, filename) do
221 match_group_patterns(group_patterns, fn pattern ->
223 %Regex{} = regex -> Regex.match?(regex, filename)
224 string when is_binary(string) -> filename == string
229 defp match_group_patterns(group_patterns, matcher) do
230 Enum.find_value(group_patterns, fn {group, patterns} ->
234 |> Enum.map(fn pattern ->
235 if String.contains?(pattern, "*") do
236 ~r(#{String.replace(pattern, "*", ".*")})
242 Enum.any?(patterns, matcher) && group