summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/api_spec/operations/admin/report_operation.ex
blob: 8d7577505c61805538c3260edbf38ebc2507951d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ApiSpec.Admin.ReportOperation do
  alias OpenApiSpex.Operation
  alias OpenApiSpex.Schema
  alias Pleroma.Web.ApiSpec.Schemas.Account
  alias Pleroma.Web.ApiSpec.Schemas.ApiError
  alias Pleroma.Web.ApiSpec.Schemas.FlakeID
  alias Pleroma.Web.ApiSpec.Schemas.Status

  import Pleroma.Web.ApiSpec.Helpers

  def open_api_operation(action) do
    operation = String.to_existing_atom("#{action}_operation")
    apply(__MODULE__, operation, [])
  end

  def index_operation do
    %Operation{
      tags: ["Report managment"],
      summary: "Retrieve a list of reports",
      operationId: "AdminAPI.ReportController.index",
      security: [%{"oAuth" => ["admin:read:reports"]}],
      parameters: [
        Operation.parameter(
          :state,
          :query,
          report_state(),
          "Filter by report state"
        ),
        Operation.parameter(
          :limit,
          :query,
          %Schema{type: :integer},
          "The number of records to retrieve"
        ),
        Operation.parameter(
          :page,
          :query,
          %Schema{type: :integer, default: 1},
          "Page number"
        ),
        Operation.parameter(
          :page_size,
          :query,
          %Schema{type: :integer, default: 50},
          "Number number of log entries per page"
        )
        | admin_api_params()
      ],
      responses: %{
        200 =>
          Operation.response("Response", "application/json", %Schema{
            type: :object,
            properties: %{
              total: %Schema{type: :integer},
              reports: %Schema{
                type: :array,
                items: report()
              }
            }
          }),
        403 => Operation.response("Forbidden", "application/json", ApiError)
      }
    }
  end

  def show_operation do
    %Operation{
      tags: ["Report managment"],
      summary: "Retrieve a report",
      operationId: "AdminAPI.ReportController.show",
      parameters: [id_param() | admin_api_params()],
      security: [%{"oAuth" => ["admin:read:reports"]}],
      responses: %{
        200 => Operation.response("Report", "application/json", report()),
        404 => Operation.response("Not Found", "application/json", ApiError)
      }
    }
  end

  def update_operation do
    %Operation{
      tags: ["Report managment"],
      summary: "Change state of specified reports",
      operationId: "AdminAPI.ReportController.update",
      security: [%{"oAuth" => ["admin:write:reports"]}],
      parameters: admin_api_params(),
      requestBody: request_body("Parameters", update_request(), required: true),
      responses: %{
        204 => no_content_response(),
        400 => Operation.response("Bad Request", "application/json", update_400_response()),
        403 => Operation.response("Forbidden", "application/json", ApiError)
      }
    }
  end

  def notes_create_operation do
    %Operation{
      tags: ["Report managment"],
      summary: "Add a note to the report",
      operationId: "AdminAPI.ReportController.notes_create",
      parameters: [id_param() | admin_api_params()],
      requestBody:
        request_body("Parameters", %Schema{
          type: :object,
          properties: %{
            content: %Schema{type: :string, description: "The message"}
          }
        }),
      security: [%{"oAuth" => ["admin:write:reports"]}],
      responses: %{
        204 => no_content_response(),
        404 => Operation.response("Not Found", "application/json", ApiError)
      }
    }
  end

  def notes_delete_operation do
    %Operation{
      tags: ["Report managment"],
      summary: "Delete note attached to the report",
      operationId: "AdminAPI.ReportController.notes_delete",
      parameters: [
        Operation.parameter(:report_id, :path, :string, "Report ID"),
        Operation.parameter(:id, :path, :string, "Note ID")
        | admin_api_params()
      ],
      security: [%{"oAuth" => ["admin:write:reports"]}],
      responses: %{
        204 => no_content_response(),
        404 => Operation.response("Not Found", "application/json", ApiError)
      }
    }
  end

  def report_state do
    %Schema{type: :string, enum: ["open", "closed", "resolved"]}
  end

  def id_param do
    Operation.parameter(:id, :path, FlakeID, "Report ID",
      example: "9umDrYheeY451cQnEe",
      required: true
    )
  end

  defp report do
    %Schema{
      type: :object,
      properties: %{
        id: FlakeID,
        state: report_state(),
        account: account_admin(),
        actor: account_admin(),
        content: %Schema{type: :string},
        created_at: %Schema{type: :string, format: :"date-time"},
        statuses: %Schema{type: :array, items: Status},
        notes: %Schema{
          type: :array,
          items: %Schema{
            type: :object,
            properties: %{
              id: %Schema{type: :integer},
              user_id: FlakeID,
              content: %Schema{type: :string},
              inserted_at: %Schema{type: :string, format: :"date-time"}
            }
          }
        }
      }
    }
  end

  defp account_admin do
    %Schema{
      title: "Account",
      description: "Account view for admins",
      type: :object,
      properties:
        Map.merge(Account.schema().properties, %{
          nickname: %Schema{type: :string},
          is_active: %Schema{type: :boolean},
          local: %Schema{type: :boolean},
          roles: %Schema{
            type: :object,
            properties: %{
              admin: %Schema{type: :boolean},
              moderator: %Schema{type: :boolean}
            }
          },
          is_confirmed: %Schema{type: :boolean}
        })
    }
  end

  defp update_request do
    %Schema{
      type: :object,
      required: [:reports],
      properties: %{
        reports: %Schema{
          type: :array,
          items: %Schema{
            type: :object,
            properties: %{
              id: %Schema{allOf: [FlakeID], description: "Required, report ID"},
              state: %Schema{
                type: :string,
                description:
                  "Required, the new state. Valid values are `open`, `closed` and `resolved`"
              }
            }
          },
          example: %{
            "reports" => [
              %{"id" => "123", "state" => "closed"},
              %{"id" => "1337", "state" => "resolved"}
            ]
          }
        }
      }
    }
  end

  defp update_400_response do
    %Schema{
      type: :array,
      items: %Schema{
        type: :object,
        properties: %{
          id: %Schema{allOf: [FlakeID], description: "Report ID"},
          error: %Schema{type: :string, description: "Error message"}
        }
      }
    }
  end
end