From 5debd7b5cc31a6dedd8d0c8bc177be2cd1b995aa Mon Sep 17 00:00:00 2001 From: ensra Date: Tue, 21 Aug 2018 14:03:36 +0100 Subject: [PATCH 1/5] Twitter API: Support Mastodon-style bios (multi-line, with links, and user mentions) --- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 15 +++++++++++++-- lib/pleroma/web/twitter_api/views/user_view.ex | 6 ++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 65e67396b..dfa6d2464 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -1,7 +1,9 @@ defmodule Pleroma.Web.TwitterAPI.Controller do use Pleroma.Web, :controller + alias Pleroma.Formatter alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView} alias Pleroma.Web.CommonAPI + alias Pleroma.Web.CommonAPI.Utils, as: CommonUtils alias Pleroma.{Repo, Activity, User, Notification} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils @@ -411,8 +413,17 @@ defmodule Pleroma.Web.TwitterAPI.Controller do def update_profile(%{assigns: %{user: user}} = conn, params) do params = if bio = params["description"] do - bio_brs = Regex.replace(~r/\r?\n/, bio, "
") - Map.put(params, "bio", bio_brs) + mentions = Formatter.parse_mentions(bio) + tags = Formatter.parse_tags(bio) + 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_html = CommonUtils.format_input(bio, mentions, tags) + |> Formatter.emojify(emoji) + Map.put(params, "bio", bio_html) else params end diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 7d0f0e703..25fda1aa8 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -36,12 +36,10 @@ defmodule Pleroma.Web.TwitterAPI.UserView do {String.trim(name, ":"), url} end) - bio = HtmlSanitizeEx.strip_tags(user.bio) - data = %{ "created_at" => user.inserted_at |> Utils.format_naive_asctime(), - "description" => bio, - "description_html" => bio |> Formatter.emojify(emoji), + "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("
", "\n")), + "description_html" => HtmlSanitizeEx.basic_html(user.bio), "favourites_count" => 0, "followers_count" => user_info[:follower_count], "following" => following, From cdb3dd48ab7177a21e1cdcef4bcff00bf5dc124f Mon Sep 17 00:00:00 2001 From: ensra Date: Tue, 21 Aug 2018 14:12:55 +0100 Subject: [PATCH 2/5] Format --- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index dfa6d2464..b3a56b27e 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -415,15 +415,16 @@ defmodule Pleroma.Web.TwitterAPI.Controller do if bio = params["description"] do mentions = Formatter.parse_mentions(bio) tags = Formatter.parse_tags(bio) + 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_html = CommonUtils.format_input(bio, mentions, tags) - |> Formatter.emojify(emoji) - Map.put(params, "bio", bio_html) + Map.put(params, "bio", bio_html |> Formatter.emojify(emoji)) else params end From 1495d04789239f78e06c7b4af4219f54be15eeaa Mon Sep 17 00:00:00 2001 From: ensra Date: Tue, 21 Aug 2018 14:51:27 +0100 Subject: [PATCH 3/5] fix test for converting bio \r\n to
. --- lib/pleroma/web/common_api/utils.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index c7b0d7935..869f4c566 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -64,7 +64,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do status - |> String.replace("\r", "") |> format_input(mentions, tags) |> maybe_add_attachments(attachments, no_attachment_links) end @@ -95,7 +94,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do def format_input(text, mentions, tags) do text |> Formatter.html_escape() - |> String.replace("\n", "
") + |> String.replace(~r/\r?\n/, "
") |> (&{[], &1}).() |> Formatter.add_links() |> Formatter.add_user_links(mentions) From f533daffdbbf2912677868c68f2ca0e28f8db347 Mon Sep 17 00:00:00 2001 From: ensra Date: Tue, 21 Aug 2018 15:24:06 +0100 Subject: [PATCH 4/5] sync bio sanitization code in tests --- test/web/twitter_api/views/user_view_test.exs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index fefb6bdcc..180b7602e 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -65,8 +65,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => user.name, "screen_name" => user.nickname, "name_html" => user.name, - "description" => HtmlSanitizeEx.strip_tags(user.bio), - "description_html" => HtmlSanitizeEx.strip_tags(user.bio), + "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("
", "\n")), + "description_html" => HtmlSanitizeEx.basic_html(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 1, @@ -104,8 +104,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => user.name, "screen_name" => user.nickname, "name_html" => user.name, - "description" => HtmlSanitizeEx.strip_tags(user.bio), - "description_html" => HtmlSanitizeEx.strip_tags(user.bio), + "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("
", "\n")), + "description_html" => HtmlSanitizeEx.basic_html(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, @@ -144,8 +144,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => follower.name, "screen_name" => follower.nickname, "name_html" => follower.name, - "description" => HtmlSanitizeEx.strip_tags(follower.bio), - "description_html" => HtmlSanitizeEx.strip_tags(follower.bio), + "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("
", "\n")), + "description_html" => HtmlSanitizeEx.basic_html(user.bio), "created_at" => follower.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, @@ -191,8 +191,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => user.name, "screen_name" => user.nickname, "name_html" => user.name, - "description" => HtmlSanitizeEx.strip_tags(user.bio), - "description_html" => HtmlSanitizeEx.strip_tags(user.bio), + "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("
", "\n")), + "description_html" => HtmlSanitizeEx.basic_html(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, From e883587cc3a0c386e3cc3f63c2d8a2d64f7c953b Mon Sep 17 00:00:00 2001 From: ensra Date: Tue, 21 Aug 2018 15:25:21 +0100 Subject: [PATCH 5/5] this should be looking at the follower's bio, not the user's bio. fixes test. --- test/web/twitter_api/views/user_view_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 180b7602e..24a5c5bca 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -144,8 +144,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => follower.name, "screen_name" => follower.nickname, "name_html" => follower.name, - "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("
", "\n")), - "description_html" => HtmlSanitizeEx.basic_html(user.bio), + "description" => HtmlSanitizeEx.strip_tags(follower.bio |> String.replace("
", "\n")), + "description_html" => HtmlSanitizeEx.basic_html(follower.bio), "created_at" => follower.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0,