From 5135656f579954cf786011b539934c7150e0d0bc Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Wed, 27 Nov 2019 22:54:12 +0900 Subject: [PATCH] Handle reopened reports with deleted statuses --- lib/pleroma/web/activity_pub/utils.ex | 50 ++++++++------- .../web/admin_api/views/report_view.ex | 11 ++-- .../admin_api/admin_api_controller_test.exs | 64 ++++++++++++++++++- 3 files changed, 93 insertions(+), 32 deletions(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 962f02a05..d91abf7b3 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -722,16 +722,22 @@ defmodule Pleroma.Web.ActivityPub.Utils do act when is_binary(act) -> act end - activity = Activity.get_by_ap_id_with_object(id) - actor = User.get_by_ap_id(activity.object.data["actor"]) + case Activity.get_by_ap_id_with_object(id) do + %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" => activity.data["id"], - "content" => activity.object.data["content"], - "published" => activity.object.data["published"], - "actor" => AccountView.render("show.json", %{user: actor}) - } + _ -> + %{"id" => id, "deleted" => true} + end end defp build_flag_object(_), do: [] @@ -792,30 +798,27 @@ defmodule Pleroma.Web.ActivityPub.Utils do reports = get_reports_by_status_id(activity["id"]) max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"])) actors = Enum.map(reports, & &1.user_actor) - {deleted, status} = get_status_data(activity) + status = get_status_data(activity) %{ date: max_date.data["published"], account: activity["actor"], status: status, - status_deleted: deleted, actors: Enum.uniq(actors), reports: reports } end - defp get_status_data(activity) do - case Activity.get_by_ap_id(activity["id"]) do - %Activity{} = act -> - {false, act} + defp get_status_data(status) do + case status["deleted"] do + true -> + %{ + "id" => status["id"], + "deleted" => true + } _ -> - {true, - %{ - id: activity["id"], - content: activity["content"], - published: activity["published"] - }} + Activity.get_by_ap_id(status["id"]) end end @@ -829,7 +832,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do |> Repo.all() end - @spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{ + @spec get_reports_grouped_by_status([String.t()]) :: %{ required(:groups) => [ %{ required(:date) => String.t(), @@ -838,8 +841,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do required(:actors) => [%User{}], required(:reports) => [%Activity{}] } - ], - required(:total) => integer + ] } def get_reports_grouped_by_status(activity_ids) do parsed_groups = diff --git a/lib/pleroma/web/admin_api/views/report_view.ex b/lib/pleroma/web/admin_api/views/report_view.ex index 45ce75272..13602efd9 100644 --- a/lib/pleroma/web/admin_api/views/report_view.ex +++ b/lib/pleroma/web/admin_api/views/report_view.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.AdminAPI.ReportView do use Pleroma.Web, :view + alias Pleroma.Activity alias Pleroma.HTML alias Pleroma.User alias Pleroma.Web.AdminAPI.Report @@ -46,15 +47,15 @@ defmodule Pleroma.Web.AdminAPI.ReportView do reports = Enum.map(groups, fn group -> status = - if group[:status_deleted], - do: group[:status], - else: StatusView.render("show.json", %{activity: group[:status]}) + case group.status do + %Activity{} = activity -> StatusView.render("show.json", %{activity: activity}) + _ -> group.status + end %{ date: group[:date], account: group[:account], - status: status, - status_deleted: group[:status_deleted], + status: Map.put_new(status, "deleted", false), actors: Enum.map(group[:actors], &merge_account_views/1), reports: group[:reports] diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index c6ff1a065..a69fadcdc 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1613,6 +1613,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do 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_report: first_report, first_status_reports: [first_report, second_report, third_report], second_status_reports: [first_report, second_report], third_status_reports: [first_report], @@ -1655,7 +1656,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end).data["published"] 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) @@ -1671,7 +1676,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end).data["published"] 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 @@ -1687,7 +1696,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end).data["published"] 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 @@ -1697,6 +1710,51 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do assert Enum.map(third_group["reports"], & &1["id"]) -- Enum.map(third_status_reports, & &1.id) == [] 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 describe "POST /api/pleroma/admin/reports/:id/respond" do