logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git
commit: d4158e8bf01af3f998a0295668bada9821c4fdc7
parent 6806c03e8543c57ef85393eafdc6117d9776049f
Author: Alexander Strizhakov <alex.strizhakov@gmail.com>
Date:   Thu, 21 Jan 2021 19:17:37 +0300

added total

to the instance adminAPI endpoint

Diffstat:

MCHANGELOG.md1+
Mdocs/development/API/admin_api.md13++++++++++++-
Mlib/pleroma/web/activity_pub/activity_pub.ex12+++++++++++-
Mlib/pleroma/web/admin_api/controllers/admin_api_controller.ex7++++---
Mtest/pleroma/web/admin_api/controllers/admin_api_controller_test.exs35++++++++++++++++-------------------
5 files changed, 44 insertions(+), 24 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -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> diff --git a/docs/development/API/admin_api.md b/docs/development/API/admin_api.md @@ -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` diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -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: [] diff --git a/lib/pleroma/web/admin_api/controllers/admin_api_controller.ex b/lib/pleroma/web/admin_api/controllers/admin_api_controller.ex @@ -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 diff --git a/test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs b/test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs @@ -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