X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Femoji%2Floader.ex;h=3de2dc7626b9e59bb4fd6d627d1b2ed794720f63;hb=93f64aba78f130cced2fa30dd3475e5c446be9d6;hp=e93b0aeccf19f33da334e007454ed452de5ce919;hpb=cef2e980b1f6b07c2bdb01030559aca83257bd7e;p=akkoma
diff --git a/lib/pleroma/emoji/loader.ex b/lib/pleroma/emoji/loader.ex
index e93b0aecc..3de2dc762 100644
--- a/lib/pleroma/emoji/loader.ex
+++ b/lib/pleroma/emoji/loader.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Emoji.Loader do
@@ -11,13 +11,14 @@ defmodule Pleroma.Emoji.Loader do
* glob paths, nested folder is used as tag name for grouping e.g. priv/static/emoji/custom/nested_folder
"""
alias Pleroma.Config
+ alias Pleroma.Emoji
require Logger
@type pattern :: Regex.t() | module() | String.t()
@type patterns :: pattern() | [pattern()]
@type group_patterns :: keyword(patterns())
- @type emoji :: {String.t(), String.t(), list(String.t())}
+ @type emoji :: {String.t(), Emoji.t()}
@doc """
Loads emojis from files/packs.
@@ -63,9 +64,13 @@ defmodule Pleroma.Emoji.Loader do
)
end
- Enum.flat_map(packs, fn pack ->
- load_pack(Path.join(emoji_dir_path, pack), emoji_groups)
- end)
+ emojis =
+ Enum.flat_map(packs, fn pack ->
+ load_pack(Path.join(emoji_dir_path, pack), emoji_groups)
+ end)
+
+ Emoji.clear_all()
+ emojis
end
# Compat thing for old custom emoji handling & default emoji,
@@ -78,29 +83,46 @@ defmodule Pleroma.Emoji.Loader do
load_from_globs(shortcode_globs, emoji_groups))
|> Enum.reject(fn value -> value == nil end)
- emojis ++ emojis_txt
+ Enum.map(emojis ++ emojis_txt, &prepare_emoji/1)
end
+ defp prepare_emoji({code, _, _} = emoji), do: {code, Emoji.build(emoji)}
+
defp load_pack(pack_dir, emoji_groups) do
pack_name = Path.basename(pack_dir)
- emoji_txt = Path.join(pack_dir, "emoji.txt")
+ pack_file = Path.join(pack_dir, "pack.json")
- if File.exists?(emoji_txt) do
- load_from_file(emoji_txt, emoji_groups)
+ if File.exists?(pack_file) do
+ contents = Jason.decode!(File.read!(pack_file))
+
+ contents["files"]
+ |> Enum.map(fn {name, rel_file} ->
+ filename = Path.join("/emoji/#{pack_name}", rel_file)
+ {name, filename, ["pack:#{pack_name}"]}
+ end)
else
- extensions = Config.get([:emoji, :pack_extensions])
+ # Load from emoji.txt / all files
+ emoji_txt = Path.join(pack_dir, "emoji.txt")
- Logger.info(
- "No emoji.txt found for pack \"#{pack_name}\", assuming all #{Enum.join(extensions, ", ")} files are emoji"
- )
+ if File.exists?(emoji_txt) do
+ load_from_file(emoji_txt, emoji_groups)
+ else
+ extensions = Pleroma.Config.get([:emoji, :pack_extensions])
- make_shortcode_to_file_map(pack_dir, extensions)
- |> Enum.map(fn {shortcode, rel_file} ->
- filename = Path.join("/emoji/#{pack_name}", rel_file)
+ Logger.info(
+ "No emoji.txt found for pack \"#{pack_name}\", assuming all #{
+ Enum.join(extensions, ", ")
+ } files are emoji"
+ )
- {shortcode, filename, [to_string(match_extra(emoji_groups, filename))]}
- end)
+ make_shortcode_to_file_map(pack_dir, extensions)
+ |> Enum.map(fn {shortcode, rel_file} ->
+ filename = Path.join("/emoji/#{pack_name}", rel_file)
+
+ {shortcode, filename, [to_string(match_extra(emoji_groups, filename))]}
+ end)
+ end
end
end
@@ -112,19 +134,17 @@ defmodule Pleroma.Emoji.Loader do
end
def find_all_emoji(dir, exts) do
- Enum.reduce(
- File.ls!(dir),
- [],
- fn f, acc ->
- filepath = Path.join(dir, f)
-
- if File.dir?(filepath) do
- acc ++ find_all_emoji(filepath, exts)
- else
- acc ++ [filepath]
- end
+ dir
+ |> File.ls!()
+ |> Enum.flat_map(fn f ->
+ filepath = Path.join(dir, f)
+
+ if File.dir?(filepath) do
+ find_all_emoji(filepath, exts)
+ else
+ [filepath]
end
- )
+ end)
|> Enum.filter(fn f -> Path.extname(f) in exts end)
end