Merge branch 'develop' into openapi/admin/config
[akkoma] / lib / pleroma / web / api_spec / operations / admin / report_operation.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ApiSpec.Admin.ReportOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8 alias Pleroma.Web.ApiSpec.Schemas.Account
9 alias Pleroma.Web.ApiSpec.Schemas.ApiError
10 alias Pleroma.Web.ApiSpec.Schemas.FlakeID
11 alias Pleroma.Web.ApiSpec.Schemas.Status
12
13 import Pleroma.Web.ApiSpec.Helpers
14
15 def open_api_operation(action) do
16 operation = String.to_existing_atom("#{action}_operation")
17 apply(__MODULE__, operation, [])
18 end
19
20 def index_operation do
21 %Operation{
22 tags: ["Admin", "Reports"],
23 summary: "Get a list of reports",
24 operationId: "AdminAPI.ReportController.index",
25 security: [%{"oAuth" => ["read:reports"]}],
26 parameters: [
27 Operation.parameter(
28 :state,
29 :query,
30 report_state(),
31 "Filter by report state"
32 ),
33 Operation.parameter(
34 :limit,
35 :query,
36 %Schema{type: :integer},
37 "The number of records to retrieve"
38 ),
39 Operation.parameter(
40 :page,
41 :query,
42 %Schema{type: :integer, default: 1},
43 "Page number"
44 ),
45 Operation.parameter(
46 :page_size,
47 :query,
48 %Schema{type: :integer, default: 50},
49 "Number number of log entries per page"
50 )
51 ],
52 responses: %{
53 200 =>
54 Operation.response("Response", "application/json", %Schema{
55 type: :object,
56 properties: %{
57 total: %Schema{type: :integer},
58 reports: %Schema{
59 type: :array,
60 items: report()
61 }
62 }
63 }),
64 403 => Operation.response("Forbidden", "application/json", ApiError)
65 }
66 }
67 end
68
69 def show_operation do
70 %Operation{
71 tags: ["Admin", "Reports"],
72 summary: "Get an individual report",
73 operationId: "AdminAPI.ReportController.show",
74 parameters: [id_param()],
75 security: [%{"oAuth" => ["read:reports"]}],
76 responses: %{
77 200 => Operation.response("Report", "application/json", report()),
78 404 => Operation.response("Not Found", "application/json", ApiError)
79 }
80 }
81 end
82
83 def update_operation do
84 %Operation{
85 tags: ["Admin", "Reports"],
86 summary: "Change the state of one or multiple reports",
87 operationId: "AdminAPI.ReportController.update",
88 security: [%{"oAuth" => ["write:reports"]}],
89 requestBody: request_body("Parameters", update_request(), required: true),
90 responses: %{
91 204 => no_content_response(),
92 400 => Operation.response("Bad Request", "application/json", update_400_response()),
93 403 => Operation.response("Forbidden", "application/json", ApiError)
94 }
95 }
96 end
97
98 def notes_create_operation do
99 %Operation{
100 tags: ["Admin", "Reports"],
101 summary: "Create report note",
102 operationId: "AdminAPI.ReportController.notes_create",
103 parameters: [id_param()],
104 requestBody:
105 request_body("Parameters", %Schema{
106 type: :object,
107 properties: %{
108 content: %Schema{type: :string, description: "The message"}
109 }
110 }),
111 security: [%{"oAuth" => ["write:reports"]}],
112 responses: %{
113 204 => no_content_response(),
114 404 => Operation.response("Not Found", "application/json", ApiError)
115 }
116 }
117 end
118
119 def notes_delete_operation do
120 %Operation{
121 tags: ["Admin", "Reports"],
122 summary: "Delete report note",
123 operationId: "AdminAPI.ReportController.notes_delete",
124 parameters: [
125 Operation.parameter(:report_id, :path, :string, "Report ID"),
126 Operation.parameter(:id, :path, :string, "Note ID")
127 ],
128 security: [%{"oAuth" => ["write:reports"]}],
129 responses: %{
130 204 => no_content_response(),
131 404 => Operation.response("Not Found", "application/json", ApiError)
132 }
133 }
134 end
135
136 defp report_state do
137 %Schema{type: :string, enum: ["open", "closed", "resolved"]}
138 end
139
140 defp id_param do
141 Operation.parameter(:id, :path, FlakeID, "Report ID",
142 example: "9umDrYheeY451cQnEe",
143 required: true
144 )
145 end
146
147 defp report do
148 %Schema{
149 type: :object,
150 properties: %{
151 id: FlakeID,
152 state: report_state(),
153 account: account_admin(),
154 actor: account_admin(),
155 content: %Schema{type: :string},
156 created_at: %Schema{type: :string, format: :"date-time"},
157 statuses: %Schema{type: :array, items: Status},
158 notes: %Schema{
159 type: :array,
160 items: %Schema{
161 type: :object,
162 properties: %{
163 id: %Schema{type: :integer},
164 user_id: FlakeID,
165 content: %Schema{type: :string},
166 inserted_at: %Schema{type: :string, format: :"date-time"}
167 }
168 }
169 }
170 }
171 }
172 end
173
174 defp account_admin do
175 %Schema{
176 title: "Account",
177 description: "Account view for admins",
178 type: :object,
179 properties:
180 Map.merge(Account.schema().properties, %{
181 nickname: %Schema{type: :string},
182 deactivated: %Schema{type: :boolean},
183 local: %Schema{type: :boolean},
184 roles: %Schema{
185 type: :object,
186 properties: %{
187 admin: %Schema{type: :boolean},
188 moderator: %Schema{type: :boolean}
189 }
190 },
191 confirmation_pending: %Schema{type: :boolean}
192 })
193 }
194 end
195
196 defp update_request do
197 %Schema{
198 type: :object,
199 required: [:reports],
200 properties: %{
201 reports: %Schema{
202 type: :array,
203 items: %Schema{
204 type: :object,
205 properties: %{
206 id: %Schema{allOf: [FlakeID], description: "Required, report ID"},
207 state: %Schema{
208 type: :string,
209 description:
210 "Required, the new state. Valid values are `open`, `closed` and `resolved`"
211 }
212 }
213 },
214 example: %{
215 "reports" => [
216 %{"id" => "123", "state" => "closed"},
217 %{"id" => "1337", "state" => "resolved"}
218 ]
219 }
220 }
221 }
222 }
223 end
224
225 defp update_400_response do
226 %Schema{
227 type: :array,
228 items: %Schema{
229 type: :object,
230 properties: %{
231 id: %Schema{allOf: [FlakeID], description: "Report ID"},
232 error: %Schema{type: :string, description: "Error message"}
233 }
234 }
235 }
236 end
237 end