From bd74ad9ce49955cb4a36ab3f5058a1f113adbca3 Mon Sep 17 00:00:00 2001 From: Oneric Date: Mon, 8 Apr 2024 23:00:24 +0200 Subject: [PATCH] Accept body parameters for /api/pleroma/notification_settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This brings it in line with its documentation and akkoma-fe’s expectations. For backwards compatibility URL parameters are still accept with lower priority. Unfortunately this means duplicating parameters and descriptions in the API spec. Usually Plug already pre-merges parameters from different sources into the plain 'params' parameter which then gets forwarded by Phoenix. However, OpenApiSpex 3.x prevents this; 4.x is set to change this https://github.com/open-api-spex/open_api_spex/issues/334 https://github.com/open-api-spex/open_api_spex/issues/92 Fixes: https://akkoma.dev/AkkomaGang/akkoma/issues/691 Fixes: https://akkoma.dev/AkkomaGang/akkoma/issues/722 --- .../operations/twitter_util_operation.ex | 20 ++++- .../controllers/util_controller.ex | 8 +- .../web/twitter_api/util_controller_test.exs | 74 ++++++++++++++++++- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/api_spec/operations/twitter_util_operation.ex b/lib/pleroma/web/api_spec/operations/twitter_util_operation.ex index 456ab14db..0f202201d 100644 --- a/lib/pleroma/web/api_spec/operations/twitter_util_operation.ex +++ b/lib/pleroma/web/api_spec/operations/twitter_util_operation.ex @@ -150,7 +150,7 @@ defmodule Pleroma.Web.ApiSpec.TwitterUtilOperation do "removes the contents of a message from the push notification" ) ], - requestBody: nil, + requestBody: request_body("Parameters", update_notification_settings_request()), responses: %{ 200 => Operation.response("Success", "application/json", %Schema{ @@ -432,4 +432,22 @@ defmodule Pleroma.Web.ApiSpec.TwitterUtilOperation do } } end + + defp update_notification_settings_request do + %Schema{ + title: "UpdateNotificationSettings", + description: "PUT paramenters (query, form or JSON) for updating notification settings", + type: :object, + properties: %{ + block_from_strangers: %Schema{ + type: :boolean, + description: "blocks notifications from accounts you do not follow" + }, + hide_notification_contents: %Schema{ + type: :boolean, + description: "removes the contents of a message from the push notification" + } + } + } + end end diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index f2b571fff..5f1a3518d 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -184,7 +184,13 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do json(conn, emoji) end - def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do + def update_notificaton_settings( + %{assigns: %{user: user}, body_params: body_params} = conn, + params + ) do + # OpenApiSpex 3.x prevents Plug's usual parameter premerging + params = Map.merge(params, body_params) + with {:ok, _} <- User.update_notification_settings(user, params) do json(conn, %{status: "success"}) end diff --git a/test/pleroma/web/twitter_api/util_controller_test.exs b/test/pleroma/web/twitter_api/util_controller_test.exs index 169e9981c..3192ad3af 100644 --- a/test/pleroma/web/twitter_api/util_controller_test.exs +++ b/test/pleroma/web/twitter_api/util_controller_test.exs @@ -24,7 +24,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do describe "PUT /api/pleroma/notification_settings" do setup do: oauth_access(["write:accounts"]) - test "it updates notification settings", %{user: user, conn: conn} do + test "it updates notification settings via url paramters", %{user: user, conn: conn} do conn |> put( "/api/pleroma/notification_settings?#{URI.encode_query(%{block_from_strangers: true})}" @@ -39,6 +39,57 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do } == user.notification_settings end + test "it updates notification settings via JSON body params", %{user: user, conn: conn} do + conn + |> put_req_header("content-type", "application/json") + |> put( + "/api/pleroma/notification_settings", + %{"block_from_strangers" => true} + ) + |> json_response_and_validate_schema(:ok) + + user = refresh_record(user) + + assert %Pleroma.User.NotificationSetting{ + block_from_strangers: true, + hide_notification_contents: false + } == user.notification_settings + end + + test "it updates notification settings via form data", %{user: user, conn: conn} do + conn + |> put_req_header("content-type", "multipart/form-data") + |> put( + "/api/pleroma/notification_settings", + %{:block_from_strangers => true} + ) + |> json_response_and_validate_schema(:ok) + + user = refresh_record(user) + + assert %Pleroma.User.NotificationSetting{ + block_from_strangers: true, + hide_notification_contents: false + } == user.notification_settings + end + + test "it updates notification settings via urlencoded body", %{user: user, conn: conn} do + conn + |> put_req_header("content-type", "application/x-www-form-urlencoded") + |> put( + "/api/pleroma/notification_settings", + "block_from_strangers=true" + ) + |> json_response_and_validate_schema(:ok) + + user = refresh_record(user) + + assert %Pleroma.User.NotificationSetting{ + block_from_strangers: true, + hide_notification_contents: false + } == user.notification_settings + end + test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do conn |> put( @@ -53,6 +104,27 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do hide_notification_contents: true } == user.notification_settings end + + # we already test all body variants for block_from_strangers, so just one should suffice here + test "it updates notification settings to enable hiding contents via JSON body params", %{ + user: user, + conn: conn + } do + conn + |> put_req_header("content-type", "application/json") + |> put( + "/api/pleroma/notification_settings", + %{"hide_notification_contents" => true} + ) + |> json_response_and_validate_schema(:ok) + + user = refresh_record(user) + + assert %Pleroma.User.NotificationSetting{ + block_from_strangers: false, + hide_notification_contents: true + } == user.notification_settings + end end describe "GET /api/pleroma/frontend_configurations" do