Merge branch 'mastoapi-update-credentials' into 'develop'
MastoAPI: Add update credentials endpoint. Closes #61 See merge request pleroma/pleroma!15
This commit is contained in:
commit
efcf54dc02
3 changed files with 102 additions and 0 deletions
|
@ -23,6 +23,57 @@ def create_app(conn, params) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_credentials(%{assigns: %{user: user}} = conn, params) do
|
||||||
|
params = if bio = params["note"] do
|
||||||
|
Map.put(params, "bio", bio)
|
||||||
|
else
|
||||||
|
params
|
||||||
|
end
|
||||||
|
|
||||||
|
params = if name = params["display_name"] do
|
||||||
|
Map.put(params, "name", name)
|
||||||
|
else
|
||||||
|
params
|
||||||
|
end
|
||||||
|
|
||||||
|
user = if avatar = params["avatar"] do
|
||||||
|
with %Plug.Upload{} <- avatar,
|
||||||
|
{:ok, object} <- ActivityPub.upload(avatar),
|
||||||
|
change = Ecto.Changeset.change(user, %{avatar: object.data}),
|
||||||
|
{:ok, user} = Repo.update(change) do
|
||||||
|
user
|
||||||
|
else
|
||||||
|
_e -> user
|
||||||
|
end
|
||||||
|
else
|
||||||
|
user
|
||||||
|
end
|
||||||
|
|
||||||
|
user = if banner = params["header"] do
|
||||||
|
with %Plug.Upload{} <- banner,
|
||||||
|
{:ok, object} <- ActivityPub.upload(banner),
|
||||||
|
new_info <- Map.put(user.info, "banner", object.data),
|
||||||
|
change <- User.info_changeset(user, %{info: new_info}),
|
||||||
|
{:ok, user} <- Repo.update(change) do
|
||||||
|
user
|
||||||
|
else
|
||||||
|
_e -> user
|
||||||
|
end
|
||||||
|
else
|
||||||
|
user
|
||||||
|
end
|
||||||
|
|
||||||
|
with changeset <- User.update_changeset(user, params),
|
||||||
|
{:ok, user} <- Repo.update(changeset) do
|
||||||
|
json conn, AccountView.render("account.json", %{user: user})
|
||||||
|
else
|
||||||
|
_e ->
|
||||||
|
conn
|
||||||
|
|> put_status(403)
|
||||||
|
|> json(%{error: "Invalid request"})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def verify_credentials(%{assigns: %{user: user}} = conn, params) do
|
def verify_credentials(%{assigns: %{user: user}} = conn, params) do
|
||||||
account = AccountView.render("account.json", %{user: user})
|
account = AccountView.render("account.json", %{user: user})
|
||||||
json(conn, account)
|
json(conn, account)
|
||||||
|
|
|
@ -53,6 +53,7 @@ def user_fetcher(username) do
|
||||||
scope "/api/v1", Pleroma.Web.MastodonAPI do
|
scope "/api/v1", Pleroma.Web.MastodonAPI do
|
||||||
pipe_through :authenticated_api
|
pipe_through :authenticated_api
|
||||||
|
|
||||||
|
patch "/accounts/update_credentials", MastodonAPIController, :update_credentials
|
||||||
get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
|
get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
|
||||||
get "/accounts/relationships", MastodonAPIController, :relationships
|
get "/accounts/relationships", MastodonAPIController, :relationships
|
||||||
get "/accounts/search", MastodonAPIController, :account_search
|
get "/accounts/search", MastodonAPIController, :account_search
|
||||||
|
|
|
@ -420,4 +420,54 @@ test "returns the favorites of a user", %{conn: conn} do
|
||||||
assert [status] = json_response(conn, 200)
|
assert [status] = json_response(conn, 200)
|
||||||
assert status["id"] == to_string(activity.id)
|
assert status["id"] == to_string(activity.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "updating credentials" do
|
||||||
|
test "updates the user's bio" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
conn = conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
|
||||||
|
|
||||||
|
assert user = json_response(conn, 200)
|
||||||
|
assert user["note"] == "I drink #cofe"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "updates the user's name" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
conn = conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
|
||||||
|
|
||||||
|
assert user = json_response(conn, 200)
|
||||||
|
assert user["display_name"] == "markorepairs"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "updates the user's avatar" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
|
||||||
|
|
||||||
|
conn = conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
|
||||||
|
|
||||||
|
assert user = json_response(conn, 200)
|
||||||
|
assert user["avatar"] != "https://placehold.it/48x48"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "updates the user's banner" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
|
||||||
|
|
||||||
|
conn = conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
|
||||||
|
|
||||||
|
assert user = json_response(conn, 200)
|
||||||
|
assert user["header"] != "https://placehold.it/700x335"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue