From c2d1a5e9c4f4ef316a2833914c8f134c00c95b75 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 7 Aug 2018 21:56:50 +0300 Subject: [PATCH 1/4] MastoAPI AccountView: render profile emoji. --- lib/pleroma/web/mastodon_api/views/account_view.ex | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index f33d615cf..cc5261616 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -14,6 +14,18 @@ def render("account.json", %{user: user}) do header = User.banner_url(user) |> MediaProxy.url() user_info = User.user_info(user) + emojis = + (user.info["source_data"]["tag"] || []) + |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end) + |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} -> + %{ + "shortcode" => String.trim(name, ":"), + "url" => MediaProxy.url(url), + "static_url" => MediaProxy.url(url), + "visible_in_picker" => false + } + end) + %{ id: to_string(user.id), username: hd(String.split(user.nickname, "@")), @@ -30,7 +42,7 @@ def render("account.json", %{user: user}) do avatar_static: image, header: header, header_static: header, - emojis: [], + emojis: emojis, fields: [], source: %{ note: "", From cee63ad3f725a90fdd1a438520c33377cee8ad81 Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 8 Aug 2018 08:38:25 +0300 Subject: [PATCH 2/4] TwitterAPI user view: add screen_name_html and description_html. --- lib/pleroma/web/twitter_api/views/user_view.ex | 14 +++++++++++++- test/web/twitter_api/views/user_view_test.exs | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 9c8460378..30cf266bd 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -1,6 +1,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do use Pleroma.Web, :view alias Pleroma.User + alias Pleroma.Formatter alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.MediaProxy @@ -28,9 +29,19 @@ def render("user.json", %{user: user = %User{}} = assigns) do user_info = User.get_cached_user_info(user) + emoji = + (user.info["source_data"]["tag"] || []) + |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end) + |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} -> + {String.trim(name, ":"), url} + end) + + bio = HtmlSanitizeEx.strip_tags(user.bio) + data = %{ "created_at" => user.inserted_at |> Utils.format_naive_asctime(), - "description" => HtmlSanitizeEx.strip_tags(user.bio), + "description" => bio, + "description_html" => bio |> Formatter.emojify(emoji), "favourites_count" => 0, "followers_count" => user_info[:follower_count], "following" => following, @@ -47,6 +58,7 @@ def render("user.json", %{user: user = %User{}} = assigns) do "delete_others_notice" => !!user.info["is_moderator"] }, "screen_name" => user.nickname, + "screen_name_html" => Formatter.emojify(user.nickname, emoji), "statuses_count" => user_info[:note_count], "statusnet_profile_url" => user.ap_id, "cover_photo" => User.banner_url(user) |> MediaProxy.url(), diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 49f73c2fe..000c589af 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -40,7 +40,9 @@ test "A user" do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, + "screen_name_html" => user.nickname, "description" => HtmlSanitizeEx.strip_tags(user.bio), + "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 1, @@ -77,7 +79,9 @@ test "A user for a given other follower", %{user: user} do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, + "screen_name_html" => user.nickname, "description" => HtmlSanitizeEx.strip_tags(user.bio), + "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, @@ -115,7 +119,9 @@ test "A user that follows you", %{user: user} do "id" => follower.id, "name" => follower.name, "screen_name" => follower.nickname, + "screen_name_html" => follower.nickname, "description" => HtmlSanitizeEx.strip_tags(follower.bio), + "description_html" => HtmlSanitizeEx.strip_tags(follower.bio), "created_at" => follower.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, @@ -160,7 +166,9 @@ test "A blocked user for the blocker" do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, + "screen_name_html" => user.nickname, "description" => HtmlSanitizeEx.strip_tags(user.bio), + "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, From ed9738e031e02a9338bedd3a8f3ff73329c101e7 Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 8 Aug 2018 09:24:50 +0300 Subject: [PATCH 3/4] Add tests for emoji in user profiles Also use the correct field in TwitterAPI... --- .../web/twitter_api/views/user_view.ex | 2 +- test/web/mastodon_api/account_view_test.exs | 22 +++++++++++-- test/web/twitter_api/views/user_view_test.exs | 32 ++++++++++++++++--- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 30cf266bd..30aaaf1ce 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -50,6 +50,7 @@ def render("user.json", %{user: user = %User{}} = assigns) do "friends_count" => user_info[:following_count], "id" => user.id, "name" => user.name, + "name_html" => Formatter.emojify(user.name, emoji), "profile_image_url" => image, "profile_image_url_https" => image, "profile_image_url_profile_size" => image, @@ -58,7 +59,6 @@ def render("user.json", %{user: user = %User{}} = assigns) do "delete_others_notice" => !!user.info["is_moderator"] }, "screen_name" => user.nickname, - "screen_name_html" => Formatter.emojify(user.nickname, emoji), "statuses_count" => user_info[:note_count], "statusnet_profile_url" => user.ap_id, "cover_photo" => User.banner_url(user) |> MediaProxy.url(), diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs index b93418b3f..8bf194e6b 100644 --- a/test/web/mastodon_api/account_view_test.exs +++ b/test/web/mastodon_api/account_view_test.exs @@ -5,10 +5,21 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do alias Pleroma.User test "Represent a user account" do + source_data = %{ + "tag" => [ + %{ + "type" => "Emoji", + "icon" => %{"url" => "/file.png"}, + "name" => ":karjalanpiirakka:" + } + ] + } + user = insert(:user, %{ - info: %{"note_count" => 5, "follower_count" => 3}, + info: %{"note_count" => 5, "follower_count" => 3, "source_data" => source_data}, nickname: "shp@shitposter.club", + name: ":karjalanpiirakka: shp", inserted_at: ~N[2017-08-15 15:47:06.597036] }) @@ -28,7 +39,14 @@ test "Represent a user account" do avatar_static: "http://localhost:4001/images/avi.png", header: "http://localhost:4001/images/banner.png", header_static: "http://localhost:4001/images/banner.png", - emojis: [], + emojis: [ + %{ + "static_url" => "/file.png", + "url" => "/file.png", + "shortcode" => "karjalanpiirakka", + "visible_in_picker" => false + } + ], fields: [], source: %{ note: "", diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 000c589af..fefb6bdcc 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -20,6 +20,30 @@ test "A user with an avatar object", %{user: user} do assert represented["profile_image_url"] == image end + test "A user with emoji in username", %{user: user} do + expected = + "karjalanpiirakka man" + + user = %{ + user + | info: %{ + "source_data" => %{ + "tag" => [ + %{ + "type" => "Emoji", + "icon" => %{"url" => "/file.png"}, + "name" => ":karjalanpiirakka:" + } + ] + } + } + } + + user = %{user | name: ":karjalanpiirakka: man"} + represented = UserView.render("show.json", %{user: user}) + assert represented["name_html"] == expected + end + test "A user" do note_activity = insert(:note_activity) user = User.get_cached_by_ap_id(note_activity.data["actor"]) @@ -40,7 +64,7 @@ test "A user" do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, - "screen_name_html" => user.nickname, + "name_html" => user.name, "description" => HtmlSanitizeEx.strip_tags(user.bio), "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), @@ -79,7 +103,7 @@ test "A user for a given other follower", %{user: user} do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, - "screen_name_html" => user.nickname, + "name_html" => user.name, "description" => HtmlSanitizeEx.strip_tags(user.bio), "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), @@ -119,7 +143,7 @@ test "A user that follows you", %{user: user} do "id" => follower.id, "name" => follower.name, "screen_name" => follower.nickname, - "screen_name_html" => follower.nickname, + "name_html" => follower.name, "description" => HtmlSanitizeEx.strip_tags(follower.bio), "description_html" => HtmlSanitizeEx.strip_tags(follower.bio), "created_at" => follower.inserted_at |> Utils.format_naive_asctime(), @@ -166,7 +190,7 @@ test "A blocked user for the blocker" do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, - "screen_name_html" => user.nickname, + "name_html" => user.name, "description" => HtmlSanitizeEx.strip_tags(user.bio), "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), From 37b802682ce1231f99976538a11c1584d48f47f4 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 9 Aug 2018 13:07:03 +0300 Subject: [PATCH 4/4] HTML-sanitize usernames before emojifying. --- lib/pleroma/web/twitter_api/views/user_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 30aaaf1ce..7d0f0e703 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -50,7 +50,7 @@ def render("user.json", %{user: user = %User{}} = assigns) do "friends_count" => user_info[:following_count], "id" => user.id, "name" => user.name, - "name_html" => Formatter.emojify(user.name, emoji), + "name_html" => HtmlSanitizeEx.strip_tags(user.name) |> Formatter.emojify(emoji), "profile_image_url" => image, "profile_image_url_https" => image, "profile_image_url_profile_size" => image,