Add search users endpoint

This commit is contained in:
Maxim Filippov 2019-02-28 19:04:47 +03:00
parent 70e82a3465
commit 46f29b9da1
4 changed files with 41 additions and 5 deletions

View 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),

View 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)

View 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)

View 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