Restricted embedding of relationships where applicable (statuses / notifications...
[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
11 alias Pleroma.Web.AdminAPI.Report
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Web.MastodonAPI
14 alias Pleroma.Web.MastodonAPI.StatusView
15
16 def render("index.json", %{reports: reports}) do
17 %{
18 reports:
19 reports[:items]
20 |> Enum.map(&Report.extract_report_info(&1))
21 |> Enum.map(&render(__MODULE__, "show.json", &1))
22 |> Enum.reverse(),
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", &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
75 defp merge_account_views(%User{} = user) do
76 MastodonAPI.AccountView.render("show.json", %{user: user, skip_relationships: true})
77 |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
78 end
79
80 defp merge_account_views(_), do: %{}
81 end