From 46f29b9da1cfdcc2ab14616f999f061fa0c87ddc Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Thu, 28 Feb 2019 19:04:47 +0300 Subject: [PATCH] Add search users endpoint --- lib/pleroma/user.ex | 8 +++---- .../web/admin_api/admin_api_controller.ex | 13 ++++++++++ lib/pleroma/web/router.ex | 1 + .../admin_api/admin_api_controller_test.exs | 24 ++++++++++++++++++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 80e4ae296..52df171c5 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -755,18 +755,18 @@ def get_recipients_from_activity(%Activity{recipients: to}) do Repo.all(query) end - def search(query, resolve \\ false, for_user \\ nil) do + def search(query, resolve \\ false, for_user \\ nil, limit \\ 20) do # Strip the beginning @ off if there is a query query = String.trim_leading(query, "@") if resolve, do: get_or_fetch(query) - fts_results = do_search(fts_search_subquery(query), for_user) + fts_results = do_search(fts_search_subquery(query), for_user, %{limit: limit}) {:ok, trigram_results} = Repo.transaction(fn -> Ecto.Adapters.SQL.query(Repo, "select set_limit(0.25)", []) - do_search(trigram_search_subquery(query), for_user) + do_search(trigram_search_subquery(query), for_user, %{limit: limit}) end) Enum.uniq_by(fts_results ++ trigram_results, & &1.id) @@ -793,7 +793,7 @@ def count_all_except_one(user) do Repo.aggregate(query, :count, :id) end - defp do_search(subquery, for_user, options \\ []) do + defp do_search(subquery, for_user, options) do q = from( s in subquery(subquery), diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index d8e3d57e1..37159cd40 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -78,6 +78,19 @@ def list_users(%{assigns: %{user: admin}} = conn, %{"page" => page_string}) do ) end + def search_users(%{assigns: %{user: admin}} = conn, %{"query" => query}) do + users = User.search(query, true, admin, @users_page_size) + + conn + |> json( + AccountView.render("index.json", %{ + users: users, + count: length(users), + page_size: @users_page_size + }) + ) + end + def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname}) when permission_group in ["moderator", "admin"] do user = User.get_by_nickname(nickname) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 3b1fd46a5..6fcb46878 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -140,6 +140,7 @@ 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 893387ef5..14625af32 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -356,7 +356,7 @@ test "renders users array for the first page" do test "renders empty array for the second page" do admin = insert(:user, info: %{is_admin: true}) - user = insert(:user) + insert(:user) conn = build_conn() @@ -387,4 +387,26 @@ test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do "nickname" => user.nickname } end + + test "GET /api/pleroma/admin/users/search" do + admin = insert(:user, info: %{is_admin: true}) + user = insert(:user, nickname: "bob") + + conn = + build_conn() + |> assign(:user, admin) + |> get("/api/pleroma/admin/users/search?query=bo") + + assert json_response(conn, 200) == %{ + "count" => 1, + "page_size" => 50, + "users" => [ + %{ + "deactivated" => user.info.deactivated, + "id" => user.id, + "nickname" => user.nickname + } + ] + } + end end