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