First
[anni] / lib / pleroma / web / admin_api / views / report_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
8   alias Pleroma.HTML
9   alias Pleroma.User
10   alias Pleroma.Web.AdminAPI
11   alias Pleroma.Web.AdminAPI.Report
12   alias Pleroma.Web.CommonAPI.Utils
13   alias Pleroma.Web.MastodonAPI.StatusView
14
15   defdelegate merge_account_views(user), to: AdminAPI.AccountView
16
17   def render("index.json", %{reports: reports}) do
18     %{
19       reports:
20         reports[:items]
21         |> Enum.map(&Report.extract_report_info/1)
22         |> Enum.map(&render(__MODULE__, "show.json", &1)),
23       total: reports[:total]
24     }
25   end
26
27   def render("show.json", %{report: report, user: user, account: account, statuses: statuses}) do
28     created_at = Utils.to_masto_date(report.data["published"])
29
30     content =
31       unless is_nil(report.data["content"]) do
32         HTML.filter_tags(report.data["content"])
33       else
34         nil
35       end
36
37     %{
38       id: report.id,
39       account: merge_account_views(account),
40       actor: merge_account_views(user),
41       content: content,
42       created_at: created_at,
43       statuses:
44         StatusView.render("index.json", %{
45           activities: statuses,
46           as: :activity
47         }),
48       state: report.data["state"],
49       notes: render(__MODULE__, "index_notes.json", %{notes: report.report_notes})
50     }
51   end
52
53   def render("index_notes.json", %{notes: notes}) when is_list(notes) do
54     Enum.map(notes, &render(__MODULE__, "show_note.json", Map.from_struct(&1)))
55   end
56
57   def render("index_notes.json", _), do: []
58
59   def render("show_note.json", %{
60         id: id,
61         content: content,
62         user_id: user_id,
63         inserted_at: inserted_at
64       }) do
65     user = User.get_by_id(user_id)
66
67     %{
68       id: id,
69       content: content,
70       user: merge_account_views(user),
71       created_at: Utils.to_masto_date(inserted_at)
72     }
73   end
74 end