report_controller.ex 2.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.ReportController do
  use Pleroma.Web, :controller

  import Pleroma.Web.ControllerHelper, only: [json_response: 3]

  alias Pleroma.Activity
  alias Pleroma.ModerationLog
  alias Pleroma.ReportNote
  alias Pleroma.Web.ActivityPub.Utils
  alias Pleroma.Web.AdminAPI
  alias Pleroma.Web.AdminAPI.Report
  alias Pleroma.Web.CommonAPI
17
  alias Pleroma.Web.Plugs.OAuthScopesPlug
18
19
20

  require Logger

21
  plug(Pleroma.Web.ApiSpec.CastAndValidate)
22
23
24
25
26
27
28
29
30
31
  plug(OAuthScopesPlug, %{scopes: ["read:reports"], admin: true} when action in [:index, :show])

  plug(
    OAuthScopesPlug,
    %{scopes: ["write:reports"], admin: true}
    when action in [:update, :notes_create, :notes_delete]
  )

  action_fallback(AdminAPI.FallbackController)

32
  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ReportOperation
33

34
35
  def index(conn, params) do
    reports = Utils.get_reports(params, params.page, params.page_size)
36
37
38
39

    render(conn, "index.json", reports: reports)
  end

40
  def show(conn, %{id: id}) do
41
    with %Activity{} = report <- Activity.get_report(id) do
42
43
44
45
46
47
      render(conn, "show.json", Report.extract_report_info(report))
    else
      _ -> {:error, :not_found}
    end
  end

48
  def update(%{assigns: %{user: admin}, body_params: %{reports: reports}} = conn, _) do
49
    result =
50
51
52
53
54
55
56
57
58
59
60
61
62
      Enum.map(reports, fn report ->
        case CommonAPI.update_report_state(report.id, report.state) do
          {:ok, activity} ->
            ModerationLog.insert_log(%{
              action: "report_update",
              actor: admin,
              subject: activity
            })

            activity

          {:error, message} ->
            %{id: report.id, error: message}
63
64
65
        end
      end)

66
67
68
69
    if Enum.any?(result, &Map.has_key?(&1, :error)) do
      json_response(conn, :bad_request, result)
    else
      json_response(conn, :no_content, "")
70
71
72
    end
  end

73
74
  def notes_create(%{assigns: %{user: user}, body_params: %{content: content}} = conn, %{
        id: report_id
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      }) do
    with {:ok, _} <- ReportNote.create(user.id, report_id, content) do
      ModerationLog.insert_log(%{
        action: "report_note",
        actor: user,
        subject: Activity.get_by_id(report_id),
        text: content
      })

      json_response(conn, :no_content, "")
    else
      _ -> json_response(conn, :bad_request, "")
    end
  end

  def notes_delete(%{assigns: %{user: user}} = conn, %{
91
92
        id: note_id,
        report_id: report_id
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      }) do
    with {:ok, note} <- ReportNote.destroy(note_id) do
      ModerationLog.insert_log(%{
        action: "report_note_delete",
        actor: user,
        subject: Activity.get_by_id(report_id),
        text: note.content
      })

      json_response(conn, :no_content, "")
    else
      _ -> json_response(conn, :bad_request, "")
    end
  end
end