Handle reopened reports with deleted statuses
[akkoma] / lib / pleroma / web / admin_api / views / report_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.ReportView do
6 use Pleroma.Web, :view
7 alias Pleroma.Activity
8 alias Pleroma.HTML
9 alias Pleroma.User
10 alias Pleroma.Web.AdminAPI.Report
11 alias Pleroma.Web.CommonAPI.Utils
12 alias Pleroma.Web.MastodonAPI.StatusView
13
14 def render("index.json", %{reports: reports}) do
15 %{
16 reports:
17 reports[:items]
18 |> Enum.map(&Report.extract_report_info(&1))
19 |> Enum.map(&render(__MODULE__, "show.json", &1))
20 |> Enum.reverse(),
21 total: reports[:total]
22 }
23 end
24
25 def render("show.json", %{report: report, user: user, account: account, statuses: statuses}) do
26 created_at = Utils.to_masto_date(report.data["published"])
27
28 content =
29 unless is_nil(report.data["content"]) do
30 HTML.filter_tags(report.data["content"])
31 else
32 nil
33 end
34
35 %{
36 id: report.id,
37 account: merge_account_views(account),
38 actor: merge_account_views(user),
39 content: content,
40 created_at: created_at,
41 statuses: StatusView.render("index.json", %{activities: statuses, as: :activity}),
42 state: report.data["state"]
43 }
44 end
45
46 def render("index_grouped.json", %{groups: groups}) do
47 reports =
48 Enum.map(groups, fn group ->
49 status =
50 case group.status do
51 %Activity{} = activity -> StatusView.render("show.json", %{activity: activity})
52 _ -> group.status
53 end
54
55 %{
56 date: group[:date],
57 account: group[:account],
58 status: Map.put_new(status, "deleted", false),
59 actors: Enum.map(group[:actors], &merge_account_views/1),
60 reports:
61 group[:reports]
62 |> Enum.map(&Report.extract_report_info(&1))
63 |> Enum.map(&render(__MODULE__, "show.json", &1))
64 }
65 end)
66
67 %{
68 reports: reports
69 }
70 end
71
72 defp merge_account_views(%User{} = user) do
73 Pleroma.Web.MastodonAPI.AccountView.render("show.json", %{user: user})
74 |> Map.merge(Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}))
75 end
76
77 defp merge_account_views(_), do: %{}
78 end