1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Emoji.Loader do
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
18 @type pattern :: Regex.t() | module() | String.t()
19 @type patterns :: pattern() | [pattern()]
20 @type group_patterns :: keyword(patterns())
21 @type emoji :: {String.t(), Emoji.t()}
24 Loads emojis from files/packs.
26 returns list emojis in format:
27 `{"000", "/emoji/freespeechextremist.com/000.png", ["Custom"]}`
29 @spec load() :: list(emoji)
31 emoji_dir_path = Path.join(Config.get!([:instance, :static_dir]), "emoji")
33 emoji_groups = Config.get([:emoji, :groups])
36 case File.ls(emoji_dir_path) do
38 # The custom emoji directory doesn't exist,
43 # There was some other error
44 Logger.error("Could not access the custom emoji directory #{emoji_dir_path}: #{e}")
49 Enum.group_by(results, fn file ->
50 File.dir?(Path.join(emoji_dir_path, file))
53 packs = grouped[true] || []
54 files = grouped[false] || []
56 # Print the packs we've found
57 Logger.info("Found emoji packs: #{Enum.join(packs, ", ")}")
59 if not Enum.empty?(files) do
61 "Found files in the emoji folder. These will be ignored, please move them to a subdirectory\nFound files: #{Enum.join(files, ", ")}"
66 Enum.flat_map(packs, fn pack ->
67 load_pack(Path.join(emoji_dir_path, pack), emoji_groups)
74 # Compat thing for old custom emoji handling & default emoji,
75 # it should run even if there are no emoji packs
76 shortcode_globs = Config.get([:emoji, :shortcode_globs], [])
78 # for testing emoji.txt entries we do not want exposed in normal operation
80 if Application.get_env(:pleroma, __MODULE__)[:test_emoji] do
81 load_from_file("test/config/emoji.txt", emoji_groups)
87 (load_from_file("config/emoji.txt", emoji_groups) ++
88 load_from_file("config/custom_emoji.txt", emoji_groups) ++
89 load_from_globs(shortcode_globs, emoji_groups) ++
91 |> Enum.reject(fn value -> value == nil end)
93 Enum.map(emojis ++ emojis_txt, &prepare_emoji/1)
96 defp prepare_emoji({code, _, _} = emoji), do: {code, Emoji.build(emoji)}
98 defp load_pack(pack_dir, emoji_groups) do
99 pack_name = Path.basename(pack_dir)
101 pack_file = Path.join(pack_dir, "pack.json")
103 if File.exists?(pack_file) do
104 Logger.info("Loading emoji pack from JSON: #{pack_file}")
105 contents = Jason.decode!(File.read!(pack_file))
108 |> Enum.map(fn {name, rel_file} ->
109 filename = Path.join("/emoji/#{pack_name}", rel_file)
110 {name, filename, ["pack:#{pack_name}"]}
113 # Load from emoji.txt / all files
114 emoji_txt = Path.join(pack_dir, "emoji.txt")
116 if File.exists?(emoji_txt) do
117 Logger.info("Loading emoji pack from emoji.txt: #{emoji_txt}")
118 load_from_file(emoji_txt, emoji_groups)
120 extensions = Config.get([:emoji, :pack_extensions])
123 "No emoji.txt found for pack \"#{pack_name}\", assuming all #{Enum.join(extensions, ", ")} files are emoji"
126 make_shortcode_to_file_map(pack_dir, extensions)
127 |> Enum.map(fn {shortcode, rel_file} ->
128 filename = Path.join("/emoji/#{pack_name}", rel_file)
130 {shortcode, filename, [to_string(match_extra(emoji_groups, filename))]}
136 def make_shortcode_to_file_map(pack_dir, exts) do
137 find_all_emoji(pack_dir, exts)
138 |> Enum.map(&Path.relative_to(&1, pack_dir))
139 |> Enum.map(fn f -> {f |> Path.basename() |> Path.rootname(), f} end)
143 def find_all_emoji(dir, exts) do
146 |> Enum.flat_map(fn f ->
147 filepath = Path.join(dir, f)
149 if File.dir?(filepath) do
150 find_all_emoji(filepath, exts)
155 |> Enum.filter(fn f -> Path.extname(f) in exts end)
158 defp load_from_file(file, emoji_groups) do
159 if File.exists?(file) do
160 load_from_file_stream(File.stream!(file), emoji_groups)
166 defp load_from_file_stream(stream, emoji_groups) do
168 |> Stream.map(&String.trim/1)
169 |> Stream.map(fn line ->
170 case String.split(line, ~r/,\s*/) do
172 {name, file, [to_string(match_extra(emoji_groups, file))]}
174 [name, file | tags] ->
184 defp load_from_globs(globs, emoji_groups) do
185 static_path = Path.join(:code.priv_dir(:pleroma), "static")
188 Enum.map(globs, fn glob ->
189 Path.join(static_path, glob)
194 Enum.map(paths, fn path ->
195 tag = match_extra(emoji_groups, Path.join("/", Path.relative_to(path, static_path)))
196 shortcode = Path.basename(path, Path.extname(path))
197 external_path = Path.join("/", Path.relative_to(path, static_path))
198 {shortcode, external_path, [to_string(tag)]}
203 Finds a matching group for the given emoji filename
205 @spec match_extra(group_patterns(), String.t()) :: atom() | nil
206 def match_extra(group_patterns, filename) do
207 match_group_patterns(group_patterns, fn pattern ->
209 %Regex{} = regex -> Regex.match?(regex, filename)
210 string when is_binary(string) -> filename == string
215 defp match_group_patterns(group_patterns, matcher) do
216 Enum.find_value(group_patterns, fn {group, patterns} ->
220 |> Enum.map(fn pattern ->
221 if String.contains?(pattern, "*") do
222 ~r(#{String.replace(pattern, "*", ".*")})
228 Enum.any?(patterns, matcher) && group