Apply incoming custom emoji.
authorRoger Braun <roger@rogerbraun.net>
Sat, 16 Sep 2017 14:40:20 +0000 (16:40 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sat, 16 Sep 2017 14:40:20 +0000 (16:40 +0200)
lib/pleroma/formatter.ex
lib/pleroma/web/twitter_api/representers/activity_representer.ex
test/web/twitter_api/representers/activity_representer_test.exs

index f85eb04b7a5a258980ae67c7b0e498ba0a081eb7..e9f4bf06bf0231e089c06d58b09336347525e6aa 100644 (file)
@@ -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}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{file}' />")
     end)
   end
index 29a9f3941814b9623588bb5884a60418050c81a0..929e26bf0ff1dfe3bb0f40bce37ab26e1ff54f07 100644 (file)
@@ -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,
index 500afdac7668742106c009c59d2924f5d8d2aa18..f08644611639154c4e27285fe46f6cc9d98f97aa 100644 (file)
@@ -70,7 +70,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
       }
     }
 
-    content_html = "<script>alert('YAY')</script>Some content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
+    content_html = "<script>alert('YAY')</script>Some :2hu: content mentioning <a href='#{mentioned_user.ap_id}'>@shp</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 <img height='32px' width='32px' alt='2hu' title='2hu' src='corndog.png' /> content mentioning <a href=\"#{mentioned_user.ap_id}\">@shp</a>"
 
     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",