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 * emoji packs in INSTANCE-DIR/emoji
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}]
28 GenServer.start_link(__MODULE__, [], name: __MODULE__)
31 @doc "Reloads the emojis from disk."
34 GenServer.call(__MODULE__, :reload)
37 @doc "Returns the path of the emoji `name`."
38 @spec get(String.t()) :: String.t() | nil
40 case :ets.lookup(@ets, name) do
46 @doc "Returns all the emojos!!"
47 @spec get_all() :: [{String.t(), String.t()}, ...]
54 @ets = :ets.new(@ets, @ets_options)
55 GenServer.cast(self(), :reload)
60 def handle_cast(:reload, state) do
66 def handle_call(:reload, _from, state) do
72 def terminate(_, _) do
77 def code_change(_old_vsn, state, _extra) do
85 Pleroma.Config.get!([:instance, :static_dir]),
89 emoji_groups = Pleroma.Config.get([:emoji, :groups])
91 case File.ls(emoji_dir_path) do
93 # The custom emoji directory doesn't exist,
98 # There was some other error
99 Logger.error("Could not access the custom emoji directory #{emoji_dir_path}: #{e}")
103 Enum.group_by(results, fn file -> File.dir?(Path.join(emoji_dir_path, file)) end)
105 packs = grouped[true] || []
106 files = grouped[false] || []
108 # Print the packs we've found
109 Logger.info("Found emoji packs: #{Enum.join(packs, ", ")}")
111 if not Enum.empty?(files) do
113 "Found files in the emoji folder. These will be ignored, please move them to a subdirectory\nFound files: #{
114 Enum.join(files, ", ")
122 fn pack -> load_pack(Path.join(emoji_dir_path, pack), emoji_groups) end
125 # Clear out old emojis
126 :ets.delete_all_objects(@ets)
128 true = :ets.insert(@ets, emojis)
131 # Compat thing for old custom emoji handling & default emoji,
132 # it should run even if there are no emoji packs
133 shortcode_globs = Pleroma.Config.get([:emoji, :shortcode_globs], [])
136 (load_from_file("config/emoji.txt", emoji_groups) ++
137 load_from_file("config/custom_emoji.txt", emoji_groups) ++
138 load_from_globs(shortcode_globs, emoji_groups))
139 |> Enum.reject(fn value -> value == nil end)
141 true = :ets.insert(@ets, emojis)
146 defp load_pack(pack_dir, emoji_groups) do
147 pack_name = Path.basename(pack_dir)
149 pack_file = Path.join(pack_dir, "pack.json")
151 if File.exists?(pack_file) do
152 contents = Jason.decode!(File.read!(pack_file))
155 |> Enum.map(fn {name, rel_file} ->
156 filename = Path.join("/emoji/#{pack_name}", rel_file)
157 {name, filename, pack_name}
160 # Load from emoji.txt / all files
161 emoji_txt = Path.join(pack_dir, "emoji.txt")
163 if File.exists?(emoji_txt) do
164 load_from_file(emoji_txt, emoji_groups)
166 extensions = Pleroma.Config.get([:emoji, :pack_extensions])
169 "No emoji.txt found for pack \"#{pack_name}\", assuming all #{
170 Enum.join(extensions, ", ")
174 make_shortcode_to_file_map(pack_dir, extensions)
175 |> Enum.map(fn {shortcode, rel_file} ->
176 filename = Path.join("/emoji/#{pack_name}", rel_file)
178 {shortcode, filename, [to_string(match_extra(emoji_groups, filename))]}
184 def make_shortcode_to_file_map(pack_dir, exts) do
185 find_all_emoji(pack_dir, exts)
186 |> Enum.map(&Path.relative_to(&1, pack_dir))
187 |> Enum.map(fn f -> {f |> Path.basename() |> Path.rootname(), f} end)
191 def find_all_emoji(dir, exts) do
196 filepath = Path.join(dir, f)
198 if File.dir?(filepath) do
199 acc ++ find_all_emoji(filepath, exts)
205 |> Enum.filter(fn f -> Path.extname(f) in exts end)
208 defp load_from_file(file, emoji_groups) do
209 if File.exists?(file) do
210 load_from_file_stream(File.stream!(file), emoji_groups)
216 defp load_from_file_stream(stream, emoji_groups) do
218 |> Stream.map(&String.trim/1)
219 |> Stream.map(fn line ->
220 case String.split(line, ~r/,\s*/) do
222 {name, file, [to_string(match_extra(emoji_groups, file))]}
224 [name, file | tags] ->
234 defp load_from_globs(globs, emoji_groups) do
235 static_path = Path.join(:code.priv_dir(:pleroma), "static")
238 Enum.map(globs, fn glob ->
239 Path.join(static_path, glob)
244 Enum.map(paths, fn path ->
245 tag = match_extra(emoji_groups, Path.join("/", Path.relative_to(path, static_path)))
246 shortcode = Path.basename(path, Path.extname(path))
247 external_path = Path.join("/", Path.relative_to(path, static_path))
248 {shortcode, external_path, [to_string(tag)]}
253 Finds a matching group for the given emoji filename
255 @spec match_extra(group_patterns(), String.t()) :: atom() | nil
256 def match_extra(group_patterns, filename) do
257 match_group_patterns(group_patterns, fn pattern ->
259 %Regex{} = regex -> Regex.match?(regex, filename)
260 string when is_binary(string) -> filename == string
265 defp match_group_patterns(group_patterns, matcher) do
266 Enum.find_value(group_patterns, fn {group, patterns} ->
270 |> Enum.map(fn pattern ->
271 if String.contains?(pattern, "*") do
272 ~r(#{String.replace(pattern, "*", ".*")})
278 Enum.any?(patterns, matcher) && group