Fix notifications mark as read API
authorEgor Kislitsyn <egor@kislitsyn.com>
Fri, 22 May 2020 14:15:36 +0000 (18:15 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Fri, 22 May 2020 14:15:36 +0000 (18:15 +0400)
lib/pleroma/web/api_spec/operations/pleroma_notification_operation.ex
lib/pleroma/web/pleroma_api/controllers/notification_controller.ex
test/web/pleroma_api/controllers/notification_controller_test.exs

index 636c39a15ae4c9b02e0347c45d233f404cd8e1a7..b0c8db863acc4feadced1250148247d88ebf3031 100644 (file)
@@ -8,6 +8,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaNotificationOperation do
   alias Pleroma.Web.ApiSpec.NotificationOperation
   alias Pleroma.Web.ApiSpec.Schemas.ApiError
 
+  import Pleroma.Web.ApiSpec.Helpers
+
   def open_api_operation(action) do
     operation = String.to_existing_atom("#{action}_operation")
     apply(__MODULE__, operation, [])
@@ -17,10 +19,14 @@ defmodule Pleroma.Web.ApiSpec.PleromaNotificationOperation do
     %Operation{
       tags: ["Notifications"],
       summary: "Mark notifications as read. Query parameters are mutually exclusive.",
-      parameters: [
-        Operation.parameter(:id, :query, :string, "A single notification ID to read"),
-        Operation.parameter(:max_id, :query, :string, "Read all notifications up to this id")
-      ],
+      requestBody:
+        request_body("Parameters", %Schema{
+          type: :object,
+          properties: %{
+            id: %Schema{type: :integer, description: "A single notification ID to read"},
+            max_id: %Schema{type: :integer, description: "Read all notifications up to this ID"}
+          }
+        }),
       security: [%{"oAuth" => ["write:notifications"]}],
       operationId: "PleromaAPI.NotificationController.mark_as_read",
       responses: %{
index 0b2f678c50212b46bdffb7711ae73cf5df47f3a9..3ed8bd294027eba8c708fdce17c41f461c147308 100644 (file)
@@ -14,7 +14,7 @@ defmodule Pleroma.Web.PleromaAPI.NotificationController do
 
   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaNotificationOperation
 
-  def mark_as_read(%{assigns: %{user: user}} = conn, %{id: notification_id}) do
+  def mark_as_read(%{assigns: %{user: user}, body_params: %{id: notification_id}} = conn, _) do
     with {:ok, notification} <- Notification.read_one(user, notification_id) do
       render(conn, "show.json", notification: notification, for: user)
     else
@@ -25,7 +25,7 @@ defmodule Pleroma.Web.PleromaAPI.NotificationController do
     end
   end
 
-  def mark_as_read(%{assigns: %{user: user}} = conn, %{max_id: max_id}) do
+  def mark_as_read(%{assigns: %{user: user}, body_params: %{max_id: max_id}} = conn, _) do
     notifications =
       user
       |> Notification.set_read_up_to(max_id)
index 7c5ace804d7e16e6bb946a5028d60047a740dbe2..bb4fe6c4992d9f93ec78eb6fe46dfc52e8666e82 100644 (file)
@@ -23,7 +23,8 @@ defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
 
       response =
         conn
-        |> post("/api/v1/pleroma/notifications/read?id=#{notification1.id}")
+        |> put_req_header("content-type", "application/json")
+        |> post("/api/v1/pleroma/notifications/read", %{id: notification1.id})
         |> json_response_and_validate_schema(:ok)
 
       assert %{"pleroma" => %{"is_seen" => true}} = response
@@ -41,7 +42,8 @@ defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
 
       [response1, response2] =
         conn
-        |> post("/api/v1/pleroma/notifications/read?max_id=#{notification2.id}")
+        |> put_req_header("content-type", "application/json")
+        |> post("/api/v1/pleroma/notifications/read", %{max_id: notification2.id})
         |> json_response_and_validate_schema(:ok)
 
       assert %{"pleroma" => %{"is_seen" => true}} = response1
@@ -54,7 +56,10 @@ defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
     test "it returns error when notification not found", %{conn: conn} do
       response =
         conn
-        |> post("/api/v1/pleroma/notifications/read?id=22222222222222")
+        |> put_req_header("content-type", "application/json")
+        |> post("/api/v1/pleroma/notifications/read", %{
+          id: 22_222_222_222_222
+        })
         |> json_response_and_validate_schema(:bad_request)
 
       assert response == %{"error" => "Cannot get notification"}