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}
24 @emoji_regex ~r/:[A-Za-z0-9_-]+(@.+)?:/
26 defstruct [:code, :file, :tags, :safe_code, :safe_file]
28 @doc "Build emoji struct"
29 def build({code, file, tags}) do
34 safe_code: Pleroma.HTML.strip_tags(code),
35 safe_file: Pleroma.HTML.strip_tags(file)
39 def build({code, file}), do: build({code, file, []})
43 GenServer.start_link(__MODULE__, [], name: __MODULE__)
46 @doc "Reloads the emojis from disk."
49 GenServer.call(__MODULE__, :reload)
52 @doc "Returns the path of the emoji `name`."
53 @spec get(String.t()) :: String.t() | nil
56 if String.starts_with?(name, ":") do
58 |> String.replace_leading(":", "")
59 |> String.replace_trailing(":", "")
64 case :ets.lookup(@ets, name) do
70 @spec exist?(String.t()) :: boolean()
71 def exist?(name), do: not is_nil(get(name))
73 @doc "Returns all the emojos!!"
74 @spec get_all() :: list({String.t(), String.t(), String.t()})
79 @doc "Clear out old emojis"
80 def clear_all, do: :ets.delete_all_objects(@ets)
84 @ets = :ets.new(@ets, @ets_options)
85 GenServer.cast(self(), :reload)
90 def handle_cast(:reload, state) do
91 update_emojis(Loader.load())
96 def handle_call(:reload, _from, state) do
97 update_emojis(Loader.load())
102 def terminate(_, _) do
107 def code_change(_old_vsn, state, _extra) do
108 update_emojis(Loader.load())
112 defp update_emojis(emojis) do
113 :ets.insert(@ets, emojis)
116 @external_resource "lib/pleroma/emoji-test.txt"
118 regional_indicators =
119 Enum.map(127_462..127_487, fn codepoint ->
126 |> String.split("\n")
127 |> Enum.filter(fn line ->
128 line != "" and not String.starts_with?(line, "#") and
129 String.contains?(line, "fully-qualified")
131 |> Enum.map(fn line ->
133 |> String.split(";", parts: 2)
137 |> Enum.map(fn codepoint ->
138 <<String.to_integer(codepoint, 16)::utf8>>
144 emojis = emojis ++ regional_indicators
146 for emoji <- emojis do
147 def is_unicode_emoji?(unquote(emoji)), do: true
150 def is_unicode_emoji?(_), do: false
152 def stripped_name(name) when is_binary(name) do
154 |> String.replace_leading(":", "")
155 |> String.replace_trailing(":", "")
158 def stripped_name(name), do: name
160 def maybe_quote(name) when is_binary(name) do
161 if is_unicode_emoji?(name) do
164 if String.starts_with?(name, ":") do
172 def maybe_quote(name), do: name
174 def emoji_url(%{"type" => "EmojiReact", "content" => _, "tag" => []}), do: nil
176 def emoji_url(%{"type" => "EmojiReact", "content" => emoji, "tag" => tags}) do
179 |> Enum.find(fn tag -> tag["type"] == "Emoji" && tag["name"] == stripped_name(emoji) end)
190 def emoji_url(_), do: nil
192 def emoji_name_with_instance(name, url) do
193 url = url |> URI.parse() |> Map.get(:host)
197 emoji_qualification_map =
199 |> Enum.filter(&String.contains?(&1, "\uFE0F"))
200 |> Combinations.variate_emoji_qualification()
202 for {qualified, unqualified_list} <- emoji_qualification_map do
203 for unqualified <- unqualified_list do
204 def fully_qualify_emoji(unquote(unqualified)), do: unquote(qualified)
208 def fully_qualify_emoji(emoji), do: emoji
210 def matches_shortcode?(nil), do: false
211 def matches_shortcode?(s), do: Regex.match?(@emoji_regex, s)