From 884006a9e973dff527ec51e08b3d6a99edfdd93d Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 16 Sep 2017 16:14:23 +0200 Subject: [PATCH] Federate emoji out. --- lib/pleroma/formatter.ex | 6 +++++- lib/pleroma/web/ostatus/activity_representer.ex | 11 ++++++++++- .../twitter_api/representers/activity_representer.ex | 2 +- test/formatter_test.exs | 8 +++++++- test/support/factory.ex | 2 +- test/web/ostatus/activity_representer_test.exs | 2 ++ 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index c3b7bdaee..f85eb04b7 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -95,9 +95,13 @@ def parse_mentions(text) 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}:", "#{emoji}") end) end + + def get_emoji(text) do + Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end) + end end diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index dda29d3cf..2092ab7fa 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -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 @@ defp get_links(%{local: false, 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 @@ def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, 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 @@ def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, {: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 diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 8f7b89175..29a9f3941 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -139,7 +139,7 @@ def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = ac "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, diff --git a/test/formatter_test.exs b/test/formatter_test.exs index 4b15f4080..d96f433f9 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -49,6 +49,12 @@ test "it adds cool emoji" do expected_result = "I love moominmamma" - 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 diff --git a/test/support/factory.ex b/test/support/factory.ex index eca73725d..f499024a2 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -13,7 +13,7 @@ def user_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 = %{ diff --git a/test/web/ostatus/activity_representer_test.exs b/test/web/ostatus/activity_representer_test.exs index bcf569395..8a0121b37 100644 --- a/test/web/ostatus/activity_representer_test.exs +++ b/test/web/ostatus/activity_representer_test.exs @@ -40,6 +40,7 @@ test "a note activity" do + """ tuple = ActivityRepresenter.to_simple_form(note_activity, user) @@ -78,6 +79,7 @@ test "a reply note" do + """ tuple = ActivityRepresenter.to_simple_form(answer, user)