added total
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 21 Jan 2021 15:51:21 +0000 (18:51 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Wed, 27 Jan 2021 04:45:02 +0000 (07:45 +0300)
to the user statuses 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
lib/pleroma/web/admin_api/views/status_view.ex
test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs

index c4f3867a26d3d8797b0c4e5bcb8cd79ab8de6735..a6b1f31dba7ca60687d1380bc5c2f3dc683d0725 100644 (file)
@@ -10,18 +10,25 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 
 - **Breaking**: Changed `mix pleroma.user toggle_confirmed` to `mix pleroma.user confirm`
 - **Breaking**: Changed `mix pleroma.user toggle_activated` to `mix pleroma.user activate/deactivate`
-- **Breaking**: AdminAPI changed User field `confirmation_pending` to `is_confirmed`
-- **Breaking**: AdminAPI changed User field `approval_pending` to `is_approved`
-- **Breaking**: AdminAPI changed User field `deactivated` to `is_active`
 - Polls now always return a `voters_count`, even if they are single-choice.
 - Admin Emails: The ap id is used as the user link in emails now.
 - Improved registration workflow for email confirmation and account approval modes.
 - Search: When using Postgres 11+, Pleroma will use the `websearch_to_tsvector` function to parse search queries.
 - Emoji: Support the full Unicode 13.1 set of Emoji for reactions, plus regional indicators.
-- Admin API: Reports now ordered by newest
 - Deprecated `Pleroma.Uploaders.S3, :public_endpoint`. Now `Pleroma.Upload, :base_url` is the standard configuration key for all uploaders.
 - Improved Apache webserver support: updated sample configuration, MediaProxy cache invalidation verified with the included sample script
 
+<details>
+  <summary>API Changes</summary>
+
+- **Breaking:** AdminAPI changed User field `confirmation_pending` to `is_confirmed`
+- **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.
+- Admin API: Reports now ordered by newest
+
+</details>
+
 ### Added
 
 - Reports now generate notifications for admins and mods.
index 5253dc6689cce569d3cb78514d9bf7969e49e167..5b75a7b01adce6175e2d1aa6d3e0e5ab0fa66cc0 100644 (file)
@@ -287,7 +287,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 user's latest statuses
+ - On success: JSON, where:
+    - `total`: total count of the statuses for the user
+    - `activities`: list of the statuses for the user
+
+```json
+{
+  "total" : 1,
+  "activities": [
+    // activities list
+  ]
+}
+```
 
 ## `GET /api/pleroma/admin/instances/:instance/statuses`
 
index d0bb07aab8f2327a2acab59901bf1f7a671a2a70..9ec10674922a1a9d15fd02749a1aa213bd218499 100644 (file)
@@ -591,7 +591,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> Enum.reverse()
   end
 
-  def fetch_user_activities(user, reading_user, params \\ %{}) do
+  def fetch_user_activities(user, reading_user, params \\ %{})
+
+  def fetch_user_activities(user, reading_user, %{total: true} = params) do
+    result = fetch_activities_for_user(user, reading_user, params)
+
+    Keyword.put(result, :items, Enum.reverse(result[:items]))
+  end
+
+  def fetch_user_activities(user, reading_user, params) do
+    user
+    |> fetch_activities_for_user(reading_user, params)
+    |> Enum.reverse()
+  end
+
+  defp fetch_activities_for_user(user, reading_user, params) do
     params =
       params
       |> Map.put(:type, ["Create", "Announce"])
@@ -616,7 +630,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     }
     |> user_activities_recipients()
     |> fetch_activities(params, pagination_type)
-    |> Enum.reverse()
   end
 
   def fetch_statuses(reading_user, params) do
index 709c863ecc77181b5bb7ba1331677fb4e0c9c16a..500556710a579605998ba3e5c2c8a2b67e6d3259 100644 (file)
@@ -105,18 +105,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
       {page, page_size} = page_params(params)
 
-      activities =
+      result =
         ActivityPub.fetch_user_activities(user, nil, %{
           limit: page_size,
           offset: (page - 1) * page_size,
           godmode: godmode,
           exclude_reblogs: not with_reblogs,
-          pagination_type: :offset
+          pagination_type: :offset,
+          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})
     else
       _ -> {:error, :not_found}
     end
index 361fa5b0d604dbcd6f1dec13154c55d7c3232e6a..48d639b410bcb0ee230d195e5d9cdae5a0845482 100644 (file)
@@ -13,6 +13,10 @@ defmodule Pleroma.Web.AdminAPI.StatusView do
 
   defdelegate merge_account_views(user), to: AdminAPI.AccountView
 
+  def render("index.json", %{total: total} = opts) do
+    %{total: total, activities: safe_render_many(opts.activities, __MODULE__, "show.json", opts)}
+  end
+
   def render("index.json", opts) do
     safe_render_many(opts.activities, __MODULE__, "show.json", opts)
   end
index 23e4bc3af585ea12076ba0254e71d9a84f717bf0..fe35a26bdecf9f02da584a991f205c21ff4f7c0c 100644 (file)
@@ -405,13 +405,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     setup do
       user = insert(:user)
 
-      date1 = (DateTime.to_unix(DateTime.utc_now()) + 2000) |> DateTime.from_unix!()
-      date2 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
-      date3 = (DateTime.to_unix(DateTime.utc_now()) + 3000) |> DateTime.from_unix!()
-
-      insert(:note_activity, user: user, published: date1)
-      insert(:note_activity, user: user, published: date2)
-      insert(:note_activity, user: user, published: date3)
+      insert(:note_activity, user: user)
+      insert(:note_activity, user: user)
+      insert(:note_activity, user: user)
 
       %{user: user}
     end
@@ -419,23 +415,22 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     test "renders user's statuses", %{conn: conn, user: user} do
       conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses")
 
-      assert json_response(conn, 200) |> length() == 3
+      assert %{"total" => 3, "activities" => activities} = json_response(conn, 200)
+      assert length(activities) == 3
     end
 
     test "renders user's statuses with pagination", %{conn: conn, user: user} do
-      conn1 = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=1&page=1")
-
-      response1 = json_response(conn1, 200)
-
-      assert response1 |> length() == 1
-
-      conn2 = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=1&page=2")
-
-      response2 = json_response(conn2, 200)
+      %{"total" => 3, "activities" => [activity1]} =
+        conn
+        |> get("/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=1&page=1")
+        |> json_response(200)
 
-      assert response2 |> length() == 1
+      %{"total" => 3, "activities" => [activity2]} =
+        conn
+        |> get("/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=1&page=2")
+        |> json_response(200)
 
-      refute response1 == response2
+      refute activity1 == activity2
     end
 
     test "doesn't return private statuses by default", %{conn: conn, user: user} do
@@ -443,9 +438,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
       {:ok, _public_status} = CommonAPI.post(user, %{status: "public", visibility: "public"})
 
-      conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses")
+      %{"total" => 4, "activities" => activities} =
+        conn
+        |> get("/api/pleroma/admin/users/#{user.nickname}/statuses")
+        |> json_response(200)
 
-      assert json_response(conn, 200) |> length() == 4
+      assert length(activities) == 4
     end
 
     test "returns private statuses with godmode on", %{conn: conn, user: user} do
@@ -453,9 +451,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
       {:ok, _public_status} = CommonAPI.post(user, %{status: "public", visibility: "public"})
 
-      conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?godmode=true")
+      %{"total" => 5, "activities" => activities} =
+        conn
+        |> get("/api/pleroma/admin/users/#{user.nickname}/statuses?godmode=true")
+        |> json_response(200)
 
-      assert json_response(conn, 200) |> length() == 5
+      assert length(activities) == 5
     end
 
     test "excludes reblogs by default", %{conn: conn, user: user} do
@@ -463,13 +464,17 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       {:ok, activity} = CommonAPI.post(user, %{status: "."})
       {:ok, %Activity{}} = CommonAPI.repeat(activity.id, other_user)
 
-      conn_res = get(conn, "/api/pleroma/admin/users/#{other_user.nickname}/statuses")
-      assert json_response(conn_res, 200) |> length() == 0
-
-      conn_res =
-        get(conn, "/api/pleroma/admin/users/#{other_user.nickname}/statuses?with_reblogs=true")
+      assert %{"total" => 0, "activities" => []} ==
+               conn
+               |> get("/api/pleroma/admin/users/#{other_user.nickname}/statuses")
+               |> json_response(200)
 
-      assert json_response(conn_res, 200) |> length() == 1
+      assert %{"total" => 1, "activities" => [_]} =
+               conn
+               |> get(
+                 "/api/pleroma/admin/users/#{other_user.nickname}/statuses?with_reblogs=true"
+               )
+               |> json_response(200)
     end
   end