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)
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)
|> 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
|> 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