Federate emoji out.
authorRoger Braun <roger@rogerbraun.net>
Sat, 16 Sep 2017 14:14:23 +0000 (16:14 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sat, 16 Sep 2017 14:14:23 +0000 (16:14 +0200)
lib/pleroma/formatter.ex
lib/pleroma/web/ostatus/activity_representer.ex
lib/pleroma/web/twitter_api/representers/activity_representer.ex
test/formatter_test.exs
test/support/factory.ex
test/web/ostatus/activity_representer_test.exs

index c3b7bdaee9271f413841380e6e529d658a0e4126..f85eb04b7a5a258980ae67c7b0e498ba0a081eb7 100644 (file)
@@ -95,9 +95,13 @@ defmodule Pleroma.Formatter do
 
   @emoji @finmoji_with_filenames
 
-  def finmojifiy(text) do
+  def emojify(text) do
     Enum.reduce(@emoji, text, fn ({emoji, file}, text) ->
       String.replace(text, ":#{emoji}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{file}' />")
     end)
   end
+
+  def get_emoji(text) do
+    Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end)
+  end
 end
index dda29d3cf60fe664719ccba255b25b7504dcd0b7..2092ab7fa5a81c09817a7ff168e7c5be7969b87b 100644 (file)
@@ -1,6 +1,7 @@
 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
   alias Pleroma.{Activity, User, Object}
   alias Pleroma.Web.OStatus.UserRepresenter
+  alias Pleroma.Formatter
   require Logger
 
   defp get_href(id) do
@@ -55,6 +56,12 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
 
   defp get_links(_activity), do: []
 
+  defp get_emoji_links(content) do
+    Enum.map(Formatter.get_emoji(content), fn({emoji, file}) ->
+      {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist("#{Pleroma.Web.Endpoint.static_url}#{file}")], []}
+    end)
+  end
+
   def to_simple_form(activity, user, with_author \\ false)
   def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
     h = fn(str) -> [to_charlist(str)] end
@@ -74,6 +81,8 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
     categories = (activity.data["object"]["tag"] || [])
     |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
 
+    emoji_links = get_emoji_links(activity.data["object"]["content"] || "")
+
     [
       {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
       {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
@@ -84,7 +93,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
       {:updated, h.(updated_at)},
       {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
       {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
-    ] ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions
+    ] ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links
   end
 
   def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
index 8f7b89175f2da71beb7f1ae3e516882d289cfe25..29a9f3941814b9623588bb5884a60418050c81a0 100644 (file)
@@ -139,7 +139,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
       "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.finmojifiy,
+      "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.emojify,
       "text" => HtmlSanitizeEx.strip_tags(content),
       "is_local" => activity.local,
       "is_post_verb" => true,
index 4b15f408052b3c36eca50e52918cc321a780a6ae..d96f433f93f4f1880c74abd468a628fb80ab9767 100644 (file)
@@ -49,6 +49,12 @@ defmodule Pleroma.FormatterTest do
 
     expected_result = "I love <img height='32px' width='32px' alt='moominmamma' title='moominmamma' src='/finmoji/128px/moominmamma-128.png' />"
 
-    assert Formatter.finmojifiy(text) == expected_result
+    assert Formatter.emojify(text) == expected_result
+  end
+
+  test "it returns the emoji used in the text" do
+    text = "I love :moominmamma:"
+
+    assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
   end
 end
index eca73725dccea2839fb8f2172bbd2c3e7808b491..f499024a2289073cdc86fb1244a9ea0912c18c24 100644 (file)
@@ -13,7 +13,7 @@ defmodule Pleroma.Factory do
   end
 
   def note_factory do
-    text = sequence(:text, &"This is note #{&1}")
+    text = sequence(:text, &"This is :moominmamma: note #{&1}")
 
     user = insert(:user)
     data = %{
index bcf569395fce53109d188ef7ee774f46b03f45fb..8a0121b37414d5065d235458477304da785bdaea 100644 (file)
@@ -40,6 +40,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
     <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
     <category term="2hu"/>
     <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
+    <link name="moominmamma" rel="emoji" href="#{Pleroma.Web.Endpoint.static_url}/finmoji/128px/moominmamma-128.png" />
     """
 
     tuple = ActivityRepresenter.to_simple_form(note_activity, user)
@@ -78,6 +79,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
     <category term="2hu"/>
     <thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" />
     <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
+    <link name="moominmamma" rel="emoji" href="#{Pleroma.Web.Endpoint.static_url}/finmoji/128px/moominmamma-128.png" />
     """
 
     tuple = ActivityRepresenter.to_simple_form(answer, user)