Mastodon API: Fix PUT /api/web/settings
authoreal <eal@waifu.club>
Sun, 16 Dec 2018 11:15:34 +0000 (13:15 +0200)
committereal <eal@waifu.club>
Sun, 16 Dec 2018 11:15:34 +0000 (13:15 +0200)
lib/pleroma/user/info.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
test/web/mastodon_api/mastodon_api_controller_test.exs

index d81b45b8d077322a3645d74442c82d638f8aed40..a3785447c39164876ed9af74ec2551bbc44ed0e4 100644 (file)
@@ -149,9 +149,12 @@ defmodule Pleroma.User.Info do
     ])
   end
 
-  def mastodon_settings_update(info, params) do
+  def mastodon_settings_update(info, settings) do
+    params = %{settings: settings}
+
     info
     |> cast(params, [:settings])
+    |> validate_required([:settings])
   end
 
   def set_source_data(info, source_data) do
index 5c8602322f23310592f0aebcb4ac0e6a5ae7ac37..a46e1878fec56148085770b524fa42bbe6f4dd29 100644 (file)
@@ -929,7 +929,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
             ]
           },
           settings:
-            Map.get(user.info, :settings) ||
+            user.info.settings ||
               %{
                 onboarded: true,
                 home: %{
@@ -978,13 +978,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
     info_cng = User.Info.mastodon_settings_update(user.info, settings)
 
-    with changeset <- User.update_changeset(user),
+    with changeset <- Ecto.Changeset.change(user),
          changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
          {:ok, _user} <- User.update_and_set_cache(changeset) do
       json(conn, %{})
     else
       e ->
-        json(conn, %{error: inspect(e)})
+        conn
+        |> put_resp_content_type("application/json")
+        |> send_resp(500, Jason.encode!(%{"error" => inspect(e)}))
     end
   end
 
index e8275d4ab91bb4b4fbf71b726e29578841afa40c..aec0f851cf2717fe1ea35fc0a61553ef9acc44f7 100644 (file)
@@ -1415,4 +1415,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     assert result["stats"]["user_count"] == 2
     assert result["stats"]["status_count"] == 1
   end
+
+  test "put settings", %{conn: conn} do
+    user = insert(:user)
+
+    conn =
+      conn
+      |> assign(:user, user)
+      |> put("/api/web/settings", %{"data" => %{"programming" => "socks"}})
+
+    assert result = json_response(conn, 200)
+
+    user = User.get_cached_by_ap_id(user.ap_id)
+    assert user.info.settings == %{"programming" => "socks"}
+  end
 end