From: lain Date: Sat, 1 Dec 2018 09:40:01 +0000 (+0100) Subject: Fix masto api user updating. X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=347df6421d6b1794a39ac6ce9e24a9e51e136a62;p=akkoma Fix masto api user updating. --- diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index bed8fd1b4..a84ab6841 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -112,10 +112,9 @@ defmodule Pleroma.User do end end - # TODO: Check if this still used def update_changeset(struct, params \\ %{}) do struct - |> cast(params, [:bio, :name]) + |> cast(params, [:bio, :name, :avatar]) |> unique_constraint(:nickname) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_length(:bio, max: 5000) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index a5ce6010f..8a938e514 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -128,6 +128,14 @@ defmodule Pleroma.User.Info do ]) end + def mastodon_profile_update(info, params) do + info + |> cast(params, [ + :locked, + :banner + ]) + end + def set_source_data(info, source_data) do params = %{source_data: source_data} diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index f209bb43c..d19d55044 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -32,67 +32,55 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + defp add_if_present( + map, + params, + params_field, + map_field, + value_function \\ fn x -> {:ok, x} end + ) do + if Map.has_key?(params, params_field) do + case value_function.(params[params_field]) do + {:ok, new_value} -> Map.put(map, map_field, new_value) + :error -> map + end + else + map + end + end + def update_credentials(%{assigns: %{user: user}} = conn, params) do original_user = user - params = - if bio = params["note"] do - Map.put(params, "bio", bio) - else - params - end - - params = - if name = params["display_name"] do - Map.put(params, "name", name) - else - params - end + user_params = + %{} + |> add_if_present(params, "display_name", :name) + |> add_if_present(params, "note", :bio) + |> add_if_present(params, "avatar", :avatar, fn value -> + with %Plug.Upload{} <- value, + {:ok, object} <- ActivityPub.upload(value, type: :avatar) do + {:ok, object.data} + else + _ -> :error + end + end) - user = - if avatar = params["avatar"] do - with %Plug.Upload{} <- avatar, - {:ok, object} <- ActivityPub.upload(avatar, type: :avatar), - change = Ecto.Changeset.change(user, %{avatar: object.data}), - {:ok, user} = User.update_and_set_cache(change) do - user + info_params = + %{} + |> add_if_present(params, "locked", :locked, fn value -> {:ok, value == "true"} end) + |> add_if_present(params, "header", :banner, fn value -> + with %Plug.Upload{} <- value, + {:ok, object} <- ActivityPub.upload(value, type: :banner) do + {:ok, object.data} else - _e -> user + _ -> :error end - else - user - end + end) + + info_cng = User.Info.mastodon_profile_update(user.info, info_params) - # user = - # if banner = params["header"] do - # with %Plug.Upload{} <- banner, - # {:ok, object} <- ActivityPub.upload(banner, type: :banner), - # new_info <- Map.put(user.info, "banner", object.data), - # 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 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 - - with changeset <- User.update_changeset(user, params), + with changeset <- User.update_changeset(user, user_params), + changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng), {:ok, user} <- User.update_and_set_cache(changeset) do if original_user != user do CommonAPI.update(user) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index ef9db20cf..7042a6ace 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1263,6 +1263,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert user["note"] == "I drink #cofe" end + test "updates the user's locking status", %{conn: conn} do + user = insert(:user) + + conn = + conn + |> assign(:user, user) + |> patch("/api/v1/accounts/update_credentials", %{locked: "true"}) + + assert user = json_response(conn, 200) + assert user["locked"] == true + end + test "updates the user's name", %{conn: conn} do user = insert(:user) @@ -1289,8 +1301,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do |> assign(:user, user) |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar}) - assert user = json_response(conn, 200) - assert user["avatar"] != "https://placehold.it/48x48" + assert user_response = json_response(conn, 200) + assert user_response["avatar"] != User.avatar_url(user) end test "updates the user's banner", %{conn: conn} do @@ -1307,8 +1319,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do |> assign(:user, user) |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header}) - assert user = json_response(conn, 200) - assert user["header"] != "https://placehold.it/700x335" + assert user_response = json_response(conn, 200) + assert user_response["header"] != User.banner_url(user) end end