Add profile update.

This commit is contained in:
Roger Braun 2017-08-29 15:14:00 +02:00
parent 171ef33cbb
commit 5142a8efbb
4 changed files with 46 additions and 2 deletions

View file

@ -80,6 +80,15 @@ defmodule Pleroma.User do
end end
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 def register_changeset(struct, params \\ %{}) do
changeset = struct changeset = struct
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
@ -89,8 +98,8 @@ defmodule Pleroma.User do
|> unique_constraint(:nickname) |> unique_constraint(:nickname)
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|> validate_format(:email, @email_regex) |> validate_format(:email, @email_regex)
|> validate_length(:bio, max: 1000) |> validate_length(:bio, min: 1, max: 1000)
|> validate_length(:name, max: 100) |> validate_length(:name, min: 1, max: 100)
if changeset.valid? do if changeset.valid? do
hashed = Pbkdf2.hashpwsalt(changeset.changes[:password]) hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])

View file

@ -69,6 +69,8 @@ defmodule Pleroma.Web.Router do
get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
post "/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 post "/account/most_recent_notification", TwitterAPI.Controller, :update_most_recent_notification
get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline

View file

@ -6,6 +6,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Ecto.Changeset alias Ecto.Changeset
require Logger
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
render(conn, UserView, "show.json", %{user: user}) render(conn, UserView, "show.json", %{user: user})
end end
@ -226,6 +228,21 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end end
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 defp bad_request_reply(conn, error_message) do
json = error_json(conn, error_message) json = error_json(conn, error_message)
json_reply(conn, 400, json) json_reply(conn, 400, json)

View file

@ -455,6 +455,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
end end
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 defp valid_user(_context) do
user = insert(:user) user = insert(:user)
[user: user] [user: user]