Merge branch 'fix/notifications-api' into 'develop'

Fix notifications mark as read API

Closes #1800

See merge request pleroma/pleroma!2570
This commit is contained in:
rinpatch 2020-05-22 14:35:13 +00:00
commit 96650c55ae
3 changed files with 20 additions and 9 deletions

View File

@ -8,6 +8,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaNotificationOperation do
alias Pleroma.Web.ApiSpec.NotificationOperation alias Pleroma.Web.ApiSpec.NotificationOperation
alias Pleroma.Web.ApiSpec.Schemas.ApiError alias Pleroma.Web.ApiSpec.Schemas.ApiError
import Pleroma.Web.ApiSpec.Helpers
def open_api_operation(action) do def open_api_operation(action) do
operation = String.to_existing_atom("#{action}_operation") operation = String.to_existing_atom("#{action}_operation")
apply(__MODULE__, operation, []) apply(__MODULE__, operation, [])
@ -17,10 +19,14 @@ def mark_as_read_operation do
%Operation{ %Operation{
tags: ["Notifications"], tags: ["Notifications"],
summary: "Mark notifications as read. Query parameters are mutually exclusive.", summary: "Mark notifications as read. Query parameters are mutually exclusive.",
parameters: [ requestBody:
Operation.parameter(:id, :query, :string, "A single notification ID to read"), request_body("Parameters", %Schema{
Operation.parameter(:max_id, :query, :string, "Read all notifications up to this id") 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"]}], security: [%{"oAuth" => ["write:notifications"]}],
operationId: "PleromaAPI.NotificationController.mark_as_read", operationId: "PleromaAPI.NotificationController.mark_as_read",
responses: %{ responses: %{

View File

@ -14,7 +14,7 @@ defmodule Pleroma.Web.PleromaAPI.NotificationController do
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaNotificationOperation 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 with {:ok, notification} <- Notification.read_one(user, notification_id) do
render(conn, "show.json", notification: notification, for: user) render(conn, "show.json", notification: notification, for: user)
else else
@ -25,7 +25,7 @@ def mark_as_read(%{assigns: %{user: user}} = conn, %{id: notification_id}) do
end end
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 = notifications =
user user
|> Notification.set_read_up_to(max_id) |> Notification.set_read_up_to(max_id)

View File

@ -23,7 +23,8 @@ test "it marks a single notification as read", %{user: user1, conn: conn} do
response = response =
conn 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) |> json_response_and_validate_schema(:ok)
assert %{"pleroma" => %{"is_seen" => true}} = response assert %{"pleroma" => %{"is_seen" => true}} = response
@ -41,7 +42,8 @@ test "it marks multiple notifications as read", %{user: user1, conn: conn} do
[response1, response2] = [response1, response2] =
conn 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) |> json_response_and_validate_schema(:ok)
assert %{"pleroma" => %{"is_seen" => true}} = response1 assert %{"pleroma" => %{"is_seen" => true}} = response1
@ -54,7 +56,10 @@ test "it marks multiple notifications as read", %{user: user1, conn: conn} do
test "it returns error when notification not found", %{conn: conn} do test "it returns error when notification not found", %{conn: conn} do
response = response =
conn 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) |> json_response_and_validate_schema(:bad_request)
assert response == %{"error" => "Cannot get notification"} assert response == %{"error" => "Cannot get notification"}