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.
19 @type pattern :: Regex.t() | module() | String.t()
20 @type patterns :: pattern() | [pattern()]
21 @type group_patterns :: keyword(patterns())
24 @ets_options [:ordered_set, :protected, :named_table, {:read_concurrency, true}]
25 @groups Application.get_env(:pleroma, :emoji)[:groups]
29 GenServer.start_link(__MODULE__, [], name: __MODULE__)
32 @doc "Reloads the emojis from disk."
35 GenServer.call(__MODULE__, :reload)
38 @doc "Returns the path of the emoji `name`."
39 @spec get(String.t()) :: String.t() | nil
41 case :ets.lookup(@ets, name) do
47 @doc "Returns all the emojos!!"
48 @spec get_all() :: [{String.t(), String.t()}, ...]
55 @ets = :ets.new(@ets, @ets_options)
56 GenServer.cast(self(), :reload)
61 def handle_cast(:reload, state) do
67 def handle_call(:reload, _from, state) do
73 def terminate(_, _) do
78 def code_change(_old_vsn, state, _extra) do
84 static_path = Path.join(:code.priv_dir(:pleroma), "static")
89 Pleroma.Config.get!([:instance, :static_dir]),
93 case File.ls(emoji_dir_path) do
95 # The custom emoji directory doesn't exist,
100 # There was some other error
101 Logger.error("Could not access the custom emoji directory #{emoji_dir_path}: #{e}")
104 # Print the packs we've found
105 Logger.info("Found emoji packs: #{Enum.join(packs, ", ")}")
107 # compat thing for old custom emoji handling
108 shortcode_globs = Application.get_env(:pleroma, :emoji)[:shortcode_globs] || []
113 fn pack -> load_pack(Path.join(emoji_dir_path, pack)) end
115 load_from_file("config/emoji.txt") ++
116 load_from_file("config/custom_emoji.txt") ++
117 load_from_globs(shortcode_globs))
118 |> Enum.reject(fn value -> value == nil end)
120 true = :ets.insert(@ets, emojis)
126 defp load_pack(pack_dir) do
127 pack_name = Path.basename(pack_dir)
129 emoji_txt = Path.join(pack_dir, "emoji.txt")
131 if File.exists?(emoji_txt) do
132 load_from_file(emoji_txt)
135 "No emoji.txt found for pack \"#{pack_name}\", assuming all .png files are emoji"
141 Pleroma.Config.get!([:instance, :static_dir]),
146 make_shortcode_to_file_map(pack_dir, [".png"])
147 |> Enum.map(fn {shortcode, rel_file} ->
148 filename = Path.join(common_pack_path, rel_file)
150 # If no tag matches, use the pack name as a tag
151 {shortcode, filename, to_string(match_extra(@groups, filename))}
156 def make_shortcode_to_file_map(pack_dir, exts) do
157 find_all_emoji(pack_dir, exts)
158 |> Enum.map(&Path.relative_to(&1, pack_dir))
159 |> Enum.map(fn f -> {f |> Path.basename() |> Path.rootname(), f} end)
163 def find_all_emoji(dir, exts) do
168 filepath = Path.join(dir, f)
170 if File.dir?(filepath) do
171 acc ++ find_all_emoji(filepath, exts)
177 |> Enum.filter(fn f -> Path.extname(f) in exts end)
180 defp load_from_file(file) do
181 if File.exists?(file) do
182 load_from_file_stream(File.stream!(file))
188 defp load_from_file_stream(stream) do
190 |> Stream.map(&String.trim/1)
191 |> Stream.map(fn line ->
192 case String.split(line, ~r/,\s*/) do
193 [name, file, tags] ->
197 {name, file, to_string(match_extra(@groups, file))}
206 defp load_from_globs(globs) do
207 static_path = Path.join(:code.priv_dir(:pleroma), "static")
210 Enum.map(globs, fn glob ->
211 Path.join(static_path, glob)
216 Enum.map(paths, fn path ->
217 tag = match_extra(@groups, Path.join("/", Path.relative_to(path, static_path)))
218 shortcode = Path.basename(path, Path.extname(path))
219 external_path = Path.join("/", Path.relative_to(path, static_path))
220 {shortcode, external_path, to_string(tag)}
225 Finds a matching group for the given emoji filename
227 @spec match_extra(group_patterns(), String.t()) :: atom() | nil
228 def match_extra(group_patterns, filename) do
229 match_group_patterns(group_patterns, fn pattern ->
231 %Regex{} = regex -> Regex.match?(regex, filename)
232 string when is_binary(string) -> filename == string
237 defp match_group_patterns(group_patterns, matcher) do
238 Enum.find_value(group_patterns, fn {group, patterns} ->
242 |> Enum.map(fn pattern ->
243 if String.contains?(pattern, "*") do
244 ~r(#{String.replace(pattern, "*", ".*")})
250 Enum.any?(patterns, matcher) && group