From d407f662af625818aa1a3d236b354b120546516d Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 30 Nov 2018 17:07:37 +0100 Subject: [PATCH] Fix TwitterApiController. --- lib/pleroma/user/info.ex | 9 ++ .../web/twitter_api/twitter_api_controller.ex | 101 ++++++++---------- .../twitter_api_controller_test.exs | 14 +-- 3 files changed, 59 insertions(+), 65 deletions(-) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 08370fe3c..49387163a 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -114,6 +114,15 @@ defmodule Pleroma.User.Info do ]) end + def profile_update(info, params) do + info + |> cast(params, [ + :locked, + :no_rich_text, + :default_scope + ]) + end + def set_source_data(info, source_data) do params = %{source_data: source_data} diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 8a5cf5fcd..b0bd98dbc 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -437,67 +437,52 @@ defmodule Pleroma.Web.TwitterAPI.Controller do json(conn, []) end + defp build_info_cng(user, params) do + info_params = + ["no_rich_text", "locked"] + |> Enum.reduce(%{}, fn key, res -> + if value = params[key] do + Map.put(res, key, value == "true") + else + res + end + end) + + info_params = + if value = params["default_scope"] do + Map.put(info_params, "default_scope", value) + else + info_params + end + + User.Info.profile_update(user.info, info_params) + end + + defp add_profile_emoji(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)) + else + params + end + end + def update_profile(%{assigns: %{user: user}} = conn, params) do - params = - 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)) - else - params - end - - user = - if locked = params["locked"] do - with locked <- locked == "true", - new_info <- Map.put(user.info, "locked", locked), - change <- User.info_changeset(user, %{info: new_info}), - {:ok, user} <- User.update_and_set_cache(change) do - user - else - _e -> user - end - else - user - end - - user = - if no_rich_text = params["no_rich_text"] do - with no_rich_text <- no_rich_text == "true", - new_info <- Map.put(user.info, "no_rich_text", no_rich_text), - change <- User.info_changeset(user, %{info: new_info}), - {:ok, user} <- User.update_and_set_cache(change) do - user - else - _e -> user - end - else - user - end - - user = - if default_scope = params["default_scope"] do - with new_info <- Map.put(user.info, "default_scope", default_scope), - change <- User.info_changeset(user, %{info: new_info}), - {:ok, user} <- User.update_and_set_cache(change) do - user - else - _e -> user - end - else - user - end + params = add_profile_emoji(user, params) + info_cng = build_info_cng(user, params) with changeset <- User.update_changeset(user, params), + changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng), {:ok, user} <- User.update_and_set_cache(changeset) do CommonAPI.update(user) render(conn, UserView, "user.json", %{user: user, for: user}) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 9062f1c4e..a76112e37 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -67,7 +67,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "GET /statuses/public_timeline.json" do test "returns statuses", %{conn: conn} do - {:ok, user} = UserBuilder.insert() + user = insert(:user) activities = ActivityBuilder.insert_list(30, %{}, %{user: user}) ActivityBuilder.insert_list(10, %{}, %{user: user}) since_id = List.last(activities).id @@ -571,7 +571,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do |> post("/api/blocks/destroy.json", %{user_id: blocked.id}) current_user = Repo.get(User, current_user.id) - assert current_user.info["blocks"] == [] + assert current_user.info.blocks == [] assert json_response(conn, 200) == UserView.render("show.json", %{user: blocked, for: current_user}) @@ -946,7 +946,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do }) user = Repo.get!(User, user.id) - assert user.info["locked"] == true + assert user.info.locked == true assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end @@ -962,7 +962,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do }) user = Repo.get!(User, user.id) - assert user.info["locked"] == false + assert user.info.locked == false assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end @@ -1136,7 +1136,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do user = insert(:user, %{info: %{"locked" => true}}) other_user = insert(:user) - {:ok, activity} = ActivityPub.follow(other_user, user) + {:ok, _activity} = ActivityPub.follow(other_user, user) user = Repo.get(User, user.id) other_user = Repo.get(User, other_user.id) @@ -1158,7 +1158,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do user = insert(:user, %{info: %{"locked" => true}}) other_user = insert(:user) - {:ok, activity} = ActivityPub.follow(other_user, user) + {:ok, _activity} = ActivityPub.follow(other_user, user) user = Repo.get(User, user.id) other_user = Repo.get(User, other_user.id) @@ -1181,7 +1181,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do user = insert(:user, %{info: %{"locked" => true}}) other_user = insert(:user) - {:ok, activity} = ActivityPub.follow(other_user, user) + {:ok, _activity} = ActivityPub.follow(other_user, user) user = Repo.get(User, user.id) other_user = Repo.get(User, other_user.id)