Federate emoji out.

This commit is contained in:
Roger Braun 2017-09-16 16:14:23 +02:00
parent 79aeb219d8
commit 884006a9e9
6 changed files with 26 additions and 5 deletions

View file

@ -95,9 +95,13 @@ defmodule Pleroma.Formatter do
@emoji @finmoji_with_filenames @emoji @finmoji_with_filenames
def finmojifiy(text) do def emojify(text) do
Enum.reduce(@emoji, text, fn ({emoji, file}, text) -> Enum.reduce(@emoji, text, fn ({emoji, file}, text) ->
String.replace(text, ":#{emoji}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{file}' />") String.replace(text, ":#{emoji}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{file}' />")
end) end)
end end
def get_emoji(text) do
Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end)
end
end end

View file

@ -1,6 +1,7 @@
defmodule Pleroma.Web.OStatus.ActivityRepresenter do defmodule Pleroma.Web.OStatus.ActivityRepresenter do
alias Pleroma.{Activity, User, Object} alias Pleroma.{Activity, User, Object}
alias Pleroma.Web.OStatus.UserRepresenter alias Pleroma.Web.OStatus.UserRepresenter
alias Pleroma.Formatter
require Logger require Logger
defp get_href(id) do defp get_href(id) do
@ -55,6 +56,12 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
defp get_links(_activity), 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(activity, user, with_author \\ false)
def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
h = fn(str) -> [to_charlist(str)] end h = fn(str) -> [to_charlist(str)] end
@ -74,6 +81,8 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
categories = (activity.data["object"]["tag"] || []) categories = (activity.data["object"]["tag"] || [])
|> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end) |> 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:object-type", ['http://activitystrea.ms/schema/1.0/note']},
{:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']}, {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
@ -84,7 +93,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
{:updated, h.(updated_at)}, {:updated, h.(updated_at)},
{:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])}, {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])},
{:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}, {: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 end
def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do

View file

@ -139,7 +139,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
"id" => activity.id, "id" => activity.id,
"uri" => activity.data["object"]["id"], "uri" => activity.data["object"]["id"],
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}), "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), "text" => HtmlSanitizeEx.strip_tags(content),
"is_local" => activity.local, "is_local" => activity.local,
"is_post_verb" => true, "is_post_verb" => true,

View 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' />" 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
end end

View file

@ -13,7 +13,7 @@ defmodule Pleroma.Factory do
end end
def note_factory do def note_factory do
text = sequence(:text, &"This is note #{&1}") text = sequence(:text, &"This is :moominmamma: note #{&1}")
user = insert(:user) user = insert(:user)
data = %{ data = %{

View file

@ -40,6 +40,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
<link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" /> <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
<category term="2hu"/> <category term="2hu"/>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/> <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) tuple = ActivityRepresenter.to_simple_form(note_activity, user)
@ -78,6 +79,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
<category term="2hu"/> <category term="2hu"/>
<thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" /> <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 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) tuple = ActivityRepresenter.to_simple_form(answer, user)