diff options
| author | Roger Braun <roger@rogerbraun.net> | 2017-08-29 15:14:00 +0200 | 
|---|---|---|
| committer | Roger Braun <roger@rogerbraun.net> | 2017-08-29 15:14:00 +0200 | 
| commit | 5142a8efbbe7301c5d909393264ae62b51ff1bf2 (patch) | |
| tree | f55f0747b76d6711f5627ae334f0fc22d3301260 /lib | |
| parent | 171ef33cbbdd96f2fb7c893be899f7e13c64a449 (diff) | |
| download | pleroma-5142a8efbbe7301c5d909393264ae62b51ff1bf2.tar.gz pleroma-5142a8efbbe7301c5d909393264ae62b51ff1bf2.zip | |
Add profile update.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/user.ex | 13 | ||||
| -rw-r--r-- | lib/pleroma/web/router.ex | 2 | ||||
| -rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api_controller.ex | 17 | 
3 files changed, 30 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) | 
