1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 @doc "Returns all the emojos!!"
60 @spec get_all() :: list({String.t(), String.t(), String.t()})
65 @doc "Clear out old emojis"
66 def clear_all, do: :ets.delete_all_objects(@ets)
70 @ets = :ets.new(@ets, @ets_options)
71 GenServer.cast(self(), :reload)
76 def handle_cast(:reload, state) do
77 update_emojis(Loader.load())
82 def handle_call(:reload, _from, state) do
83 update_emojis(Loader.load())
88 def terminate(_, _) do
93 def code_change(_old_vsn, state, _extra) do
94 update_emojis(Loader.load())
98 defp update_emojis(emojis) do
99 :ets.insert(@ets, emojis)
102 @external_resource "lib/pleroma/emoji-data.txt"
107 |> String.split("\n")
108 |> Enum.filter(fn line -> line != "" and not String.starts_with?(line, "#") end)
109 |> Enum.map(fn line ->
111 |> String.split(";", parts: 2)
114 |> String.split("..")
117 <<String.to_integer(number, 16)::utf8>>
120 String.to_integer(first, 16)..String.to_integer(last, 16)
121 |> Enum.map(&<<&1::utf8>>)
127 for emoji <- emojis do
128 def is_unicode_emoji?(unquote(emoji)), do: true
131 def is_unicode_emoji?(_), do: false