d50969b2a7a4712e63c619d42dcbb57950cd6006
[akkoma] / lib / pleroma / web / admin_api / views / report_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.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:
42 StatusView.render("index.json", %{
43 activities: statuses,
44 as: :activity,
45 skip_relationships: false
46 }),
47 state: report.data["state"],
48 notes: render(__MODULE__, "index_notes.json", %{notes: report.report_notes})
49 }
50 end
51
52 def render("index_notes.json", %{notes: notes}) when is_list(notes) do
53 Enum.map(notes, &render(__MODULE__, "show_note.json", &1))
54 end
55
56 def render("index_notes.json", _), do: []
57
58 def render("show_note.json", %{
59 id: id,
60 content: content,
61 user_id: user_id,
62 inserted_at: inserted_at
63 }) do
64 user = User.get_by_id(user_id)
65
66 %{
67 id: id,
68 content: content,
69 user: merge_account_views(user),
70 created_at: Utils.to_masto_date(inserted_at)
71 }
72 end
73
74 defp merge_account_views(%User{} = user) do
75 Pleroma.Web.MastodonAPI.AccountView.render("show.json", %{user: user})
76 |> Map.merge(Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}))
77 end
78
79 defp merge_account_views(_), do: %{}
80 end