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.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
55 if String.starts_with?(name, ":") do
57 |> String.replace_leading(":", "")
58 |> String.replace_trailing(":", "")
63 case :ets.lookup(@ets, name) do
69 @spec exist?(String.t()) :: boolean()
70 def exist?(name), do: not is_nil(get(name))
72 @doc "Returns all the emojos!!"
73 @spec get_all() :: list({String.t(), String.t(), String.t()})
78 @doc "Clear out old emojis"
79 def clear_all, do: :ets.delete_all_objects(@ets)
83 @ets = :ets.new(@ets, @ets_options)
84 GenServer.cast(self(), :reload)
89 def handle_cast(:reload, state) do
90 update_emojis(Loader.load())
95 def handle_call(:reload, _from, state) do
96 update_emojis(Loader.load())
101 def terminate(_, _) do
106 def code_change(_old_vsn, state, _extra) do
107 update_emojis(Loader.load())
111 defp update_emojis(emojis) do
112 :ets.insert(@ets, emojis)
115 @external_resource "lib/pleroma/emoji-test.txt"
117 regional_indicators =
118 Enum.map(127_462..127_487, fn codepoint ->
125 |> String.split("\n")
126 |> Enum.filter(fn line ->
127 line != "" and not String.starts_with?(line, "#") and
128 String.contains?(line, "fully-qualified")
130 |> Enum.map(fn line ->
132 |> String.split(";", parts: 2)
136 |> Enum.map(fn codepoint ->
137 <<String.to_integer(codepoint, 16)::utf8>>
143 emojis = emojis ++ regional_indicators
145 for emoji <- emojis do
146 def is_unicode_emoji?(unquote(emoji)), do: true
149 def is_unicode_emoji?(_), do: false
151 def stripped_name(name) when is_binary(name) do
153 |> String.replace_leading(":", "")
154 |> String.replace_trailing(":", "")
157 def stripped_name(name), do: name
159 def maybe_quote(name) when is_binary(name) do
160 if is_unicode_emoji?(name) do
163 if String.starts_with?(name, ":") do
171 def maybe_quote(name), do: name
173 def emoji_url(%{"type" => "EmojiReact", "content" => _, "tag" => []}), do: nil
175 def emoji_url(%{"type" => "EmojiReact", "content" => emoji, "tag" => tags}) do
178 |> Enum.find(fn tag -> tag["type"] == "Emoji" && tag["name"] == stripped_name(emoji) end)
189 def emoji_url(_), do: nil
191 def emoji_name_with_instance(name, url) do
192 url = url |> URI.parse() |> Map.get(:host)
196 emoji_qualification_map =
198 |> Enum.filter(&String.contains?(&1, "\uFE0F"))
199 |> Combinations.variate_emoji_qualification()
201 for {qualified, unqualified_list} <- emoji_qualification_map do
202 for unqualified <- unqualified_list do
203 def fully_qualify_emoji(unquote(unqualified)), do: unquote(qualified)
207 def fully_qualify_emoji(emoji), do: emoji