X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fadmin_api%2Fsearch.ex;h=0bfb8f02261ec953ca675edc00cfb601de46986d;hb=0883a706dc376fdfb7de9df1366803e87c8e7c98;hp=9a8e41c2a7b1d62251d67664e6c04906c24dbb22;hpb=52bec238c3c80a246eb269de9afb1f9b3ea58f4f;p=akkoma diff --git a/lib/pleroma/web/admin_api/search.ex b/lib/pleroma/web/admin_api/search.ex index 9a8e41c2a..0bfb8f022 100644 --- a/lib/pleroma/web/admin_api/search.ex +++ b/lib/pleroma/web/admin_api/search.ex @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors +# Copyright © 2017-2020 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.AdminAPI.Search do @@ -10,45 +10,27 @@ defmodule Pleroma.Web.AdminAPI.Search do @page_size 50 - def user(%{query: term} = params) when is_nil(term) or term == "" do - query = maybe_filtered_query(params) - - paginated_query = - maybe_filtered_query(params) - |> paginate(params[:page] || 1, params[:page_size] || @page_size) - - count = query |> Repo.aggregate(:count, :id) - - results = Repo.all(paginated_query) - - {:ok, results, count} + defmacro not_empty_string(string) do + quote do + is_binary(unquote(string)) and unquote(string) != "" + end end - def user(%{query: term} = params) when is_binary(term) do - search_query = from(u in maybe_filtered_query(params), where: ilike(u.nickname, ^"%#{term}%")) + @spec user(map()) :: {:ok, [User.t()], pos_integer()} + def user(params \\ %{}) do + query = + params + |> Map.drop([:page, :page_size]) + |> Map.put(:invisible, false) + |> User.Query.build() + |> order_by([u], u.nickname) - count = search_query |> Repo.aggregate(:count, :id) + paginated_query = + User.Query.paginate(query, params[:page] || 1, params[:page_size] || @page_size) - results = - search_query - |> paginate(params[:page] || 1, params[:page_size] || @page_size) - |> Repo.all() + count = Repo.aggregate(query, :count, :id) + results = Repo.all(paginated_query) {:ok, results, count} end - - defp maybe_filtered_query(params) do - from(u in User, order_by: u.nickname) - |> User.maybe_local_user_query(params[:local]) - |> User.maybe_external_user_query(params[:external]) - |> User.maybe_active_user_query(params[:active]) - |> User.maybe_deactivated_user_query(params[:deactivated]) - end - - defp paginate(query, page, page_size) do - from(u in query, - limit: ^page_size, - offset: ^((page - 1) * page_size) - ) - end end