Add pagination to logs

This commit is contained in:
Maxim Filippov 2019-09-01 21:23:30 +03:00
parent 4d6e22bb9b
commit 9c96b17e16
2 changed files with 24 additions and 10 deletions

View File

@ -15,12 +15,18 @@ defmodule Pleroma.ModerationLog do
end
def get_all(params) do
params
|> get_all_query()
base_query =
get_all_query()
|> maybe_filter_by_date(params)
|> maybe_filter_by_user(params)
|> maybe_filter_by_search(params)
|> Repo.all()
query_with_pagination = base_query |> paginate_query(params)
%{
items: Repo.all(query_with_pagination),
count: Repo.aggregate(base_query, :count, :id)
}
end
defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
@ -61,14 +67,19 @@ defp maybe_filter_by_search(query, %{search: search}) do
)
end
defp get_all_query(%{page: page, page_size: page_size}) do
from(q in __MODULE__,
order_by: [desc: q.inserted_at],
defp paginate_query(query, %{page: page, page_size: page_size}) do
from(q in query,
limit: ^page_size,
offset: ^((page - 1) * page_size)
)
end
defp get_all_query do
from(q in __MODULE__,
order_by: [desc: q.inserted_at]
)
end
defp parse_datetime(datetime) do
{:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)

View File

@ -8,7 +8,10 @@ defmodule Pleroma.Web.AdminAPI.ModerationLogView do
alias Pleroma.ModerationLog
def render("index.json", %{log: log}) do
render_many(log, __MODULE__, "show.json", as: :log_entry)
%{
items: render_many(log.items, __MODULE__, "show.json", as: :log_entry),
total: log.count
}
end
def render("show.json", %{log_entry: log_entry}) do