Create AdminAPI.InstanceController
authorAlex Gleason <alex@alexgleason.me>
Sat, 17 Jul 2021 18:05:25 +0000 (13:05 -0500)
committerAlex Gleason <alex@alexgleason.me>
Sat, 17 Jul 2021 18:05:25 +0000 (13:05 -0500)
lib/pleroma/web/admin_api/controllers/admin_api_controller.ex
lib/pleroma/web/admin_api/controllers/instance_controller.ex [new file with mode: 0644]
lib/pleroma/web/router.ex
test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs
test/pleroma/web/admin_api/controllers/instance_controller_test.exs [new file with mode: 0644]

index 839ac1a8d9185381f56cc84ff14122da63cfb899..50aa294f0ff0aed48ac6089323a47a4a6e2353b7 100644 (file)
@@ -49,7 +49,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   plug(
     OAuthScopesPlug,
     %{scopes: ["admin:read:statuses"]}
-    when action in [:list_user_statuses, :list_instance_statuses]
+    when action in [:list_user_statuses]
   )
 
   plug(
@@ -81,24 +81,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
 
   action_fallback(AdminAPI.FallbackController)
 
-  def list_instance_statuses(conn, %{"instance" => instance} = params) do
-    with_reblogs = params["with_reblogs"] == "true" || params["with_reblogs"] == true
-    {page, page_size} = page_params(params)
-
-    result =
-      ActivityPub.fetch_statuses(nil, %{
-        instance: instance,
-        limit: page_size,
-        offset: (page - 1) * page_size,
-        exclude_reblogs: not with_reblogs,
-        total: true
-      })
-
-    conn
-    |> put_view(AdminAPI.StatusView)
-    |> render("index.json", %{total: result[:total], activities: result[:items], as: :activity})
-  end
-
   def list_user_statuses(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname} = params) do
     with_reblogs = params["with_reblogs"] == "true" || params["with_reblogs"] == true
     godmode = params["godmode"] == "true" || params["godmode"] == true
diff --git a/lib/pleroma/web/admin_api/controllers/instance_controller.ex b/lib/pleroma/web/admin_api/controllers/instance_controller.ex
new file mode 100644 (file)
index 0000000..ad6ea89
--- /dev/null
@@ -0,0 +1,50 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.AdminAPI.InstanceController do
+  use Pleroma.Web, :controller
+
+  import Pleroma.Web.ControllerHelper, only: [fetch_integer_param: 3]
+
+  alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.AdminAPI
+  alias Pleroma.Web.Plugs.OAuthScopesPlug
+
+  require Logger
+
+  @default_page_size 50
+
+  plug(
+    OAuthScopesPlug,
+    %{scopes: ["admin:read:statuses"]}
+    when action in [:list_instance_statuses]
+  )
+
+  action_fallback(AdminAPI.FallbackController)
+
+  def list_instance_statuses(conn, %{"instance" => instance} = params) do
+    with_reblogs = params["with_reblogs"] == "true" || params["with_reblogs"] == true
+    {page, page_size} = page_params(params)
+
+    result =
+      ActivityPub.fetch_statuses(nil, %{
+        instance: instance,
+        limit: page_size,
+        offset: (page - 1) * page_size,
+        exclude_reblogs: not with_reblogs,
+        total: true
+      })
+
+    conn
+    |> put_view(AdminAPI.StatusView)
+    |> render("index.json", %{total: result[:total], activities: result[:items], as: :activity})
+  end
+
+  defp page_params(params) do
+    {
+      fetch_integer_param(params, "page", 1),
+      fetch_integer_param(params, "page_size", @default_page_size)
+    }
+  end
+end
index 72ad14f052a39e56f10e8262e07406f52620f97b..dbc7df104eaa5cdab68f7cece2dc60a2e14b16a9 100644 (file)
@@ -209,7 +209,7 @@ defmodule Pleroma.Web.Router do
     get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses)
     get("/users/:nickname/chats", AdminAPIController, :list_user_chats)
 
-    get("/instances/:instance/statuses", AdminAPIController, :list_instance_statuses)
+    get("/instances/:instance/statuses", InstanceController, :list_instance_statuses)
 
     get("/instance_document/:name", InstanceDocumentController, :show)
     patch("/instance_document/:name", InstanceDocumentController, :update)
index 8cd9f939b20312ca64553e47a1b39c78b9ba63c3..a9a0c800b873931b47f3eb02bc0cb370d44cc230 100644 (file)
@@ -800,40 +800,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     end
   end
 
-  describe "instances" do
-    test "GET /instances/:instance/statuses", %{conn: conn} do
-      user = insert(:user, local: false, ap_id: "https://archae.me/users/archaeme")
-      user2 = insert(:user, local: false, ap_id: "https://test.com/users/test")
-      insert_pair(:note_activity, user: user)
-      activity = insert(:note_activity, user: user2)
-
-      %{"total" => 2, "activities" => activities} =
-        conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
-
-      assert length(activities) == 2
-
-      %{"total" => 1, "activities" => [_]} =
-        conn |> get("/api/pleroma/admin/instances/test.com/statuses") |> json_response(200)
-
-      %{"total" => 0, "activities" => []} =
-        conn |> get("/api/pleroma/admin/instances/nonexistent.com/statuses") |> json_response(200)
-
-      CommonAPI.repeat(activity.id, user)
-
-      %{"total" => 2, "activities" => activities} =
-        conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
-
-      assert length(activities) == 2
-
-      %{"total" => 3, "activities" => activities} =
-        conn
-        |> get("/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true")
-        |> json_response(200)
-
-      assert length(activities) == 3
-    end
-  end
-
   describe "PATCH /confirm_email" do
     test "it confirms emails of two users", %{conn: conn, admin: admin} do
       [first_user, second_user] = insert_pair(:user, is_confirmed: false)
diff --git a/test/pleroma/web/admin_api/controllers/instance_controller_test.exs b/test/pleroma/web/admin_api/controllers/instance_controller_test.exs
new file mode 100644 (file)
index 0000000..b70efcf
--- /dev/null
@@ -0,0 +1,64 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.AdminAPI.InstanceControllerTest do
+  use Pleroma.Web.ConnCase
+  use Oban.Testing, repo: Pleroma.Repo
+
+  import Pleroma.Factory
+
+  alias Pleroma.Web.CommonAPI
+
+  setup_all do
+    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
+
+    :ok
+  end
+
+  setup do
+    admin = insert(:user, is_admin: true)
+    token = insert(:oauth_admin_token, user: admin)
+
+    conn =
+      build_conn()
+      |> assign(:user, admin)
+      |> assign(:token, token)
+
+    {:ok, %{admin: admin, token: token, conn: conn}}
+  end
+
+  describe "instances" do
+    test "GET /instances/:instance/statuses", %{conn: conn} do
+      user = insert(:user, local: false, ap_id: "https://archae.me/users/archaeme")
+      user2 = insert(:user, local: false, ap_id: "https://test.com/users/test")
+      insert_pair(:note_activity, user: user)
+      activity = insert(:note_activity, user: user2)
+
+      %{"total" => 2, "activities" => activities} =
+        conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
+
+      assert length(activities) == 2
+
+      %{"total" => 1, "activities" => [_]} =
+        conn |> get("/api/pleroma/admin/instances/test.com/statuses") |> json_response(200)
+
+      %{"total" => 0, "activities" => []} =
+        conn |> get("/api/pleroma/admin/instances/nonexistent.com/statuses") |> json_response(200)
+
+      CommonAPI.repeat(activity.id, user)
+
+      %{"total" => 2, "activities" => activities} =
+        conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
+
+      assert length(activities) == 2
+
+      %{"total" => 3, "activities" => activities} =
+        conn
+        |> get("/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true")
+        |> json_response(200)
+
+      assert length(activities) == 3
+    end
+  end
+end