Handle reopened reports with deleted statuses
authorMaxim Filippov <colixer@gmail.com>
Wed, 27 Nov 2019 13:54:12 +0000 (22:54 +0900)
committerMaxim Filippov <colixer@gmail.com>
Wed, 27 Nov 2019 13:54:12 +0000 (22:54 +0900)
lib/pleroma/web/activity_pub/utils.ex
lib/pleroma/web/admin_api/views/report_view.ex
test/web/admin_api/admin_api_controller_test.exs

index 962f02a054bdc7b82f0fa3396ef45879ce22a353..d91abf7b3283b38cd791a1d26c48ed271328b87b 100644 (file)
@@ -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 =
index 45ce75272c2d8d7f8171889324c47affd040b5d6..13602efd9f4d9612b6f6787f3226e8f77c63a5a1 100644 (file)
@@ -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]
index c6ff1a0653895ccfa62e7e90a84ccf82f9da01a3..a69fadcdc85bdcef75e9d0545704c53b3a8fa55a 100644 (file)
@@ -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