From ab50c6b3ca26c87e4b7fdbe04da5d0d236d2e9ce Mon Sep 17 00:00:00 2001 From: Eugenij Date: Tue, 2 Jul 2019 01:27:00 +0000 Subject: [PATCH] Add fallback value to the admin report view. 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 | 4 +++- test/web/admin_api/views/report_view_test.exs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/admin_api/views/report_view.ex b/lib/pleroma/web/admin_api/views/report_view.ex index 73ccdc582..a25f3f1fe 100644 --- a/lib/pleroma/web/admin_api/views/report_view.ex +++ b/lib/pleroma/web/admin_api/views/report_view.ex @@ -46,8 +46,10 @@ def render("show.json", %{report: report}) 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 diff --git a/test/web/admin_api/views/report_view_test.exs b/test/web/admin_api/views/report_view_test.exs index 75d8bb4b5..a00c9c579 100644 --- a/test/web/admin_api/views/report_view_test.exs +++ b/test/web/admin_api/views/report_view_test.exs @@ -111,4 +111,20 @@ test "sanitizes report description" do refute "" == 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