From ce98d5eb9b4d9c5a09b91a9d4d13bb48ba2b8453 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Sun, 2 Dec 2018 22:03:53 +0300 Subject: [PATCH 1/3] Parse user's bio on register --- lib/pleroma/user.ex | 16 +++++++++++ lib/pleroma/web/twitter_api/twitter_api.ex | 2 +- .../web/twitter_api/twitter_api_controller.ex | 17 ++--------- .../twitter_api_controller_test.exs | 5 ++-- test/web/twitter_api/twitter_api_test.exs | 28 +++++++++++++++++++ 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 76712b4bf..3bd92c157 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -4,6 +4,8 @@ defmodule Pleroma.User do import Ecto.{Changeset, Query} alias Pleroma.{Repo, User, Object, Web, Activity, Notification} alias Comeonin.Pbkdf2 + alias Pleroma.Formatter + alias Pleroma.Web.CommonAPI.Utils, as: CommonUtils alias Pleroma.Web.{OStatus, Websub, OAuth} alias Pleroma.Web.ActivityPub.{Utils, ActivityPub} @@ -802,4 +804,18 @@ defmodule Pleroma.User do :error end end + + def parse_bio(bio, user \\ %User{info: %{source_data: %{}}}) 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) + + CommonUtils.format_input(bio, mentions, tags, "text/plain") |> Formatter.emojify(emoji) + end end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 39a2974bb..c19a4f084 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -132,7 +132,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do params = %{ nickname: params["nickname"], name: params["fullname"], - bio: params["bio"], + bio: User.parse_bio(params["bio"]), email: params["email"], password: params["password"], password_confirmation: params["confirm"] diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index ff644dd79..961250d92 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -448,27 +448,16 @@ defmodule Pleroma.Web.TwitterAPI.Controller do User.Info.profile_update(user.info, info_params) end - defp add_profile_emoji(user, params) do + defp parse_profile_bio(user, params) 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, "text/plain") - Map.put(params, "bio", bio_html |> Formatter.emojify(emoji)) + Map.put(params, "bio", User.parse_bio(bio, user)) else params end end def update_profile(%{assigns: %{user: user}} = conn, params) do - params = add_profile_emoji(user, params) + params = parse_profile_bio(user, params) info_cng = build_info_cng(user, params) with changeset <- User.update_changeset(user, params), diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 89c176da7..a1eb09a05 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -949,18 +949,19 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "POST /api/account/update_profile.json" do test "it updates a user's profile", %{conn: conn} do user = insert(:user) + user2 = insert(:user) conn = conn |> assign(:user, user) |> post("/api/account/update_profile.json", %{ "name" => "new name", - "description" => "new description" + "description" => "hi @#{user2.nickname}" }) user = Repo.get!(User, user.id) assert user.name == "new name" - assert user.bio == "new description" + assert user.bio == "hi @#{user2.nickname}" assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index ec13b89d4..baeea5a9e 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -257,6 +257,34 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do UserView.render("show.json", %{user: fetched_user}) end + test "it registers a new user and parses mentions in the bio" do + data1 = %{ + "nickname" => "john", + "email" => "john@gmail.com", + "fullname" => "John Doe", + "bio" => "test", + "password" => "bear", + "confirm" => "bear" + } + + {:ok, user1} = TwitterAPI.register_user(data1) + + data2 = %{ + "nickname" => "lain", + "email" => "lain@wired.jp", + "fullname" => "lain iwakura", + "bio" => "@john test", + "password" => "bear", + "confirm" => "bear" + } + + {:ok, user2} = TwitterAPI.register_user(data2) + + expected_text = "@john test" + + assert user2.bio == expected_text + end + @moduletag skip: "needs 'registrations_open: false' in config" test "it registers a new user via invite token and returns the user." do {:ok, token} = UserInviteToken.create_token() From 8148943368510d1ec27889b91067d543ed4c2f10 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Sun, 2 Dec 2018 22:08:53 +0300 Subject: [PATCH 2/3] Format files --- test/web/twitter_api/twitter_api_controller_test.exs | 4 +++- test/web/twitter_api/twitter_api_test.exs | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index a1eb09a05..2cae8a071 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -961,7 +961,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do user = Repo.get!(User, user.id) assert user.name == "new name" - assert user.bio == "hi @#{user2.nickname}" + + assert user.bio == + "hi @#{user2.nickname}" assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index baeea5a9e..28230699f 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -280,7 +280,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do {:ok, user2} = TwitterAPI.register_user(data2) - expected_text = "@john test" + expected_text = + "@john test" assert user2.bio == expected_text end From 4b70e5eae93f34c1b98263a96d4e1380ef579ede Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 3 Dec 2018 18:59:50 +0300 Subject: [PATCH 3/3] Parse bio for MastodonAPI --- .../web/mastodon_api/mastodon_api_controller.ex | 2 +- .../web/mastodon_api/mastodon_api_controller_test.exs | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index d19d55044..543fdf416 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -55,7 +55,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do user_params = %{} |> add_if_present(params, "display_name", :name) - |> add_if_present(params, "note", :bio) + |> add_if_present(params, "note", :bio, fn value -> {:ok, User.parse_bio(value)} end) |> add_if_present(params, "avatar", :avatar, fn value -> with %Plug.Upload{} <- value, {:ok, object} <- ActivityPub.upload(value, type: :avatar) do diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 7042a6ace..098acb59f 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1253,14 +1253,21 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do describe "updating credentials" do test "updates the user's bio", %{conn: conn} do user = insert(:user) + user2 = insert(:user) conn = conn |> assign(:user, user) - |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"}) + |> patch("/api/v1/accounts/update_credentials", %{ + "note" => "I drink #cofe with @#{user2.nickname}" + }) assert user = json_response(conn, 200) - assert user["note"] == "I drink #cofe" + + assert user["note"] == + "I drink #cofe with @#{user2.nickname}" end test "updates the user's locking status", %{conn: conn} do