Merge search endpoint into /users
This commit is contained in:
parent
ca5d894e68
commit
f620199836
@ -749,13 +749,41 @@ defmodule Pleroma.User do
|
|||||||
Repo.all(query)
|
Repo.all(query)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec search_for_admin(binary(), %{
|
@spec search_for_admin(%{
|
||||||
|
local: boolean(),
|
||||||
|
page: number(),
|
||||||
|
page_size: number()
|
||||||
|
}) :: {:ok, [Pleroma.User.t()], number()}
|
||||||
|
def search_for_admin(%{query: nil, local: local, page: page, page_size: page_size}) do
|
||||||
|
query =
|
||||||
|
from(u in User, order_by: u.id)
|
||||||
|
|> maybe_local_user_query(local)
|
||||||
|
|
||||||
|
paginated_query =
|
||||||
|
query
|
||||||
|
|> paginate(page, page_size)
|
||||||
|
|
||||||
|
count =
|
||||||
|
query
|
||||||
|
|> Repo.aggregate(:count, :id)
|
||||||
|
|
||||||
|
{:ok, Repo.all(paginated_query), count}
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec search_for_admin(%{
|
||||||
|
query: binary(),
|
||||||
admin: Pleroma.User.t(),
|
admin: Pleroma.User.t(),
|
||||||
local: boolean(),
|
local: boolean(),
|
||||||
page: number(),
|
page: number(),
|
||||||
page_size: number()
|
page_size: number()
|
||||||
}) :: {:ok, [Pleroma.User.t()], number()}
|
}) :: {:ok, [Pleroma.User.t()], number()}
|
||||||
def search_for_admin(term, %{admin: admin, local: local, page: page, page_size: page_size}) do
|
def search_for_admin(%{
|
||||||
|
query: term,
|
||||||
|
admin: admin,
|
||||||
|
local: local,
|
||||||
|
page: page,
|
||||||
|
page_size: page_size
|
||||||
|
}) do
|
||||||
term = String.trim_leading(term, "@")
|
term = String.trim_leading(term, "@")
|
||||||
|
|
||||||
local_paginated_query =
|
local_paginated_query =
|
||||||
@ -774,21 +802,6 @@ defmodule Pleroma.User do
|
|||||||
{:ok, do_search(search_query, admin), count}
|
{:ok, do_search(search_query, admin), count}
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec all_for_admin(number(), number()) :: {:ok, [Pleroma.User.t()], number()}
|
|
||||||
def all_for_admin(page, page_size) do
|
|
||||||
query = from(u in User, order_by: u.id)
|
|
||||||
|
|
||||||
paginated_query =
|
|
||||||
query
|
|
||||||
|> paginate(page, page_size)
|
|
||||||
|
|
||||||
count =
|
|
||||||
query
|
|
||||||
|> Repo.aggregate(:count, :id)
|
|
||||||
|
|
||||||
{:ok, Repo.all(paginated_query), count}
|
|
||||||
end
|
|
||||||
|
|
||||||
def search(query, resolve \\ false, for_user \\ nil) do
|
def search(query, resolve \\ false, for_user \\ nil) do
|
||||||
# Strip the beginning @ off if there is a query
|
# Strip the beginning @ off if there is a query
|
||||||
query = String.trim_leading(query, "@")
|
query = String.trim_leading(query, "@")
|
||||||
|
@ -63,28 +63,29 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|
|||||||
do: json_response(conn, :no_content, "")
|
do: json_response(conn, :no_content, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_users(conn, params) do
|
# def list_users(conn, params) do
|
||||||
{page, page_size} = page_params(params)
|
# {page, page_size} = page_params(params)
|
||||||
|
|
||||||
with {:ok, users, count} <- User.all_for_admin(page, page_size),
|
# with {:ok, users, count} <- User.all_for_admin(page, page_size),
|
||||||
do:
|
# do:
|
||||||
conn
|
# conn
|
||||||
|> json(
|
# |> json(
|
||||||
AccountView.render("index.json",
|
# AccountView.render("index.json",
|
||||||
users: users,
|
# users: users,
|
||||||
count: count,
|
# count: count,
|
||||||
page_size: page_size
|
# page_size: page_size
|
||||||
)
|
# )
|
||||||
)
|
# )
|
||||||
end
|
# end
|
||||||
|
|
||||||
def search_users(%{assigns: %{user: admin}} = conn, %{"query" => query} = params) do
|
def list_users(%{assigns: %{user: admin}} = conn, params) do
|
||||||
{page, page_size} = page_params(params)
|
{page, page_size} = page_params(params)
|
||||||
|
|
||||||
with {:ok, users, count} <-
|
with {:ok, users, count} <-
|
||||||
User.search_for_admin(query, %{
|
User.search_for_admin(%{
|
||||||
|
query: params["query"],
|
||||||
admin: admin,
|
admin: admin,
|
||||||
local: params["local"] == "true",
|
local: params["local_only"] == "true",
|
||||||
page: page,
|
page: page,
|
||||||
page_size: page_size
|
page_size: page_size
|
||||||
}),
|
}),
|
||||||
|
@ -140,7 +140,6 @@ defmodule Pleroma.Web.Router do
|
|||||||
pipe_through([:admin_api, :oauth_write])
|
pipe_through([:admin_api, :oauth_write])
|
||||||
|
|
||||||
get("/users", AdminAPIController, :list_users)
|
get("/users", AdminAPIController, :list_users)
|
||||||
get("/users/search", AdminAPIController, :search_users)
|
|
||||||
delete("/user", AdminAPIController, :user_delete)
|
delete("/user", AdminAPIController, :user_delete)
|
||||||
patch("/users/:nickname/toggle_activation", AdminAPIController, :user_toggle_activation)
|
patch("/users/:nickname/toggle_activation", AdminAPIController, :user_toggle_activation)
|
||||||
post("/user", AdminAPIController, :user_create)
|
post("/user", AdminAPIController, :user_create)
|
||||||
|
@ -374,26 +374,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||||||
"users" => []
|
"users" => []
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do
|
|
||||||
admin = insert(:user, info: %{is_admin: true})
|
|
||||||
user = insert(:user)
|
|
||||||
|
|
||||||
conn =
|
|
||||||
build_conn()
|
|
||||||
|> assign(:user, admin)
|
|
||||||
|> patch("/api/pleroma/admin/users/#{user.nickname}/toggle_activation")
|
|
||||||
|
|
||||||
assert json_response(conn, 200) ==
|
|
||||||
%{
|
|
||||||
"deactivated" => !user.info.deactivated,
|
|
||||||
"id" => user.id,
|
|
||||||
"nickname" => user.nickname
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "GET /api/pleroma/admin/users/search" do
|
|
||||||
test "regular search" do
|
test "regular search" do
|
||||||
admin = insert(:user, info: %{is_admin: true})
|
admin = insert(:user, info: %{is_admin: true})
|
||||||
user = insert(:user, nickname: "bob")
|
user = insert(:user, nickname: "bob")
|
||||||
@ -401,7 +382,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||||||
conn =
|
conn =
|
||||||
build_conn()
|
build_conn()
|
||||||
|> assign(:user, admin)
|
|> assign(:user, admin)
|
||||||
|> get("/api/pleroma/admin/users/search?query=bo")
|
|> get("/api/pleroma/admin/users?query=bo")
|
||||||
|
|
||||||
assert json_response(conn, 200) == %{
|
assert json_response(conn, 200) == %{
|
||||||
"count" => 1,
|
"count" => 1,
|
||||||
@ -424,7 +405,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||||||
conn =
|
conn =
|
||||||
build_conn()
|
build_conn()
|
||||||
|> assign(:user, admin)
|
|> assign(:user, admin)
|
||||||
|> get("/api/pleroma/admin/users/search?query=bo&page_size=1&page=1")
|
|> get("/api/pleroma/admin/users?query=bo&page_size=1&page=1")
|
||||||
|
|
||||||
assert json_response(conn, 200) == %{
|
assert json_response(conn, 200) == %{
|
||||||
"count" => 2,
|
"count" => 2,
|
||||||
@ -441,7 +422,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||||||
conn =
|
conn =
|
||||||
build_conn()
|
build_conn()
|
||||||
|> assign(:user, admin)
|
|> assign(:user, admin)
|
||||||
|> get("/api/pleroma/admin/users/search?query=bo&page_size=1&page=2")
|
|> get("/api/pleroma/admin/users?query=bo&page_size=1&page=2")
|
||||||
|
|
||||||
assert json_response(conn, 200) == %{
|
assert json_response(conn, 200) == %{
|
||||||
"count" => 2,
|
"count" => 2,
|
||||||
@ -465,7 +446,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||||||
conn =
|
conn =
|
||||||
build_conn()
|
build_conn()
|
||||||
|> assign(:user, admin)
|
|> assign(:user, admin)
|
||||||
|> get("/api/pleroma/admin/users/search?query=bo&local=true")
|
|> get("/api/pleroma/admin/users?query=bo&local_only=true")
|
||||||
|
|
||||||
assert json_response(conn, 200) == %{
|
assert json_response(conn, 200) == %{
|
||||||
"count" => 1,
|
"count" => 1,
|
||||||
@ -479,5 +460,51 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "only local users with no query" do
|
||||||
|
admin = insert(:user, info: %{is_admin: true}, nickname: "john")
|
||||||
|
user = insert(:user, nickname: "bob")
|
||||||
|
|
||||||
|
insert(:user, nickname: "bobb", local: false)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
build_conn()
|
||||||
|
|> assign(:user, admin)
|
||||||
|
|> get("/api/pleroma/admin/users?local_only=true")
|
||||||
|
|
||||||
|
assert json_response(conn, 200) == %{
|
||||||
|
"count" => 2,
|
||||||
|
"page_size" => 50,
|
||||||
|
"users" => [
|
||||||
|
%{
|
||||||
|
"deactivated" => admin.info.deactivated,
|
||||||
|
"id" => admin.id,
|
||||||
|
"nickname" => admin.nickname
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
"deactivated" => user.info.deactivated,
|
||||||
|
"id" => user.id,
|
||||||
|
"nickname" => user.nickname
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do
|
||||||
|
admin = insert(:user, info: %{is_admin: true})
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
build_conn()
|
||||||
|
|> assign(:user, admin)
|
||||||
|
|> patch("/api/pleroma/admin/users/#{user.nickname}/toggle_activation")
|
||||||
|
|
||||||
|
assert json_response(conn, 200) ==
|
||||||
|
%{
|
||||||
|
"deactivated" => !user.info.deactivated,
|
||||||
|
"id" => user.id,
|
||||||
|
"nickname" => user.nickname
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user