Add search users endpoint
This commit is contained in:
parent
70e82a3465
commit
46f29b9da1
4 changed files with 41 additions and 5 deletions
|
@ -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),
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue