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 name = if String.starts_with?(name, ":") do
55 |> String.replace_leading(":", "")
56 |> String.replace_trailing(":", "")
61 case :ets.lookup(@ets, name) do
67 @spec exist?(String.t()) :: boolean()
68 def exist?(name), do: not is_nil(get(name))
70 @doc "Returns all the emojos!!"
71 @spec get_all() :: list({String.t(), String.t(), String.t()})
76 @doc "Clear out old emojis"
77 def clear_all, do: :ets.delete_all_objects(@ets)
81 @ets = :ets.new(@ets, @ets_options)
82 GenServer.cast(self(), :reload)
87 def handle_cast(:reload, state) do
88 update_emojis(Loader.load())
93 def handle_call(:reload, _from, state) do
94 update_emojis(Loader.load())
99 def terminate(_, _) do
104 def code_change(_old_vsn, state, _extra) do
105 update_emojis(Loader.load())
109 defp update_emojis(emojis) do
110 :ets.insert(@ets, emojis)
113 @external_resource "lib/pleroma/emoji-test.txt"
115 regional_indicators =
116 Enum.map(127_462..127_487, fn codepoint ->
123 |> String.split("\n")
124 |> Enum.filter(fn line ->
125 line != "" and not String.starts_with?(line, "#") and
126 String.contains?(line, "qualified")
128 |> Enum.map(fn line ->
130 |> String.split(";", parts: 2)
134 |> Enum.map(fn codepoint ->
135 <<String.to_integer(codepoint, 16)::utf8>>
141 emojis = emojis ++ regional_indicators
143 for emoji <- emojis do
144 def is_unicode_emoji?(unquote(emoji)), do: true
147 def is_unicode_emoji?(_), do: false