Add fallback value to the admin report view.
authorEugenij <eugenijm@protonmail.com>
Tue, 2 Jul 2019 01:27:00 +0000 (01:27 +0000)
committerkaniini <nenolod@gmail.com>
Tue, 2 Jul 2019 01:27:00 +0000 (01:27 +0000)
This is to avoid 500 error when the reported user no longer exists
in the database.

lib/pleroma/web/admin_api/views/report_view.ex
test/web/admin_api/views/report_view_test.exs

index 73ccdc582ae97b6da8434c9343629925295966cb..a25f3f1fed39c80f3685b1301a7535df9e4e5a79 100644 (file)
@@ -46,8 +46,10 @@ defmodule Pleroma.Web.AdminAPI.ReportView do
     }
   end
 
-  defp merge_account_views(user) do
+  defp merge_account_views(%User{} = user) do
     Pleroma.Web.MastodonAPI.AccountView.render("account.json", %{user: user})
     |> Map.merge(Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}))
   end
+
+  defp merge_account_views(_), do: %{}
 end
index 75d8bb4b5a9daf68b1b711297ee822a4427dcd0f..a00c9c5792d9fc8d3622795f6ebeabc5a0143548 100644 (file)
@@ -111,4 +111,20 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do
     refute "<script> alert('hecked :D:D:D:D:D:D:D') </script>" ==
              ReportView.render("show.json", %{report: activity})[:content]
   end
+
+  test "doesn't error out when the user doesn't exists" do
+    user = insert(:user)
+    other_user = insert(:user)
+
+    {:ok, activity} =
+      CommonAPI.report(user, %{
+        "account_id" => other_user.id,
+        "comment" => ""
+      })
+
+    Pleroma.User.delete(other_user)
+    Pleroma.User.invalidate_cache(other_user)
+
+    assert %{} = ReportView.render("show.json", %{report: activity})
+  end
 end