Filter logs by date
authorMaxim Filippov <colixer@gmail.com>
Tue, 27 Aug 2019 17:48:16 +0000 (20:48 +0300)
committerMaxim Filippov <colixer@gmail.com>
Tue, 27 Aug 2019 17:48:16 +0000 (20:48 +0300)
lib/pleroma/moderation_log.ex
lib/pleroma/user/info.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
test/web/admin_api/admin_api_controller_test.exs

index 1ef6fe67afe85f7a1b5ea008cfe6dff17ef38c0e..2164ecfc2681a2f449798d963a7da4a0fbb19451 100644 (file)
@@ -14,13 +14,46 @@ defmodule Pleroma.ModerationLog do
     timestamps()
   end
 
-  def get_all(page, page_size) do
+  def get_all(params) do
+    params
+    |> get_all_query()
+    |> maybe_filter_by_date(params)
+    |> Repo.all()
+  end
+
+  defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
+
+  defp maybe_filter_by_date(query, %{start_date: start_date, end_date: nil}) do
+    from(q in query,
+      where: q.inserted_at >= ^parse_datetime(start_date)
+    )
+  end
+
+  defp maybe_filter_by_date(query, %{start_date: nil, end_date: end_date}) do
+    from(q in query,
+      where: q.inserted_at <= ^parse_datetime(end_date)
+    )
+  end
+
+  defp maybe_filter_by_date(query, %{start_date: start_date, end_date: end_date}) do
+    from(q in query,
+      where: q.inserted_at >= ^parse_datetime(start_date),
+      where: q.inserted_at <= ^parse_datetime(end_date)
+    )
+  end
+
+  defp get_all_query(%{page: page, page_size: page_size}) do
     from(q in __MODULE__,
       order_by: [desc: q.inserted_at],
       limit: ^page_size,
       offset: ^((page - 1) * page_size)
     )
-    |> Repo.all()
+  end
+
+  defp parse_datetime(datetime) do
+    {:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)
+
+    parsed_datetime
   end
 
   def insert_log(%{
index 779bfbc188a911c324d4ad006a616565c3ba2ad1..7027c947b0e63d5948c5d7d474f459afe8f6444a 100644 (file)
@@ -318,9 +318,7 @@ defmodule Pleroma.User.Info do
     name_limit = Pleroma.Config.get([:instance, :account_field_name_length], 255)
     value_limit = Pleroma.Config.get([:instance, :account_field_value_length], 255)
 
-    is_binary(name) &&
-      is_binary(value) &&
-      String.length(name) <= name_limit &&
+    is_binary(name) && is_binary(value) && String.length(name) <= name_limit &&
       String.length(value) <= value_limit
   end
 
index 544b9d7d8b530018120c3f1a9f15af36a223e43a..065394a24b331615c532f1180e7def6184d399b0 100644 (file)
@@ -539,7 +539,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   def list_log(conn, params) do
     {page, page_size} = page_params(params)
 
-    log = ModerationLog.get_all(page, page_size)
+    log =
+      ModerationLog.get_all(%{
+        page: page,
+        page_size: page_size,
+        start_date: params["start_date"],
+        end_date: params["end_date"]
+      })
 
     conn
     |> put_view(ModerationLogView)
index 4e2c274315d678782e4112d6d8d8c00282b72333..a7269aee9a35acf251e71e38e94577fab6ce0446 100644 (file)
@@ -2348,6 +2348,52 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       assert second_entry["message"] ==
                "@#{admin.nickname} followed relay: https://example.org/relay"
     end
+
+    test "filters log by date", %{conn: conn, admin: admin} do
+      first_date = "2017-08-15T15:47:06Z"
+      second_date = "2017-08-20T15:47:06Z"
+
+      Repo.insert(%ModerationLog{
+        data: %{
+          actor: %{
+            "id" => admin.id,
+            "nickname" => admin.nickname,
+            "type" => "user"
+          },
+          action: "relay_follow",
+          target: "https://example.org/relay"
+        },
+        inserted_at: NaiveDateTime.from_iso8601!(first_date)
+      })
+
+      Repo.insert(%ModerationLog{
+        data: %{
+          actor: %{
+            "id" => admin.id,
+            "nickname" => admin.nickname,
+            "type" => "user"
+          },
+          action: "relay_unfollow",
+          target: "https://example.org/relay"
+        },
+        inserted_at: NaiveDateTime.from_iso8601!(second_date)
+      })
+
+      conn1 =
+        get(
+          conn,
+          "/api/pleroma/admin/moderation_log?start_date=#{second_date}"
+        )
+
+      response1 = json_response(conn1, 200)
+      [first_entry] = response1
+
+      assert response1 |> length() == 1
+      assert first_entry["data"]["action"] == "relay_unfollow"
+
+      assert first_entry["message"] ==
+               "@#{admin.nickname} unfollowed relay: https://example.org/relay"
+    end
   end
 end