1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.ReportController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
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
21 plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
22 plug(OAuthScopesPlug, %{scopes: ["admin:read:reports"]} when action in [:index, :show])
26 %{scopes: ["admin:write:reports"]}
27 when action in [:update, :notes_create, :notes_delete]
30 action_fallback(AdminAPI.FallbackController)
32 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ReportOperation
34 def index(%{private: %{open_api_spex: %{params: params}}} = conn, _) do
35 reports = Utils.get_reports(params, params.page, params.page_size)
37 render(conn, "index.json", reports: reports)
40 def show(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
41 with %Activity{} = report <- Activity.get_report(id) do
42 render(conn, "show.json", Report.extract_report_info(report))
44 _ -> {:error, :not_found}
50 assigns: %{user: admin},
51 private: %{open_api_spex: %{body_params: %{reports: reports}}}
56 Enum.map(reports, fn report ->
57 case CommonAPI.update_report_state(report.id, report.state) do
59 report = Activity.get_by_id_with_user_actor(activity.id)
61 ModerationLog.insert_log(%{
62 action: "report_update",
65 subject_actor: report.user_actor
71 %{id: report.id, error: message}
75 if Enum.any?(result, &Map.has_key?(&1, :error)) do
76 json_response(conn, :bad_request, result)
78 json_response(conn, :no_content, "")
84 assigns: %{user: user},
85 private: %{open_api_spex: %{body_params: %{content: content}, params: %{id: report_id}}}
89 with {:ok, _} <- ReportNote.create(user.id, report_id, content),
90 report <- Activity.get_by_id_with_user_actor(report_id) do
91 ModerationLog.insert_log(%{
92 action: "report_note",
95 subject_actor: report.user_actor,
99 json_response(conn, :no_content, "")
101 _ -> json_response(conn, :bad_request, "")
107 assigns: %{user: user},
119 with {:ok, note} <- ReportNote.destroy(note_id),
120 report <- Activity.get_by_id_with_user_actor(report_id) do
121 ModerationLog.insert_log(%{
122 action: "report_note_delete",
125 subject_actor: report.user_actor,
129 json_response(conn, :no_content, "")
131 _ -> json_response(conn, :bad_request, "")