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)
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(conn, params) do
35 reports = Utils.get_reports(params, params.page, params.page_size)
37 render(conn, "index.json", reports: reports)
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))
44 _ -> {:error, :not_found}
48 def update(%{assigns: %{user: admin}, body_params: %{reports: reports}} = conn, _) do
50 Enum.map(reports, fn report ->
51 case CommonAPI.update_report_state(report.id, report.state) do
53 report = Activity.get_by_id_with_user_actor(activity.id)
55 ModerationLog.insert_log(%{
56 action: "report_update",
59 subject_actor: report.user_actor
65 %{id: report.id, error: message}
69 if Enum.any?(result, &Map.has_key?(&1, :error)) do
70 json_response(conn, :bad_request, result)
72 json_response(conn, :no_content, "")
76 def notes_create(%{assigns: %{user: user}, body_params: %{content: content}} = conn, %{
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",
85 subject_actor: report.user_actor,
89 json_response(conn, :no_content, "")
91 _ -> json_response(conn, :bad_request, "")
95 def notes_delete(%{assigns: %{user: user}} = conn, %{
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",
105 subject_actor: report.user_actor,
109 json_response(conn, :no_content, "")
111 _ -> json_response(conn, :bad_request, "")