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 end
def get_all(params) do def get_all(params) do
params base_query =
|> get_all_query() get_all_query()
|> maybe_filter_by_date(params) |> maybe_filter_by_date(params)
|> maybe_filter_by_user(params) |> maybe_filter_by_user(params)
|> maybe_filter_by_search(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 end
defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
@ -61,14 +67,19 @@ defmodule Pleroma.ModerationLog do
) )
end end
defp get_all_query(%{page: page, page_size: page_size}) do defp paginate_query(query, %{page: page, page_size: page_size}) do
from(q in __MODULE__, from(q in query,
order_by: [desc: q.inserted_at],
limit: ^page_size, limit: ^page_size,
offset: ^((page - 1) * page_size) offset: ^((page - 1) * page_size)
) )
end end
defp get_all_query do
from(q in __MODULE__,
order_by: [desc: q.inserted_at]
)
end
defp parse_datetime(datetime) do defp parse_datetime(datetime) do
{:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime) {:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)

View file

@ -8,7 +8,10 @@ defmodule Pleroma.Web.AdminAPI.ModerationLogView do
alias Pleroma.ModerationLog alias Pleroma.ModerationLog
def render("index.json", %{log: log}) do 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 end
def render("show.json", %{log_entry: log_entry}) do def render("show.json", %{log_entry: log_entry}) do