Merge branch 'fix/remove_auto_nsfw' into 'develop'
[akkoma] / lib / pleroma / web / admin_api / controllers / report_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.ReportController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
9
10 alias Pleroma.Activity
11 alias Pleroma.ModerationLog
12 alias Pleroma.ReportNote
13 alias Pleroma.Web.ActivityPub.Utils
14 alias Pleroma.Web.AdminAPI
15 alias Pleroma.Web.AdminAPI.Report
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.Plugs.OAuthScopesPlug
18
19 require Logger
20
21 plug(Pleroma.Web.ApiSpec.CastAndValidate)
22 plug(OAuthScopesPlug, %{scopes: ["admin:read:reports"]} when action in [:index, :show])
23
24 plug(
25 OAuthScopesPlug,
26 %{scopes: ["admin:write:reports"]}
27 when action in [:update, :notes_create, :notes_delete]
28 )
29
30 action_fallback(AdminAPI.FallbackController)
31
32 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ReportOperation
33
34 def index(conn, params) do
35 reports = Utils.get_reports(params, params.page, params.page_size)
36
37 render(conn, "index.json", reports: reports)
38 end
39
40 def show(conn, %{id: id}) do
41 with %Activity{} = report <- Activity.get_report(id) do
42 render(conn, "show.json", Report.extract_report_info(report))
43 else
44 _ -> {:error, :not_found}
45 end
46 end
47
48 def update(%{assigns: %{user: admin}, body_params: %{reports: reports}} = conn, _) do
49 result =
50 Enum.map(reports, fn report ->
51 case CommonAPI.update_report_state(report.id, report.state) do
52 {:ok, activity} ->
53 report = Activity.get_by_id_with_user_actor(activity.id)
54
55 ModerationLog.insert_log(%{
56 action: "report_update",
57 actor: admin,
58 subject: activity,
59 subject_actor: report.user_actor
60 })
61
62 activity
63
64 {:error, message} ->
65 %{id: report.id, error: message}
66 end
67 end)
68
69 if Enum.any?(result, &Map.has_key?(&1, :error)) do
70 json_response(conn, :bad_request, result)
71 else
72 json_response(conn, :no_content, "")
73 end
74 end
75
76 def notes_create(%{assigns: %{user: user}, body_params: %{content: content}} = conn, %{
77 id: report_id
78 }) do
79 with {:ok, _} <- ReportNote.create(user.id, report_id, content),
80 report <- Activity.get_by_id_with_user_actor(report_id) do
81 ModerationLog.insert_log(%{
82 action: "report_note",
83 actor: user,
84 subject: report,
85 subject_actor: report.user_actor,
86 text: content
87 })
88
89 json_response(conn, :no_content, "")
90 else
91 _ -> json_response(conn, :bad_request, "")
92 end
93 end
94
95 def notes_delete(%{assigns: %{user: user}} = conn, %{
96 id: note_id,
97 report_id: report_id
98 }) do
99 with {:ok, note} <- ReportNote.destroy(note_id),
100 report <- Activity.get_by_id_with_user_actor(report_id) do
101 ModerationLog.insert_log(%{
102 action: "report_note_delete",
103 actor: user,
104 subject: report,
105 subject_actor: report.user_actor,
106 text: note.content
107 })
108
109 json_response(conn, :no_content, "")
110 else
111 _ -> json_response(conn, :bad_request, "")
112 end
113 end
114 end