added total
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 21 Jan 2021 16:17:37 +0000 (19:17 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Wed, 27 Jan 2021 04:45:06 +0000 (07:45 +0300)
to the instance adminAPI endpoint

CHANGELOG.md
docs/development/API/admin_api.md
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/admin_api/controllers/admin_api_controller.ex
test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs

index a6b1f31dba7ca60687d1380bc5c2f3dc683d0725..b4fa2317730d3786466da30385845bc886a6fbe6 100644 (file)
@@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - **Breaking:** AdminAPI changed User field `approval_pending` to `is_approved`
 - **Breaking**: AdminAPI changed User field `deactivated` to `is_active`
 - **Breaking:** AdminAPI `GET /api/pleroma/admin/users/:nickname_or_id/statuses` changed response format and added the number of total users posts.
+- **Breaking:** AdminAPI `GET /api/pleroma/admin/instances/:instance/statuses` changed response format and added the number of total users posts.
 - Admin API: Reports now ordered by newest
 
 </details>
index 5b75a7b01adce6175e2d1aa6d3e0e5ab0fa66cc0..04a1814016cf86c03b85bbc2c16064242a82297f 100644 (file)
@@ -311,7 +311,18 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
   - *optional* `with_reblogs`: `true`/`false` – allows to see reblogs (default is false)
 - Response:
   - On failure: `Not found`
-  - On success: JSON array of instance's latest statuses
+  - On success: JSON, where:
+    - `total`: total count of the statuses for the instance
+    - `activities`: list of the statuses for the instance
+
+```json
+{
+  "total" : 1,
+  "activities": [
+    // activities list
+  ]
+}
+```
 
 ## `GET /api/pleroma/admin/statuses`
 
index 9ec10674922a1a9d15fd02749a1aa213bd218499..59e1e884b8450418d5f73fc97c6f007b7cd0481a 100644 (file)
@@ -632,7 +632,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> fetch_activities(params, pagination_type)
   end
 
+  def fetch_statuses(reading_user, %{total: true} = params) do
+    result = fetch_activities_for_reading_user(reading_user, params)
+    Keyword.put(result, :items, Enum.reverse(result[:items]))
+  end
+
   def fetch_statuses(reading_user, params) do
+    reading_user
+    |> fetch_activities_for_reading_user(params)
+    |> Enum.reverse()
+  end
+
+  defp fetch_activities_for_reading_user(reading_user, params) do
     params = Map.put(params, :type, ["Create", "Announce"])
 
     %{
@@ -641,7 +652,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     }
     |> user_activities_recipients()
     |> fetch_activities(params, :offset)
-    |> Enum.reverse()
   end
 
   defp user_activities_recipients(%{godmode: true}), do: []
index 500556710a579605998ba3e5c2c8a2b67e6d3259..8f89f066ab2eefce89654a58cb5b958bb1b97a18 100644 (file)
@@ -85,17 +85,18 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     with_reblogs = params["with_reblogs"] == "true" || params["with_reblogs"] == true
     {page, page_size} = page_params(params)
 
-    activities =
+    result =
       ActivityPub.fetch_statuses(nil, %{
         instance: instance,
         limit: page_size,
         offset: (page - 1) * page_size,
-        exclude_reblogs: not with_reblogs
+        exclude_reblogs: not with_reblogs,
+        total: true
       })
 
     conn
     |> put_view(AdminAPI.StatusView)
-    |> render("index.json", %{activities: activities, as: :activity})
+    |> render("index.json", %{total: result[:total], activities: result[:items], as: :activity})
   end
 
   def list_user_statuses(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname} = params) do
index fe35a26bdecf9f02da584a991f205c21ff4f7c0c..e7688c7287c894011a9b6e48663c0dd00e3639a4 100644 (file)
@@ -864,33 +864,30 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       insert_pair(:note_activity, user: user)
       activity = insert(:note_activity, user: user2)
 
-      ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses")
+      %{"total" => 2, "activities" => activities} =
+        conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
 
-      response = json_response(ret_conn, 200)
+      assert length(activities) == 2
 
-      assert length(response) == 2
+      %{"total" => 1, "activities" => [_]} =
+        conn |> get("/api/pleroma/admin/instances/test.com/statuses") |> json_response(200)
 
-      ret_conn = get(conn, "/api/pleroma/admin/instances/test.com/statuses")
+      %{"total" => 0, "activities" => []} =
+        conn |> get("/api/pleroma/admin/instances/nonexistent.com/statuses") |> json_response(200)
 
-      response = json_response(ret_conn, 200)
-
-      assert length(response) == 1
-
-      ret_conn = get(conn, "/api/pleroma/admin/instances/nonexistent.com/statuses")
-
-      response = json_response(ret_conn, 200)
+      CommonAPI.repeat(activity.id, user)
 
-      assert Enum.empty?(response)
+      %{"total" => 2, "activities" => activities} =
+        conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
 
-      CommonAPI.repeat(activity.id, user)
+      assert length(activities) == 2
 
-      ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses")
-      response = json_response(ret_conn, 200)
-      assert length(response) == 2
+      %{"total" => 3, "activities" => activities} =
+        conn
+        |> get("/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true")
+        |> json_response(200)
 
-      ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true")
-      response = json_response(ret_conn, 200)
-      assert length(response) == 3
+      assert length(activities) == 3
     end
   end