Grouped reports with status data baked in
This commit is contained in:
parent
43ea16870f
commit
f171095960
@ -6,7 +6,6 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
|||||||
alias Ecto.Changeset
|
alias Ecto.Changeset
|
||||||
alias Ecto.UUID
|
alias Ecto.UUID
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
alias Pleroma.Activity.Queries
|
|
||||||
alias Pleroma.Notification
|
alias Pleroma.Notification
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
@ -697,8 +696,8 @@ def get_reports(params, page, page_size) do
|
|||||||
required(:groups) => [
|
required(:groups) => [
|
||||||
%{
|
%{
|
||||||
required(:date) => String.t(),
|
required(:date) => String.t(),
|
||||||
required(:account) => %User{},
|
required(:account) => %{},
|
||||||
required(:status) => %Activity{},
|
required(:status) => %{},
|
||||||
required(:actors) => [%User{}],
|
required(:actors) => [%User{}],
|
||||||
required(:reports) => [%Activity{}]
|
required(:reports) => [%Activity{}]
|
||||||
}
|
}
|
||||||
@ -706,32 +705,23 @@ def get_reports(params, page, page_size) do
|
|||||||
required(:total) => integer
|
required(:total) => integer
|
||||||
}
|
}
|
||||||
def get_reports_grouped_by_status do
|
def get_reports_grouped_by_status do
|
||||||
paginated_activities = get_reported_status_ids()
|
|
||||||
|
|
||||||
groups =
|
groups =
|
||||||
paginated_activities
|
get_reported_status_ids()
|
||||||
|> Enum.map(fn entry ->
|
|> Enum.map(fn entry ->
|
||||||
status =
|
activity = Jason.decode!(entry.activity)
|
||||||
Activity
|
reports = get_reports_by_status_id(activity["id"])
|
||||||
|> Queries.by_ap_id(entry[:activity_id])
|
max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"]))
|
||||||
|> Activity.with_preloaded_object(:left)
|
|
||||||
|> Activity.with_preloaded_user_actor()
|
|
||||||
|> Repo.one()
|
|
||||||
|
|
||||||
reports = get_reports_by_status_id(status.data["id"])
|
|
||||||
|
|
||||||
max_date =
|
|
||||||
Enum.max_by(reports, &Pleroma.Web.CommonAPI.Utils.to_masto_date(&1.data["published"])).data[
|
|
||||||
"published"
|
|
||||||
]
|
|
||||||
|
|
||||||
actors = Enum.map(reports, & &1.user_actor)
|
actors = Enum.map(reports, & &1.user_actor)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
date: max_date,
|
date: max_date.data["published"],
|
||||||
account: status.user_actor,
|
account: activity["actor"],
|
||||||
status: status,
|
status: %{
|
||||||
actors: actors,
|
id: activity["id"],
|
||||||
|
content: activity["content"],
|
||||||
|
published: activity["published"]
|
||||||
|
},
|
||||||
|
actors: Enum.uniq(actors),
|
||||||
reports: reports
|
reports: reports
|
||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
@ -741,28 +731,30 @@ def get_reports_grouped_by_status do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_reports_by_status_id(status_id) do
|
def get_reports_by_status_id(ap_id) do
|
||||||
from(a in Activity,
|
from(a in Activity,
|
||||||
where: fragment("(?)->>'type' = 'Flag'", a.data),
|
where: fragment("(?)->>'type' = 'Flag'", a.data),
|
||||||
where: fragment("(?)->'object' \\? (?)", a.data, ^status_id)
|
where: fragment("(?)->'object' @> ?", a.data, ^[%{id: ap_id}])
|
||||||
)
|
)
|
||||||
|> Activity.with_preloaded_user_actor()
|
|> Activity.with_preloaded_user_actor()
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec get_reported_status_ids() :: %{
|
@spec get_reported_status_ids() :: [
|
||||||
required(:items) => [%Activity{}],
|
%{
|
||||||
required(:total) => integer
|
required(:activity) => String.t(),
|
||||||
|
required(:date) => String.t()
|
||||||
}
|
}
|
||||||
|
]
|
||||||
def get_reported_status_ids do
|
def get_reported_status_ids do
|
||||||
from(a in Activity,
|
from(a in Activity,
|
||||||
where: fragment("(?)->>'type' = 'Flag'", a.data),
|
where: fragment("(?)->>'type' = 'Flag'", a.data),
|
||||||
select: %{
|
select: %{
|
||||||
date: fragment("max(?->>'published') date", a.data),
|
date: fragment("max(?->>'published') date", a.data),
|
||||||
activity_id:
|
activity:
|
||||||
fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity_id", a.data)
|
fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity", a.data)
|
||||||
},
|
},
|
||||||
group_by: fragment("activity_id"),
|
group_by: fragment("activity"),
|
||||||
order_by: fragment("date DESC")
|
order_by: fragment("date DESC")
|
||||||
)
|
)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|
@ -47,8 +47,8 @@ def render("index_grouped.json", %{groups: groups}) do
|
|||||||
Enum.map(groups, fn group ->
|
Enum.map(groups, fn group ->
|
||||||
%{
|
%{
|
||||||
date: group[:date],
|
date: group[:date],
|
||||||
account: merge_account_views(group[:account]),
|
account: group[:account],
|
||||||
status: StatusView.render("show.json", %{activity: group[:status]}),
|
status: group[:status],
|
||||||
actors: Enum.map(group[:actors], &merge_account_views/1),
|
actors: Enum.map(group[:actors], &merge_account_views/1),
|
||||||
reports:
|
reports:
|
||||||
group[:reports]
|
group[:reports]
|
||||||
|
@ -1551,7 +1551,7 @@ test "returns 403 when requested by anonymous" do
|
|||||||
|
|
||||||
describe "GET /api/pleroma/admin/grouped_reports" do
|
describe "GET /api/pleroma/admin/grouped_reports" do
|
||||||
setup %{conn: conn} do
|
setup %{conn: conn} do
|
||||||
admin = insert(:user, info: %{is_admin: true})
|
admin = insert(:user, is_admin: true)
|
||||||
[reporter, target_user] = insert_pair(:user)
|
[reporter, target_user] = insert_pair(:user)
|
||||||
|
|
||||||
date1 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
|
date1 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
|
||||||
@ -1567,53 +1567,124 @@ test "returns 403 when requested by anonymous" do
|
|||||||
third_status =
|
third_status =
|
||||||
insert(:note_activity, user: target_user, data_attrs: %{"published" => date3})
|
insert(:note_activity, user: target_user, data_attrs: %{"published" => date3})
|
||||||
|
|
||||||
%{
|
{:ok, first_report} =
|
||||||
conn: assign(conn, :user, admin),
|
|
||||||
reporter: reporter,
|
|
||||||
target_user: target_user,
|
|
||||||
first_status: first_status,
|
|
||||||
second_status: second_status,
|
|
||||||
third_status: third_status
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
test "returns reports grouped by status", %{
|
|
||||||
conn: conn,
|
|
||||||
reporter: reporter,
|
|
||||||
target_user: target_user,
|
|
||||||
first_status: first_status,
|
|
||||||
second_status: second_status,
|
|
||||||
third_status: third_status
|
|
||||||
} do
|
|
||||||
{:ok, %{id: _}} =
|
|
||||||
CommonAPI.report(reporter, %{
|
CommonAPI.report(reporter, %{
|
||||||
"account_id" => target_user.id,
|
"account_id" => target_user.id,
|
||||||
"status_ids" => [first_status.id, second_status.id, third_status.id]
|
"status_ids" => [first_status.id, second_status.id, third_status.id]
|
||||||
})
|
})
|
||||||
|
|
||||||
{:ok, %{id: _}} =
|
{:ok, second_report} =
|
||||||
CommonAPI.report(reporter, %{
|
CommonAPI.report(reporter, %{
|
||||||
"account_id" => target_user.id,
|
"account_id" => target_user.id,
|
||||||
"status_ids" => [first_status.id, second_status.id]
|
"status_ids" => [first_status.id, second_status.id]
|
||||||
})
|
})
|
||||||
|
|
||||||
{:ok, %{id: _}} =
|
{:ok, third_report} =
|
||||||
CommonAPI.report(reporter, %{
|
CommonAPI.report(reporter, %{
|
||||||
"account_id" => target_user.id,
|
"account_id" => target_user.id,
|
||||||
"status_ids" => [first_status.id]
|
"status_ids" => [first_status.id]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
%{
|
||||||
|
conn: assign(conn, :user, admin),
|
||||||
|
first_status: Activity.get_by_ap_id_with_object(first_status.data["id"]),
|
||||||
|
second_status: Activity.get_by_ap_id_with_object(second_status.data["id"]),
|
||||||
|
third_status: Activity.get_by_ap_id_with_object(third_status.data["id"]),
|
||||||
|
first_status_reports: [first_report, second_report, third_report],
|
||||||
|
second_status_reports: [first_report, second_report],
|
||||||
|
third_status_reports: [first_report],
|
||||||
|
target_user: target_user,
|
||||||
|
reporter: reporter
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns reports grouped by status", %{
|
||||||
|
conn: conn,
|
||||||
|
first_status: first_status,
|
||||||
|
second_status: second_status,
|
||||||
|
third_status: third_status,
|
||||||
|
first_status_reports: first_status_reports,
|
||||||
|
second_status_reports: second_status_reports,
|
||||||
|
third_status_reports: third_status_reports,
|
||||||
|
target_user: target_user,
|
||||||
|
reporter: reporter
|
||||||
|
} do
|
||||||
response =
|
response =
|
||||||
conn
|
conn
|
||||||
|> get("/api/pleroma/admin/grouped_reports")
|
|> get("/api/pleroma/admin/grouped_reports")
|
||||||
|> json_response(:ok)
|
|> json_response(:ok)
|
||||||
|
|
||||||
assert length(response["reports"]) == 3
|
assert length(response["reports"]) == 3
|
||||||
[third_group, second_group, first_group] = response["reports"]
|
|
||||||
|
|
||||||
assert length(third_group["reports"]) == 3
|
first_group =
|
||||||
|
Enum.find(response["reports"], &(&1["status"]["id"] == first_status.data["id"]))
|
||||||
|
|
||||||
|
second_group =
|
||||||
|
Enum.find(response["reports"], &(&1["status"]["id"] == second_status.data["id"]))
|
||||||
|
|
||||||
|
third_group =
|
||||||
|
Enum.find(response["reports"], &(&1["status"]["id"] == third_status.data["id"]))
|
||||||
|
|
||||||
|
assert length(first_group["reports"]) == 3
|
||||||
assert length(second_group["reports"]) == 2
|
assert length(second_group["reports"]) == 2
|
||||||
assert length(first_group["reports"]) == 1
|
assert length(third_group["reports"]) == 1
|
||||||
|
|
||||||
|
assert first_group["date"] ==
|
||||||
|
Enum.max_by(first_status_reports, fn act ->
|
||||||
|
NaiveDateTime.from_iso8601!(act.data["published"])
|
||||||
|
end).data["published"]
|
||||||
|
|
||||||
|
assert first_group["status"] == %{
|
||||||
|
"id" => first_status.data["id"],
|
||||||
|
"content" => first_status.object.data["content"],
|
||||||
|
"published" => first_status.object.data["published"]
|
||||||
|
}
|
||||||
|
|
||||||
|
assert first_group["account"]["id"] == target_user.id
|
||||||
|
|
||||||
|
assert length(first_group["actors"]) == 1
|
||||||
|
assert hd(first_group["actors"])["id"] == reporter.id
|
||||||
|
|
||||||
|
assert Enum.map(first_group["reports"], & &1["id"]) --
|
||||||
|
Enum.map(first_status_reports, & &1.id) == []
|
||||||
|
|
||||||
|
assert second_group["date"] ==
|
||||||
|
Enum.max_by(second_status_reports, fn act ->
|
||||||
|
NaiveDateTime.from_iso8601!(act.data["published"])
|
||||||
|
end).data["published"]
|
||||||
|
|
||||||
|
assert second_group["status"] == %{
|
||||||
|
"id" => second_status.data["id"],
|
||||||
|
"content" => second_status.object.data["content"],
|
||||||
|
"published" => second_status.object.data["published"]
|
||||||
|
}
|
||||||
|
|
||||||
|
assert second_group["account"]["id"] == target_user.id
|
||||||
|
|
||||||
|
assert length(second_group["actors"]) == 1
|
||||||
|
assert hd(second_group["actors"])["id"] == reporter.id
|
||||||
|
|
||||||
|
assert Enum.map(second_group["reports"], & &1["id"]) --
|
||||||
|
Enum.map(second_status_reports, & &1.id) == []
|
||||||
|
|
||||||
|
assert third_group["date"] ==
|
||||||
|
Enum.max_by(third_status_reports, fn act ->
|
||||||
|
NaiveDateTime.from_iso8601!(act.data["published"])
|
||||||
|
end).data["published"]
|
||||||
|
|
||||||
|
assert third_group["status"] == %{
|
||||||
|
"id" => third_status.data["id"],
|
||||||
|
"content" => third_status.object.data["content"],
|
||||||
|
"published" => third_status.object.data["published"]
|
||||||
|
}
|
||||||
|
|
||||||
|
assert third_group["account"]["id"] == target_user.id
|
||||||
|
|
||||||
|
assert length(third_group["actors"]) == 1
|
||||||
|
assert hd(third_group["actors"])["id"] == reporter.id
|
||||||
|
|
||||||
|
assert Enum.map(third_group["reports"], & &1["id"]) --
|
||||||
|
Enum.map(third_status_reports, & &1.id) == []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user