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)
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),
)
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)
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)
test "renders empty array for the second page" do
admin = insert(:user, info: %{is_admin: true})
- user = insert(:user)
+ insert(:user)
conn =
build_conn()
"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