Merge branch 'fix/1726-user-pagination' into 'develop'
authorlain <lain@soykaf.club>
Wed, 6 May 2020 09:01:42 +0000 (09:01 +0000)
committerlain <lain@soykaf.club>
Wed, 6 May 2020 09:01:42 +0000 (09:01 +0000)
Pagination fix for service users filters in admin api

Closes #1726

See merge request pleroma/pleroma!2452

1  2 
lib/pleroma/web/admin_api/admin_api_controller.ex
test/web/admin_api/admin_api_controller_test.exs

index ac661e51522dd166cb9577af0c900465bb46cb21,bfcc81cb881aac8128655644618b0b0f20de40d7..da71e63d9a6f5da7ab1084314a7ba761847434b3
@@@ -93,7 -93,7 +93,7 @@@ defmodule Pleroma.Web.AdminAPI.AdminAPI
    plug(
      OAuthScopesPlug,
      %{scopes: ["read:statuses"], admin: true}
 -    when action in [:list_statuses, :list_user_statuses, :list_instance_statuses]
 +    when action in [:list_statuses, :list_user_statuses, :list_instance_statuses, :status_show]
    )
  
    plug(
        email: params["email"]
      }
  
-     with {:ok, users, count} <- Search.user(Map.merge(search_params, filters)),
-          {:ok, users, count} <- filter_service_users(users, count),
-          do:
-            conn
-            |> json(
-              AccountView.render("index.json",
-                users: users,
-                count: count,
-                page_size: page_size
-              )
-            )
-   end
-   defp filter_service_users(users, count) do
-     filtered_users = Enum.reject(users, &service_user?/1)
-     count = if Enum.any?(users, &service_user?/1), do: length(filtered_users), else: count
-     {:ok, filtered_users, count}
-   end
-   defp service_user?(user) do
-     String.match?(user.ap_id, ~r/.*\/relay$/) or
-       String.match?(user.ap_id, ~r/.*\/internal\/fetch$/)
+     with {:ok, users, count} <- Search.user(Map.merge(search_params, filters)) do
+       json(
+         conn,
+         AccountView.render("index.json", users: users, count: count, page_size: page_size)
+       )
+     end
    end
  
    @filters ~w(local external active deactivated is_admin is_moderator)
      |> render("index.json", %{activities: activities, as: :activity, skip_relationships: false})
    end
  
 +  def status_show(conn, %{"id" => id}) do
 +    with %Activity{} = activity <- Activity.get_by_id(id) do
 +      conn
 +      |> put_view(StatusView)
 +      |> render("show.json", %{activity: activity})
 +    else
 +      _ -> errors(conn, {:error, :not_found})
 +    end
 +  end
 +
    def status_update(%{assigns: %{user: admin}} = conn, %{"id" => id} = params) do
      with {:ok, activity} <- CommonAPI.update_activity_scope(id, params) do
        {:ok, sensitive} = Ecto.Type.cast(:boolean, params["sensitive"])
index c3f3ad0516d8e2fffa817f1a455fd4195a4a72d4,d798412e3069f27fbdd26b50808d009a33de7635..78c79bb0773476a006a2fe6979116ec01535a032
@@@ -19,6 -19,7 +19,7 @@@ defmodule Pleroma.Web.AdminAPI.AdminAPI
    alias Pleroma.Tests.ObanHelpers
    alias Pleroma.User
    alias Pleroma.UserInviteToken
+   alias Pleroma.Web
    alias Pleroma.Web.ActivityPub.Relay
    alias Pleroma.Web.CommonAPI
    alias Pleroma.Web.MediaProxy
               }
      end
  
+     test "pagination works correctly with service users", %{conn: conn} do
+       service1 = insert(:user, ap_id: Web.base_url() <> "/relay")
+       service2 = insert(:user, ap_id: Web.base_url() <> "/internal/fetch")
+       insert_list(25, :user)
+       assert %{"count" => 26, "page_size" => 10, "users" => users1} =
+                conn
+                |> get("/api/pleroma/admin/users?page=1&filters=", %{page_size: "10"})
+                |> json_response(200)
+       assert Enum.count(users1) == 10
+       assert service1 not in [users1]
+       assert service2 not in [users1]
+       assert %{"count" => 26, "page_size" => 10, "users" => users2} =
+                conn
+                |> get("/api/pleroma/admin/users?page=2&filters=", %{page_size: "10"})
+                |> json_response(200)
+       assert Enum.count(users2) == 10
+       assert service1 not in [users2]
+       assert service2 not in [users2]
+       assert %{"count" => 26, "page_size" => 10, "users" => users3} =
+                conn
+                |> get("/api/pleroma/admin/users?page=3&filters=", %{page_size: "10"})
+                |> json_response(200)
+       assert Enum.count(users3) == 6
+       assert service1 not in [users3]
+       assert service2 not in [users3]
+     end
      test "renders empty array for the second page", %{conn: conn} do
        insert(:user)
  
  
        {:ok, %{id: report_id}} =
          CommonAPI.report(reporter, %{
 -          "account_id" => target_user.id,
 -          "comment" => "I feel offended",
 -          "status_ids" => [activity.id]
 +          account_id: target_user.id,
 +          comment: "I feel offended",
 +          status_ids: [activity.id]
          })
  
        response =
  
        {:ok, %{id: report_id}} =
          CommonAPI.report(reporter, %{
 -          "account_id" => target_user.id,
 -          "comment" => "I feel offended",
 -          "status_ids" => [activity.id]
 +          account_id: target_user.id,
 +          comment: "I feel offended",
 +          status_ids: [activity.id]
          })
  
        {:ok, %{id: second_report_id}} =
          CommonAPI.report(reporter, %{
 -          "account_id" => target_user.id,
 -          "comment" => "I feel very offended",
 -          "status_ids" => [activity.id]
 +          account_id: target_user.id,
 +          comment: "I feel very offended",
 +          status_ids: [activity.id]
          })
  
        %{
  
        {:ok, %{id: report_id}} =
          CommonAPI.report(reporter, %{
 -          "account_id" => target_user.id,
 -          "comment" => "I feel offended",
 -          "status_ids" => [activity.id]
 +          account_id: target_user.id,
 +          comment: "I feel offended",
 +          status_ids: [activity.id]
          })
  
        response =
  
        {:ok, %{id: first_report_id}} =
          CommonAPI.report(reporter, %{
 -          "account_id" => target_user.id,
 -          "comment" => "I feel offended",
 -          "status_ids" => [activity.id]
 +          account_id: target_user.id,
 +          comment: "I feel offended",
 +          status_ids: [activity.id]
          })
  
        {:ok, %{id: second_report_id}} =
          CommonAPI.report(reporter, %{
 -          "account_id" => target_user.id,
 -          "comment" => "I don't like this user"
 +          account_id: target_user.id,
 +          comment: "I don't like this user"
          })
  
        CommonAPI.update_report_state(second_report_id, "closed")
      end
    end
  
 +  describe "GET /api/pleroma/admin/statuses/:id" do
 +    test "not found", %{conn: conn} do
 +      assert conn
 +             |> get("/api/pleroma/admin/statuses/not_found")
 +             |> json_response(:not_found)
 +    end
 +
 +    test "shows activity", %{conn: conn} do
 +      activity = insert(:note_activity)
 +
 +      response =
 +        conn
 +        |> get("/api/pleroma/admin/statuses/#{activity.id}")
 +        |> json_response(200)
 +
 +      assert response["id"] == activity.id
 +    end
 +  end
 +
    describe "PUT /api/pleroma/admin/statuses/:id" do
      setup do
        activity = insert(:note_activity)
  
        {:ok, %{id: report_id}} =
          CommonAPI.report(reporter, %{
 -          "account_id" => target_user.id,
 -          "comment" => "I feel offended",
 -          "status_ids" => [activity.id]
 +          account_id: target_user.id,
 +          comment: "I feel offended",
 +          status_ids: [activity.id]
          })
  
        post(conn, "/api/pleroma/admin/reports/#{report_id}/notes", %{
      end
  
      test "success", %{conn: conn} do
-       base_url = Pleroma.Web.base_url()
+       base_url = Web.base_url()
        app_name = "Trusted app"
  
        response =
      end
  
      test "with trusted", %{conn: conn} do
-       base_url = Pleroma.Web.base_url()
+       base_url = Web.base_url()
        app_name = "Trusted app"
  
        response =