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 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.Combinations
13 alias Pleroma.Emoji.Loader
22 {:read_concurrency, true}
25 defstruct [:code, :file, :tags, :safe_code, :safe_file]
27 @doc "Build emoji struct"
28 def build({code, file, tags}) do
33 safe_code: Pleroma.HTML.strip_tags(code),
34 safe_file: Pleroma.HTML.strip_tags(file)
38 def build({code, file}), do: build({code, file, []})
42 GenServer.start_link(__MODULE__, [], name: __MODULE__)
45 @doc "Reloads the emojis from disk."
48 GenServer.call(__MODULE__, :reload)
51 @doc "Returns the path of the emoji `name`."
52 @spec get(String.t()) :: String.t() | nil
54 case :ets.lookup(@ets, name) do
60 @spec exist?(String.t()) :: boolean()
61 def exist?(name), do: not is_nil(get(name))
63 @doc "Returns all the emojos!!"
64 @spec get_all() :: list({String.t(), String.t(), String.t()})
69 @doc "Clear out old emojis"
70 def clear_all, do: :ets.delete_all_objects(@ets)
74 @ets = :ets.new(@ets, @ets_options)
75 GenServer.cast(self(), :reload)
80 def handle_cast(:reload, state) do
81 update_emojis(Loader.load())
86 def handle_call(:reload, _from, state) do
87 update_emojis(Loader.load())
92 def terminate(_, _) do
97 def code_change(_old_vsn, state, _extra) do
98 update_emojis(Loader.load())
102 defp update_emojis(emojis) do
103 :ets.insert(@ets, emojis)
106 @external_resource "lib/pleroma/emoji-test.txt"
108 regional_indicators =
109 Enum.map(127_462..127_487, fn codepoint ->
116 |> String.split("\n")
117 |> Enum.filter(fn line ->
118 line != "" and not String.starts_with?(line, "#") and
119 String.contains?(line, "fully-qualified")
121 |> Enum.map(fn line ->
123 |> String.split(";", parts: 2)
127 |> Enum.map(fn codepoint ->
128 <<String.to_integer(codepoint, 16)::utf8>>
134 emojis = emojis ++ regional_indicators
136 for emoji <- emojis do
137 def is_unicode_emoji?(unquote(emoji)), do: true
140 def is_unicode_emoji?(_), do: false
142 emoji_qualification_map =
144 |> Enum.filter(&String.contains?(&1, "\uFE0F"))
145 |> Combinations.variate_emoji_qualification()
147 for {qualified, unqualified_list} <- emoji_qualification_map do
148 for unqualified <- unqualified_list do
149 def fully_qualify_emoji(unquote(unqualified)), do: unquote(qualified)
153 def fully_qualify_emoji(emoji), do: emoji