Commit 5142a8ef authored by lain's avatar lain

Add profile update.

parent 171ef33c
......@@ -80,6 +80,15 @@ def remote_user_creation(params) 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 @@ def register_changeset(struct, params \\ %{}) 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])
......
......@@ -69,6 +69,8 @@ def user_fetcher(username) 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
......
......@@ -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 @@ def friends(%{assigns: %{user: user}} = conn, _params) 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)
......
......@@ -455,6 +455,22 @@ test "it returns a user's friends", %{conn: conn} 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]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment