Merge branch 'develop' into feature/report-notes
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index 962f02a054bdc7b82f0fa3396ef45879ce22a353..e87d09134508ad2ad8a0bc9d94b24a5e565dcb8e 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: []
@@ -781,6 +787,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       params
       |> Map.put("type", "Flag")
       |> Map.put("skip_preload", true)
+      |> Map.put("preload_report_notes", true)
       |> Map.put("total", true)
       |> Map.put("limit", page_size)
       |> Map.put("offset", (page - 1) * page_size)
@@ -792,30 +799,34 @@ 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)
+    [%{data: %{"object" => [account_id | _]}} | _] = reports
+
+    account =
+      AccountView.render("show.json", %{
+        user: User.get_by_ap_id(account_id)
+      })
+
+    status = get_status_data(activity)
 
     %{
       date: max_date.data["published"],
-      account: activity["actor"],
+      account: account,
       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 +840,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 +849,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 =