Add pagination to logs
authorMaxim Filippov <colixer@gmail.com>
Sun, 1 Sep 2019 18:23:30 +0000 (21:23 +0300)
committerMaxim Filippov <colixer@gmail.com>
Sun, 1 Sep 2019 18:23:30 +0000 (21:23 +0300)
lib/pleroma/moderation_log.ex
lib/pleroma/web/admin_api/views/moderation_log_view.ex

index 89a5e13c373a54e1b77cf86e2178d720ce393a5b..352cad4335a40f9c1beea6ac5c2b258191527b72 100644 (file)
@@ -15,12 +15,18 @@ defmodule Pleroma.ModerationLog do
   end
 
   def get_all(params) do
-    params
-    |> get_all_query()
-    |> maybe_filter_by_date(params)
-    |> maybe_filter_by_user(params)
-    |> maybe_filter_by_search(params)
-    |> Repo.all()
+    base_query =
+      get_all_query()
+      |> maybe_filter_by_date(params)
+      |> maybe_filter_by_user(params)
+      |> maybe_filter_by_search(params)
+
+    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 @@ defmodule Pleroma.ModerationLog 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)
 
index b3fc7cfe57106c9d01fadfd45a42e061bab72bfe..e7752d1f3994e09075aeca18bb297d1969bf4170 100644 (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