Merge branch 'develop' into feature/report-notes
[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 notes: render(__MODULE__, "index_notes.json", %{notes: report.report_notes})
44 }
45 end
46
47 def render("index_grouped.json", %{groups: groups}) do
48 reports =
49 Enum.map(groups, fn group ->
50 status =
51 case group.status do
52 %Activity{} = activity -> StatusView.render("show.json", %{activity: activity})
53 _ -> group.status
54 end
55
56 %{
57 date: group[:date],
58 account: group[:account],
59 status: Map.put_new(status, "deleted", false),
60 actors: Enum.map(group[:actors], &merge_account_views/1),
61 reports:
62 group[:reports]
63 |> Enum.map(&Report.extract_report_info(&1))
64 |> Enum.map(&render(__MODULE__, "show.json", &1))
65 }
66 end)
67
68 %{
69 reports: reports
70 }
71 end
72
73 def render("index_notes.json", %{notes: notes}) when is_list(notes) do
74 Enum.map(notes, &render(__MODULE__, "show_note.json", &1))
75 end
76
77 def render("index_notes.json", _), do: []
78
79 def render("show_note.json", %{
80 id: id,
81 content: content,
82 user_id: user_id,
83 inserted_at: inserted_at
84 }) do
85 user = User.get_by_id(user_id)
86
87 %{
88 id: id,
89 content: content,
90 user: merge_account_views(user),
91 created_at: Utils.to_masto_date(inserted_at)
92 }
93 end
94
95 defp merge_account_views(%User{} = user) do
96 Pleroma.Web.MastodonAPI.AccountView.render("show.json", %{user: user})
97 |> Map.merge(Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}))
98 end
99
100 defp merge_account_views(_), do: %{}
101 end