From f62019983605dd9af9017351a59b52807bb74ba1 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 4 Mar 2019 21:26:32 +0300 Subject: [PATCH 1/5] Merge search endpoint into /users --- lib/pleroma/user.ex | 47 +++++++----- .../web/admin_api/admin_api_controller.ex | 33 +++++---- lib/pleroma/web/router.ex | 1 - .../admin_api/admin_api_controller_test.exs | 73 +++++++++++++------ 4 files changed, 97 insertions(+), 57 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 50e7e7ccd..3878e4efa 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -749,13 +749,41 @@ def get_recipients_from_activity(%Activity{recipients: to}) do Repo.all(query) end - @spec search_for_admin(binary(), %{ + @spec search_for_admin(%{ + local: boolean(), + page: number(), + page_size: number() + }) :: {:ok, [Pleroma.User.t()], number()} + def search_for_admin(%{query: nil, local: local, page: page, page_size: page_size}) do + query = + from(u in User, order_by: u.id) + |> maybe_local_user_query(local) + + paginated_query = + query + |> paginate(page, page_size) + + count = + query + |> Repo.aggregate(:count, :id) + + {:ok, Repo.all(paginated_query), count} + end + + @spec search_for_admin(%{ + query: binary(), admin: Pleroma.User.t(), local: boolean(), page: number(), page_size: number() }) :: {:ok, [Pleroma.User.t()], number()} - def search_for_admin(term, %{admin: admin, local: local, page: page, page_size: page_size}) do + def search_for_admin(%{ + query: term, + admin: admin, + local: local, + page: page, + page_size: page_size + }) do term = String.trim_leading(term, "@") local_paginated_query = @@ -774,21 +802,6 @@ def search_for_admin(term, %{admin: admin, local: local, page: page, page_size: {:ok, do_search(search_query, admin), count} end - @spec all_for_admin(number(), number()) :: {:ok, [Pleroma.User.t()], number()} - def all_for_admin(page, page_size) do - query = from(u in User, order_by: u.id) - - paginated_query = - query - |> paginate(page, page_size) - - count = - query - |> Repo.aggregate(:count, :id) - - {:ok, Repo.all(paginated_query), count} - end - def search(query, resolve \\ false, for_user \\ nil) do # Strip the beginning @ off if there is a query query = String.trim_leading(query, "@") diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index aae02cab8..12b36a1c0 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -63,28 +63,29 @@ def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do do: json_response(conn, :no_content, "") end - def list_users(conn, params) do - {page, page_size} = page_params(params) + # def list_users(conn, params) do + # {page, page_size} = page_params(params) - with {:ok, users, count} <- User.all_for_admin(page, page_size), - do: - conn - |> json( - AccountView.render("index.json", - users: users, - count: count, - page_size: page_size - ) - ) - end + # with {:ok, users, count} <- User.all_for_admin(page, page_size), + # do: + # conn + # |> json( + # AccountView.render("index.json", + # users: users, + # count: count, + # page_size: page_size + # ) + # ) + # end - def search_users(%{assigns: %{user: admin}} = conn, %{"query" => query} = params) do + def list_users(%{assigns: %{user: admin}} = conn, params) do {page, page_size} = page_params(params) with {:ok, users, count} <- - User.search_for_admin(query, %{ + User.search_for_admin(%{ + query: params["query"], admin: admin, - local: params["local"] == "true", + local: params["local_only"] == "true", page: page, page_size: page_size }), diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 6fcb46878..3b1fd46a5 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -140,7 +140,6 @@ defmodule Pleroma.Web.Router do pipe_through([:admin_api, :oauth_write]) get("/users", AdminAPIController, :list_users) - get("/users/search", AdminAPIController, :search_users) delete("/user", AdminAPIController, :user_delete) patch("/users/:nickname/toggle_activation", AdminAPIController, :user_toggle_activation) post("/user", AdminAPIController, :user_create) diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 42e0daf8e..dd40b4a06 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -374,26 +374,7 @@ test "renders empty array for the second page" do "users" => [] } end - end - test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do - admin = insert(:user, info: %{is_admin: true}) - user = insert(:user) - - conn = - build_conn() - |> assign(:user, admin) - |> patch("/api/pleroma/admin/users/#{user.nickname}/toggle_activation") - - assert json_response(conn, 200) == - %{ - "deactivated" => !user.info.deactivated, - "id" => user.id, - "nickname" => user.nickname - } - end - - describe "GET /api/pleroma/admin/users/search" do test "regular search" do admin = insert(:user, info: %{is_admin: true}) user = insert(:user, nickname: "bob") @@ -401,7 +382,7 @@ test "regular search" do conn = build_conn() |> assign(:user, admin) - |> get("/api/pleroma/admin/users/search?query=bo") + |> get("/api/pleroma/admin/users?query=bo") assert json_response(conn, 200) == %{ "count" => 1, @@ -424,7 +405,7 @@ test "regular search with page size" do conn = build_conn() |> assign(:user, admin) - |> get("/api/pleroma/admin/users/search?query=bo&page_size=1&page=1") + |> get("/api/pleroma/admin/users?query=bo&page_size=1&page=1") assert json_response(conn, 200) == %{ "count" => 2, @@ -441,7 +422,7 @@ test "regular search with page size" do conn = build_conn() |> assign(:user, admin) - |> get("/api/pleroma/admin/users/search?query=bo&page_size=1&page=2") + |> get("/api/pleroma/admin/users?query=bo&page_size=1&page=2") assert json_response(conn, 200) == %{ "count" => 2, @@ -465,7 +446,7 @@ test "only local users" do conn = build_conn() |> assign(:user, admin) - |> get("/api/pleroma/admin/users/search?query=bo&local=true") + |> get("/api/pleroma/admin/users?query=bo&local_only=true") assert json_response(conn, 200) == %{ "count" => 1, @@ -479,5 +460,51 @@ test "only local users" do ] } end + + test "only local users with no query" do + admin = insert(:user, info: %{is_admin: true}, nickname: "john") + user = insert(:user, nickname: "bob") + + insert(:user, nickname: "bobb", local: false) + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/users?local_only=true") + + assert json_response(conn, 200) == %{ + "count" => 2, + "page_size" => 50, + "users" => [ + %{ + "deactivated" => admin.info.deactivated, + "id" => admin.id, + "nickname" => admin.nickname + }, + %{ + "deactivated" => user.info.deactivated, + "id" => user.id, + "nickname" => user.nickname + } + ] + } + end + end + + test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do + admin = insert(:user, info: %{is_admin: true}) + user = insert(:user) + + conn = + build_conn() + |> assign(:user, admin) + |> patch("/api/pleroma/admin/users/#{user.nickname}/toggle_activation") + + assert json_response(conn, 200) == + %{ + "deactivated" => !user.info.deactivated, + "id" => user.id, + "nickname" => user.nickname + } end end From e34710b9888d5a3602971111c3a376bf8442fb84 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 4 Mar 2019 21:33:53 +0300 Subject: [PATCH 2/5] Format & update docs --- docs/Admin-API.md | 35 +++---------------- .../admin_api/admin_api_controller_test.exs | 10 +++--- 2 files changed, 10 insertions(+), 35 deletions(-) diff --git a/docs/Admin-API.md b/docs/Admin-API.md index 407647645..7f60b768a 100644 --- a/docs/Admin-API.md +++ b/docs/Admin-API.md @@ -2,41 +2,16 @@ Authentication is required and the user must be an admin. -## `/api/pleroma/admin/users` +## `/api/pleroma/admin/users?query={query}&local={local}&page={page}&page_size={page_size}` ### List users - Method `GET` - Params: - - `page`: **integer** page number - - `page_size`: **integer** number of users per page (default is `50`) -- Response: - -```JSON -{ - "page_size": integer, - "count": integer, - "users": [ - { - "deactivated": bool, - "id": integer, - "nickname": string - }, - ... - ] -} -``` - -## `/api/pleroma/admin/users/search?query={query}&local={local}&page={page}&page_size={page_size}` - -### Search users by name or nickname - -- Method `GET` -- Params: - - `query`: **string** search term - - `local`: **bool** whether to return only local users - - `page`: **integer** page number - - `page_size`: **integer** number of users per page (default is `50`) + - `query`: **string** *optional* search term + - `local`: **bool** *optional* whether to return only local users + - `page`: **integer** *optional* page number + - `page_size`: **integer** *optional* number of users per page (default is `50`) - Response: ```JSON diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index dd40b4a06..1b8b4d4b7 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -476,11 +476,11 @@ test "only local users with no query" do "count" => 2, "page_size" => 50, "users" => [ - %{ - "deactivated" => admin.info.deactivated, - "id" => admin.id, - "nickname" => admin.nickname - }, + %{ + "deactivated" => admin.info.deactivated, + "id" => admin.id, + "nickname" => admin.nickname + }, %{ "deactivated" => user.info.deactivated, "id" => user.id, From 02359d686ca5f7feb1b83d77a35118bd55efdb52 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 4 Mar 2019 21:36:47 +0300 Subject: [PATCH 3/5] local -> only_local --- docs/Admin-API.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Admin-API.md b/docs/Admin-API.md index 7f60b768a..24944440a 100644 --- a/docs/Admin-API.md +++ b/docs/Admin-API.md @@ -2,14 +2,14 @@ Authentication is required and the user must be an admin. -## `/api/pleroma/admin/users?query={query}&local={local}&page={page}&page_size={page_size}` +## `/api/pleroma/admin/users?query={query}&only_local={only_local}&page={page}&page_size={page_size}` ### List users - Method `GET` - Params: - `query`: **string** *optional* search term - - `local`: **bool** *optional* whether to return only local users + - `only_local`: **bool** *optional* whether to return only local users - `page`: **integer** *optional* page number - `page_size`: **integer** *optional* number of users per page (default is `50`) - Response: From 2d30fc279f2a3a5af68918c32c474a2fe8eaf664 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Tue, 5 Mar 2019 02:11:15 +0300 Subject: [PATCH 4/5] Typo --- docs/Admin-API.md | 2 +- lib/pleroma/web/admin_api/admin_api_controller.ex | 15 --------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/docs/Admin-API.md b/docs/Admin-API.md index 24944440a..64e112f9b 100644 --- a/docs/Admin-API.md +++ b/docs/Admin-API.md @@ -9,7 +9,7 @@ Authentication is required and the user must be an admin. - Method `GET` - Params: - `query`: **string** *optional* search term - - `only_local`: **bool** *optional* whether to return only local users + - `local_only`: **bool** *optional* whether to return only local users - `page`: **integer** *optional* page number - `page_size`: **integer** *optional* number of users per page (default is `50`) - Response: diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index 12b36a1c0..75c2c6061 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -63,21 +63,6 @@ def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do do: json_response(conn, :no_content, "") end - # def list_users(conn, params) do - # {page, page_size} = page_params(params) - - # with {:ok, users, count} <- User.all_for_admin(page, page_size), - # do: - # conn - # |> json( - # AccountView.render("index.json", - # users: users, - # count: count, - # page_size: page_size - # ) - # ) - # end - def list_users(%{assigns: %{user: admin}} = conn, params) do {page, page_size} = page_params(params) From 76160122f6d7bf52aee929328acb4d216e4c3504 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Wed, 6 Mar 2019 05:01:38 +0300 Subject: [PATCH 5/5] Keep heading short --- docs/Admin-API.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Admin-API.md b/docs/Admin-API.md index 64e112f9b..2edb31f3c 100644 --- a/docs/Admin-API.md +++ b/docs/Admin-API.md @@ -2,12 +2,12 @@ Authentication is required and the user must be an admin. -## `/api/pleroma/admin/users?query={query}&only_local={only_local}&page={page}&page_size={page_size}` +## `/api/pleroma/admin/users` ### List users - Method `GET` -- Params: +- Query Params: - `query`: **string** *optional* search term - `local_only`: **bool** *optional* whether to return only local users - `page`: **integer** *optional* page number