From 5142a8efbbe7301c5d909393264ae62b51ff1bf2 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 29 Aug 2017 15:14:00 +0200 Subject: [PATCH] Add profile update. --- lib/pleroma/user.ex | 13 +++++++++++-- lib/pleroma/web/router.ex | 2 ++ lib/pleroma/web/twitter_api/twitter_api_controller.ex | 17 +++++++++++++++++ test/web/twitter_api/twitter_api_controller_test.exs | 16 ++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index f28f0deb0..4f5fcab5b 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -80,6 +80,15 @@ defmodule Pleroma.User do end end + def update_changeset(struct, params \\ %{}) do + changeset = struct + |> cast(params, [:bio, :name]) + |> unique_constraint(:nickname) + |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) + |> validate_length(:bio, min: 1, max: 1000) + |> validate_length(:name, min: 1, max: 100) + end + def register_changeset(struct, params \\ %{}) do changeset = struct |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) @@ -89,8 +98,8 @@ defmodule Pleroma.User do |> unique_constraint(:nickname) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_format(:email, @email_regex) - |> validate_length(:bio, max: 1000) - |> validate_length(:name, max: 100) + |> validate_length(:bio, min: 1, max: 1000) + |> validate_length(:name, min: 1, max: 100) if changeset.valid? do hashed = Pbkdf2.hashpwsalt(changeset.changes[:password]) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index d8e225f07..ab849aef5 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -69,6 +69,8 @@ defmodule Pleroma.Web.Router do get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials + post "/account/update_profile", TwitterAPI.Controller, :update_profile + post "/account/most_recent_notification", TwitterAPI.Controller, :update_most_recent_notification get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 9b1c74a3f..335cb3e7c 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -6,6 +6,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do alias Pleroma.Web.ActivityPub.ActivityPub alias Ecto.Changeset + require Logger + def verify_credentials(%{assigns: %{user: user}} = conn, _params) do render(conn, UserView, "show.json", %{user: user}) end @@ -226,6 +228,21 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def update_profile(%{assigns: %{user: user}} = conn, params) do + if bio = params["description"] do + params = Map.put(params, "bio", bio) + end + + with changeset <- User.update_changeset(user, params), + {:ok, user} <- Repo.update(changeset) do + render(conn, UserView, "user.json", %{user: user, for: user}) + else + error -> + Logger.debug("Can't update user: #{inspect(error)}") + bad_request_reply(conn, "Can't update user") + end + end + defp bad_request_reply(conn, error_message) do json = error_json(conn, error_message) json_reply(conn, 400, json) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 8c689d7d3..89b8c2eeb 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -455,6 +455,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "POST /api/account/update_profile.json" do + test "it updates a user's profile" do + user = insert(:user) + + conn = conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{"name" => "new name", "description" => "new description"}) + + user = Repo.get!(User, user.id) + assert user.name == "new name" + assert user.bio == "new description" + + assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) + end + end + defp valid_user(_context) do user = insert(:user) [user: user]