From 959e993c7d112d9a30baae3d403687903a7f6a27 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 16 Sep 2017 16:40:20 +0200 Subject: [PATCH] Apply incoming custom emoji. --- lib/pleroma/formatter.ex | 10 ++++++++-- .../twitter_api/representers/activity_representer.ex | 4 +++- .../representers/activity_representer_test.exs | 10 +++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index f85eb04b7..e9f4bf06b 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -95,8 +95,14 @@ defmodule Pleroma.Formatter do @emoji @finmoji_with_filenames - def emojify(text) do - Enum.reduce(@emoji, text, fn ({emoji, file}, text) -> + def emojify(text, additional \\ nil) do + all_emoji = if additional do + @emoji ++ Map.to_list(additional) + else + @emoji + end + + Enum.reduce(all_emoji, text, fn ({emoji, file}, text) -> String.replace(text, ":#{emoji}:", "#{emoji}") end) end diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 29a9f3941..929e26bf0 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -135,11 +135,13 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do tags = activity.data["object"]["tag"] || [] possibly_sensitive = Enum.member?(tags, "nsfw") + html = HtmlSanitizeEx.basic_html(content) |> Formatter.emojify(object["emoji"]) + %{ "id" => activity.id, "uri" => activity.data["object"]["id"], "user" => UserView.render("show.json", %{user: user, for: opts[:for]}), - "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.emojify, + "statusnet_html" => html, "text" => HtmlSanitizeEx.strip_tags(content), "is_local" => activity.local, "is_post_verb" => true, diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 500afdac7..f08644611 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -70,7 +70,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do } } - content_html = "Some content mentioning @shp" + content_html = "Some :2hu: content mentioning @shp" content = HtmlSanitizeEx.strip_tags(content_html) date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601 @@ -99,7 +99,10 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "like_count" => 5, "announcement_count" => 3, "context" => "2hu", - "tag" => ["content", "mentioning", "nsfw"] + "tag" => ["content", "mentioning", "nsfw"], + "emoji" => %{ + "2hu" => "corndog.png" + } }, "published" => date, "context" => "2hu" @@ -107,12 +110,13 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do local: false } + expected_html = "alert('YAY')Some 2hu content mentioning @shp" expected_status = %{ "id" => activity.id, "user" => UserView.render("show.json", %{user: user, for: follower}), "is_local" => false, - "statusnet_html" => HtmlSanitizeEx.basic_html(content_html), + "statusnet_html" => expected_html, "text" => content, "is_post_verb" => true, "created_at" => "Tue May 24 13:26:08 +0000 2016",