Handle reopened reports with deleted statuses

This commit is contained in:
Maxim Filippov 2019-11-27 22:54:12 +09:00
parent 5869a43fe7
commit 5135656f57
3 changed files with 93 additions and 32 deletions

View File

@ -722,16 +722,22 @@ defp build_flag_object(act) when is_map(act) or is_binary(act) do
act when is_binary(act) -> act act when is_binary(act) -> act
end end
activity = Activity.get_by_ap_id_with_object(id) case Activity.get_by_ap_id_with_object(id) do
actor = User.get_by_ap_id(activity.object.data["actor"]) %Activity{} = activity ->
%{
"type" => "Note",
"id" => activity.data["id"],
"content" => activity.object.data["content"],
"published" => activity.object.data["published"],
"actor" =>
AccountView.render("show.json", %{
user: User.get_by_ap_id(activity.object.data["actor"])
})
}
%{ _ ->
"type" => "Note", %{"id" => id, "deleted" => true}
"id" => activity.data["id"], end
"content" => activity.object.data["content"],
"published" => activity.object.data["published"],
"actor" => AccountView.render("show.json", %{user: actor})
}
end end
defp build_flag_object(_), do: [] defp build_flag_object(_), do: []
@ -792,30 +798,27 @@ def parse_report_group(activity) do
reports = get_reports_by_status_id(activity["id"]) reports = get_reports_by_status_id(activity["id"])
max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"])) max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"]))
actors = Enum.map(reports, & &1.user_actor) actors = Enum.map(reports, & &1.user_actor)
{deleted, status} = get_status_data(activity) status = get_status_data(activity)
%{ %{
date: max_date.data["published"], date: max_date.data["published"],
account: activity["actor"], account: activity["actor"],
status: status, status: status,
status_deleted: deleted,
actors: Enum.uniq(actors), actors: Enum.uniq(actors),
reports: reports reports: reports
} }
end end
defp get_status_data(activity) do defp get_status_data(status) do
case Activity.get_by_ap_id(activity["id"]) do case status["deleted"] do
%Activity{} = act -> true ->
{false, act} %{
"id" => status["id"],
"deleted" => true
}
_ -> _ ->
{true, Activity.get_by_ap_id(status["id"])
%{
id: activity["id"],
content: activity["content"],
published: activity["published"]
}}
end end
end end
@ -829,7 +832,7 @@ def get_reports_by_status_id(ap_id) do
|> Repo.all() |> Repo.all()
end end
@spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{ @spec get_reports_grouped_by_status([String.t()]) :: %{
required(:groups) => [ required(:groups) => [
%{ %{
required(:date) => String.t(), required(:date) => String.t(),
@ -838,8 +841,7 @@ def get_reports_by_status_id(ap_id) do
required(:actors) => [%User{}], required(:actors) => [%User{}],
required(:reports) => [%Activity{}] required(:reports) => [%Activity{}]
} }
], ]
required(:total) => integer
} }
def get_reports_grouped_by_status(activity_ids) do def get_reports_grouped_by_status(activity_ids) do
parsed_groups = parsed_groups =

View File

@ -4,6 +4,7 @@
defmodule Pleroma.Web.AdminAPI.ReportView do defmodule Pleroma.Web.AdminAPI.ReportView do
use Pleroma.Web, :view use Pleroma.Web, :view
alias Pleroma.Activity
alias Pleroma.HTML alias Pleroma.HTML
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.AdminAPI.Report alias Pleroma.Web.AdminAPI.Report
@ -46,15 +47,15 @@ def render("index_grouped.json", %{groups: groups}) do
reports = reports =
Enum.map(groups, fn group -> Enum.map(groups, fn group ->
status = status =
if group[:status_deleted], case group.status do
do: group[:status], %Activity{} = activity -> StatusView.render("show.json", %{activity: activity})
else: StatusView.render("show.json", %{activity: group[:status]}) _ -> group.status
end
%{ %{
date: group[:date], date: group[:date],
account: group[:account], account: group[:account],
status: status, status: Map.put_new(status, "deleted", false),
status_deleted: group[:status_deleted],
actors: Enum.map(group[:actors], &merge_account_views/1), actors: Enum.map(group[:actors], &merge_account_views/1),
reports: reports:
group[:reports] group[:reports]

View File

@ -1613,6 +1613,7 @@ test "returns 403 when requested by anonymous" do
first_status: Activity.get_by_ap_id_with_object(first_status.data["id"]), 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"]), 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"]), third_status: Activity.get_by_ap_id_with_object(third_status.data["id"]),
first_report: first_report,
first_status_reports: [first_report, second_report, third_report], first_status_reports: [first_report, second_report, third_report],
second_status_reports: [first_report, second_report], second_status_reports: [first_report, second_report],
third_status_reports: [first_report], third_status_reports: [first_report],
@ -1655,7 +1656,11 @@ test "returns reports grouped by status", %{
end).data["published"] end).data["published"]
assert first_group["status"] == assert first_group["status"] ==
stringify_keys(StatusView.render("show.json", %{activity: first_status})) Map.put(
stringify_keys(StatusView.render("show.json", %{activity: first_status})),
"deleted",
false
)
assert(first_group["account"]["id"] == target_user.id) assert(first_group["account"]["id"] == target_user.id)
@ -1671,7 +1676,11 @@ test "returns reports grouped by status", %{
end).data["published"] end).data["published"]
assert second_group["status"] == assert second_group["status"] ==
stringify_keys(StatusView.render("show.json", %{activity: second_status})) Map.put(
stringify_keys(StatusView.render("show.json", %{activity: second_status})),
"deleted",
false
)
assert second_group["account"]["id"] == target_user.id assert second_group["account"]["id"] == target_user.id
@ -1687,7 +1696,11 @@ test "returns reports grouped by status", %{
end).data["published"] end).data["published"]
assert third_group["status"] == assert third_group["status"] ==
stringify_keys(StatusView.render("show.json", %{activity: third_status})) Map.put(
stringify_keys(StatusView.render("show.json", %{activity: third_status})),
"deleted",
false
)
assert third_group["account"]["id"] == target_user.id assert third_group["account"]["id"] == target_user.id
@ -1697,6 +1710,51 @@ test "returns reports grouped by status", %{
assert Enum.map(third_group["reports"], & &1["id"]) -- assert Enum.map(third_group["reports"], & &1["id"]) --
Enum.map(third_status_reports, & &1.id) == [] Enum.map(third_status_reports, & &1.id) == []
end end
test "reopened report renders status data", %{
conn: conn,
first_report: first_report,
first_status: first_status
} do
{:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
response =
conn
|> get("/api/pleroma/admin/grouped_reports")
|> json_response(:ok)
first_group = Enum.find(response["reports"], &(&1["status"]["id"] == first_status.id))
assert first_group["status"] ==
Map.put(
stringify_keys(StatusView.render("show.json", %{activity: first_status})),
"deleted",
false
)
end
test "reopened report does not render status data if status has been deleted", %{
conn: conn,
first_report: first_report,
first_status: first_status,
target_user: target_user
} do
{:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
{:ok, _} = CommonAPI.delete(first_status.id, target_user)
refute Activity.get_by_ap_id(first_status.id)
response =
conn
|> get("/api/pleroma/admin/grouped_reports")
|> json_response(:ok)
assert Enum.find(response["reports"], &(&1["status"]["deleted"] == true))["status"][
"deleted"
] == true
assert length(Enum.filter(response["reports"], &(&1["status"]["deleted"] == false))) == 2
end
end end
describe "POST /api/pleroma/admin/reports/:id/respond" do describe "POST /api/pleroma/admin/reports/:id/respond" do