Merge remote-tracking branch 'pleroma/develop' into feature/disable-account
[akkoma] / lib / pleroma / web / admin_api / search.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.Search do
6 import Ecto.Query
7
8 alias Pleroma.Repo
9 alias Pleroma.User
10
11 @page_size 50
12
13 def user(%{query: term} = params) when is_nil(term) or term == "" do
14 query = maybe_filtered_query(params)
15
16 paginated_query =
17 maybe_filtered_query(params)
18 |> paginate(params[:page] || 1, params[:page_size] || @page_size)
19
20 count = query |> Repo.aggregate(:count, :id)
21
22 results = Repo.all(paginated_query)
23
24 {:ok, results, count}
25 end
26
27 def user(%{query: term} = params) when is_binary(term) do
28 search_query = from(u in maybe_filtered_query(params), where: ilike(u.nickname, ^"%#{term}%"))
29
30 count = search_query |> Repo.aggregate(:count, :id)
31
32 results =
33 search_query
34 |> paginate(params[:page] || 1, params[:page_size] || @page_size)
35 |> Repo.all()
36
37 {:ok, results, count}
38 end
39
40 defp maybe_filtered_query(params) do
41 from(u in User, order_by: u.nickname)
42 |> User.maybe_local_user_query(params[:local])
43 |> User.maybe_external_user_query(params[:external])
44 |> User.maybe_active_user_query(params[:active])
45 |> User.maybe_deactivated_user_query(params[:deactivated])
46 end
47
48 defp paginate(query, page, page_size) do
49 from(u in query,
50 limit: ^page_size,
51 offset: ^((page - 1) * page_size)
52 )
53 end
54 end