1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Emoji do
7 This GenServer stores in an ETS table the list of the loaded emojis,
8 and also allows to reload the list at runtime.
12 alias Pleroma.Emoji.Loader
21 {:read_concurrency, true}
24 defstruct [:code, :file, :tags, :safe_code, :safe_file]
26 @doc "Build emoji struct"
27 def build({code, file, tags}) do
32 safe_code: Pleroma.HTML.strip_tags(code),
33 safe_file: Pleroma.HTML.strip_tags(file)
37 def build({code, file}), do: build({code, file, []})
41 GenServer.start_link(__MODULE__, [], name: __MODULE__)
44 @doc "Reloads the emojis from disk."
47 GenServer.call(__MODULE__, :reload)
50 @doc "Returns the path of the emoji `name`."
51 @spec get(String.t()) :: String.t() | nil
53 case :ets.lookup(@ets, name) do
59 @spec exist?(String.t()) :: boolean()
60 def exist?(name), do: not is_nil(get(name))
62 @doc "Returns all the emojos!!"
63 @spec get_all() :: list({String.t(), String.t(), String.t()})
68 @doc "Clear out old emojis"
69 def clear_all, do: :ets.delete_all_objects(@ets)
73 @ets = :ets.new(@ets, @ets_options)
74 GenServer.cast(self(), :reload)
79 def handle_cast(:reload, state) do
80 update_emojis(Loader.load())
85 def handle_call(:reload, _from, state) do
86 update_emojis(Loader.load())
91 def terminate(_, _) do
96 def code_change(_old_vsn, state, _extra) do
97 update_emojis(Loader.load())
101 defp update_emojis(emojis) do
102 :ets.insert(@ets, emojis)
105 @external_resource "lib/pleroma/emoji-test.txt"
107 regional_indicators =
108 Enum.map(127_462..127_487, fn codepoint ->
115 |> String.split("\n")
116 |> Enum.filter(fn line ->
117 line != "" and not String.starts_with?(line, "#") and
118 String.contains?(line, "fully-qualified")
120 |> Enum.map(fn line ->
122 |> String.split(";", parts: 2)
126 |> Enum.map(fn codepoint ->
127 <<String.to_integer(codepoint, 16)::utf8>>
133 emojis = emojis ++ regional_indicators
135 for emoji <- emojis do
136 def is_unicode_emoji?(unquote(emoji)), do: true
139 def is_unicode_emoji?(_), do: false