Move notification actions to PleromaAPI.NotificationController
authorEgor Kislitsyn <egor@kislitsyn.com>
Wed, 20 May 2020 11:14:11 +0000 (15:14 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 20 May 2020 11:15:13 +0000 (15:15 +0400)
lib/pleroma/web/api_spec/operations/pleroma_notification_operation.ex [moved from lib/pleroma/web/api_spec/operations/pleroma_operation.ex with 89% similarity]
lib/pleroma/web/pleroma_api/controllers/notification_controller.ex [new file with mode: 0644]
lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex [deleted file]
lib/pleroma/web/router.ex
test/web/pleroma_api/controllers/notification_controller_test.exs [moved from test/web/pleroma_api/controllers/pleroma_api_controller_test.exs with 97% similarity]

similarity index 89%
rename from lib/pleroma/web/api_spec/operations/pleroma_operation.ex
rename to lib/pleroma/web/api_spec/operations/pleroma_notification_operation.ex
index d28451933224bd08990b0041465348e86ea87a13..636c39a15ae4c9b02e0347c45d233f404cd8e1a7 100644 (file)
@@ -2,7 +2,7 @@
 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
-defmodule Pleroma.Web.ApiSpec.PleromaOperation do
+defmodule Pleroma.Web.ApiSpec.PleromaNotificationOperation do
   alias OpenApiSpex.Operation
   alias OpenApiSpex.Schema
   alias Pleroma.Web.ApiSpec.NotificationOperation
@@ -13,7 +13,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaOperation do
     apply(__MODULE__, operation, [])
   end
 
-  def mark_notifications_as_read_operation do
+  def mark_as_read_operation do
     %Operation{
       tags: ["Notifications"],
       summary: "Mark notifications as read. Query parameters are mutually exclusive.",
@@ -22,7 +22,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaOperation do
         Operation.parameter(:max_id, :query, :string, "Read all notifications up to this id")
       ],
       security: [%{"oAuth" => ["write:notifications"]}],
-      operationId: "PleromaController.mark_notifications_as_read",
+      operationId: "PleromaAPI.NotificationController.mark_as_read",
       responses: %{
         200 =>
           Operation.response(
diff --git a/lib/pleroma/web/pleroma_api/controllers/notification_controller.ex b/lib/pleroma/web/pleroma_api/controllers/notification_controller.ex
new file mode 100644 (file)
index 0000000..0b2f678
--- /dev/null
@@ -0,0 +1,36 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.PleromaAPI.NotificationController do
+  use Pleroma.Web, :controller
+
+  alias Pleroma.Notification
+  alias Pleroma.Plugs.OAuthScopesPlug
+
+  plug(Pleroma.Web.ApiSpec.CastAndValidate)
+  plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :mark_as_read)
+  plug(:put_view, Pleroma.Web.MastodonAPI.NotificationView)
+
+  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaNotificationOperation
+
+  def mark_as_read(%{assigns: %{user: user}} = conn, %{id: notification_id}) do
+    with {:ok, notification} <- Notification.read_one(user, notification_id) do
+      render(conn, "show.json", notification: notification, for: user)
+    else
+      {:error, message} ->
+        conn
+        |> put_status(:bad_request)
+        |> json(%{"error" => message})
+    end
+  end
+
+  def mark_as_read(%{assigns: %{user: user}} = conn, %{max_id: max_id}) do
+    notifications =
+      user
+      |> Notification.set_read_up_to(max_id)
+      |> Enum.take(80)
+
+    render(conn, "index.json", notifications: notifications, for: user)
+  end
+end
diff --git a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex
deleted file mode 100644 (file)
index a58665a..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
-  use Pleroma.Web, :controller
-
-  alias Pleroma.Notification
-  alias Pleroma.Plugs.OAuthScopesPlug
-  alias Pleroma.Web.MastodonAPI.NotificationView
-
-  plug(Pleroma.Web.ApiSpec.CastAndValidate)
-
-  plug(
-    OAuthScopesPlug,
-    %{scopes: ["write:notifications"]} when action == :mark_notifications_as_read
-  )
-
-  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaOperation
-
-  def mark_notifications_as_read(%{assigns: %{user: user}} = conn, %{id: notification_id}) do
-    with {:ok, notification} <- Notification.read_one(user, notification_id) do
-      conn
-      |> put_view(NotificationView)
-      |> render("show.json", %{notification: notification, for: user})
-    else
-      {:error, message} ->
-        conn
-        |> put_status(:bad_request)
-        |> json(%{"error" => message})
-    end
-  end
-
-  def mark_notifications_as_read(%{assigns: %{user: user}} = conn, %{max_id: max_id}) do
-    with notifications <- Notification.set_read_up_to(user, max_id) do
-      notifications = Enum.take(notifications, 80)
-
-      conn
-      |> put_view(NotificationView)
-      |> render("index.json",
-        notifications: notifications,
-        for: user
-      )
-    end
-  end
-end
index 78da4a871aaa94a1f8e10d4f5428dae2d2d1adc0..0e29e56456c2d53f52138026d2a901b2c7134c92 100644 (file)
@@ -312,7 +312,7 @@ defmodule Pleroma.Web.Router do
 
       put("/statuses/:id/reactions/:emoji", EmojiReactionController, :create)
       delete("/statuses/:id/reactions/:emoji", EmojiReactionController, :delete)
-      post("/notifications/read", PleromaAPIController, :mark_notifications_as_read)
+      post("/notifications/read", NotificationController, :mark_as_read)
 
       patch("/accounts/update_avatar", AccountController, :update_avatar)
       patch("/accounts/update_banner", AccountController, :update_banner)
similarity index 97%
rename from test/web/pleroma_api/controllers/pleroma_api_controller_test.exs
rename to test/web/pleroma_api/controllers/notification_controller_test.exs
index c4c6612669a116c2b7c0ace70f04f2d56bae82b4..7c5ace804d7e16e6bb946a5028d60047a740dbe2 100644 (file)
@@ -2,7 +2,7 @@
 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
-defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
+defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
   use Pleroma.Web.ConnCase
 
   alias Pleroma.Notification