Filter logs by date

This commit is contained in:
Maxim Filippov 2019-08-27 20:48:16 +03:00
parent ba5e14be05
commit 5e4fde1d3d
4 changed files with 89 additions and 6 deletions

View File

@ -14,13 +14,46 @@ defmodule Pleroma.ModerationLog do
timestamps() timestamps()
end 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__, from(q in __MODULE__,
order_by: [desc: q.inserted_at], order_by: [desc: q.inserted_at],
limit: ^page_size, limit: ^page_size,
offset: ^((page - 1) * 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 end
def insert_log(%{ def insert_log(%{

View File

@ -318,9 +318,7 @@ defp valid_field?(%{"name" => name, "value" => value}) do
name_limit = Pleroma.Config.get([:instance, :account_field_name_length], 255) name_limit = Pleroma.Config.get([:instance, :account_field_name_length], 255)
value_limit = Pleroma.Config.get([:instance, :account_field_value_length], 255) value_limit = Pleroma.Config.get([:instance, :account_field_value_length], 255)
is_binary(name) && is_binary(name) && is_binary(value) && String.length(name) <= name_limit &&
is_binary(value) &&
String.length(name) <= name_limit &&
String.length(value) <= value_limit String.length(value) <= value_limit
end end

View File

@ -539,7 +539,13 @@ def status_delete(%{assigns: %{user: user}} = conn, %{"id" => id}) do
def list_log(conn, params) do def list_log(conn, params) do
{page, page_size} = page_params(params) {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 conn
|> put_view(ModerationLogView) |> put_view(ModerationLogView)

View File

@ -2348,6 +2348,52 @@ test "returns the log with pagination", %{conn: conn, admin: admin} do
assert second_entry["message"] == assert second_entry["message"] ==
"@#{admin.nickname} followed relay: https://example.org/relay" "@#{admin.nickname} followed relay: https://example.org/relay"
end 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
end end