294861044f1942cb8d722c0d44b7edea062b977f
[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.HTML
8 alias Pleroma.User
9 alias Pleroma.Web.AdminAPI.Report
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.StatusView
12
13 def render("index.json", %{reports: reports}) do
14 %{
15 reports:
16 reports[:items]
17 |> Enum.map(&Report.extract_report_info(&1))
18 |> Enum.map(&render(__MODULE__, "show.json", &1))
19 |> Enum.reverse(),
20 total: reports[:total]
21 }
22 end
23
24 def render("show.json", %{report: report, user: user, account: account, statuses: statuses}) do
25 created_at = Utils.to_masto_date(report.data["published"])
26
27 content =
28 unless is_nil(report.data["content"]) do
29 HTML.filter_tags(report.data["content"])
30 else
31 nil
32 end
33
34 %{
35 id: report.id,
36 account: merge_account_views(account),
37 actor: merge_account_views(user),
38 content: content,
39 created_at: created_at,
40 statuses: StatusView.render("index.json", %{activities: statuses, as: :activity}),
41 state: report.data["state"],
42 notes: render(__MODULE__, "index_notes.json", %{notes: report.report_notes})
43 }
44 end
45
46 def render("index_grouped.json", %{groups: groups}) do
47 reports =
48 Enum.map(groups, fn group ->
49 %{
50 date: group[:date],
51 account: group[:account],
52 status: group[:status],
53 actors: Enum.map(group[:actors], &merge_account_views/1),
54 reports:
55 group[:reports]
56 |> Enum.map(&Report.extract_report_info(&1))
57 |> Enum.map(&render(__MODULE__, "show.json", &1))
58 }
59 end)
60
61 %{
62 reports: reports
63 }
64 end
65
66 def render("index_notes.json", %{notes: notes}) when is_list(notes) do
67 Enum.map(notes, &render(__MODULE__, "show_note.json", &1))
68 end
69
70 def render("index_notes.json", _), do: []
71
72 def render("show_note.json", %{
73 id: id,
74 content: content,
75 user_id: user_id,
76 inserted_at: inserted_at
77 }) do
78 user = User.get_by_id(user_id)
79
80 %{
81 id: id,
82 content: content,
83 user: merge_account_views(user),
84 created_at: Utils.to_masto_date(inserted_at)
85 }
86 end
87
88 defp merge_account_views(%User{} = user) do
89 Pleroma.Web.MastodonAPI.AccountView.render("show.json", %{user: user})
90 |> Map.merge(Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}))
91 end
92
93 defp merge_account_views(_), do: %{}
94 end