Add search users endpoint
authorMaxim Filippov <colixer@gmail.com>
Thu, 28 Feb 2019 16:04:47 +0000 (19:04 +0300)
committerMaxim Filippov <colixer@gmail.com>
Thu, 28 Feb 2019 16:04:47 +0000 (19:04 +0300)
lib/pleroma/user.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/router.ex
test/web/admin_api/admin_api_controller_test.exs

index 80e4ae296f84acbcea43270f698323e59a7e4af4..52df171c55ea2933f79e0fd2160983de8072a12d 100644 (file)
@@ -755,18 +755,18 @@ defmodule Pleroma.User 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 @@ defmodule Pleroma.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),
index d8e3d57e1fd03ab7ba852706b0d19dbc98d7caf5..37159cd4017a84185f5032e864a1b3c0e79a62af 100644 (file)
@@ -78,6 +78,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController 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)
index 3b1fd46a564140234e8e1b71e1a24994131e89f4..6fcb46878c56fe217ca41da52244fbcc93e226ad 100644 (file)
@@ -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)
index 893387ef5425fb208cd0bdbb6ae94c361acc7d5f..14625af32b71c190bfec84879e9bd2e596cd26e8 100644 (file)
@@ -356,7 +356,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest 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 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest 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