MastoAPI: Add update credentials endpoint.
authoreal <eal@waifu.club>
Sat, 11 Nov 2017 22:27:09 +0000 (00:27 +0200)
committereal <eal@waifu.club>
Sat, 11 Nov 2017 22:27:09 +0000 (00:27 +0200)
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/router.ex
test/web/mastodon_api/mastodon_api_controller_test.exs

index c28e20ed11d8f32200c954b9d9a403f246663817..fb06d55c13b5cc6ce124f1493268db67b5248f29 100644 (file)
@@ -23,6 +23,57 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     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
     account = AccountView.render("account.json", %{user: user})
     json(conn, account)
index 0a0aea96622c13b435e92b846a8d771f0143e63d..4c74736e2b382f4b8ea6e097140fbe959a4a6aa2 100644 (file)
@@ -53,6 +53,7 @@ defmodule Pleroma.Web.Router do
   scope "/api/v1", Pleroma.Web.MastodonAPI do
     pipe_through :authenticated_api
 
+    patch "/accounts/update_credentials", MastodonAPIController, :update_credentials
     get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
     get "/accounts/relationships", MastodonAPIController, :relationships
     get "/accounts/search", MastodonAPIController, :account_search
index 47a61383779f482425c6d17c6db14edc350a3deb..cf60b4a51df9a25bac80065648bb65a2adb84119 100644 (file)
@@ -420,4 +420,54 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     assert [status] = json_response(conn, 200)
     assert status["id"] == to_string(activity.id)
   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