First
[anni] / lib / pleroma / web / api_spec / operations / admin / report_operation.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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: ["Report managment"],
23       summary: "Retrieve a list of reports",
24       operationId: "AdminAPI.ReportController.index",
25       security: [%{"oAuth" => ["admin: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         | admin_api_params()
52       ],
53       responses: %{
54         200 =>
55           Operation.response("Response", "application/json", %Schema{
56             type: :object,
57             properties: %{
58               total: %Schema{type: :integer},
59               reports: %Schema{
60                 type: :array,
61                 items: report()
62               }
63             }
64           }),
65         403 => Operation.response("Forbidden", "application/json", ApiError)
66       }
67     }
68   end
69
70   def show_operation do
71     %Operation{
72       tags: ["Report managment"],
73       summary: "Retrieve a report",
74       operationId: "AdminAPI.ReportController.show",
75       parameters: [id_param() | admin_api_params()],
76       security: [%{"oAuth" => ["admin:read:reports"]}],
77       responses: %{
78         200 => Operation.response("Report", "application/json", report()),
79         404 => Operation.response("Not Found", "application/json", ApiError)
80       }
81     }
82   end
83
84   def update_operation do
85     %Operation{
86       tags: ["Report managment"],
87       summary: "Change state of specified reports",
88       operationId: "AdminAPI.ReportController.update",
89       security: [%{"oAuth" => ["admin:write:reports"]}],
90       parameters: admin_api_params(),
91       requestBody: request_body("Parameters", update_request(), required: true),
92       responses: %{
93         204 => no_content_response(),
94         400 => Operation.response("Bad Request", "application/json", update_400_response()),
95         403 => Operation.response("Forbidden", "application/json", ApiError)
96       }
97     }
98   end
99
100   def notes_create_operation do
101     %Operation{
102       tags: ["Report managment"],
103       summary: "Add a note to the report",
104       operationId: "AdminAPI.ReportController.notes_create",
105       parameters: [id_param() | admin_api_params()],
106       requestBody:
107         request_body("Parameters", %Schema{
108           type: :object,
109           properties: %{
110             content: %Schema{type: :string, description: "The message"}
111           }
112         }),
113       security: [%{"oAuth" => ["admin:write:reports"]}],
114       responses: %{
115         204 => no_content_response(),
116         404 => Operation.response("Not Found", "application/json", ApiError)
117       }
118     }
119   end
120
121   def notes_delete_operation do
122     %Operation{
123       tags: ["Report managment"],
124       summary: "Delete note attached to the report",
125       operationId: "AdminAPI.ReportController.notes_delete",
126       parameters: [
127         Operation.parameter(:report_id, :path, :string, "Report ID"),
128         Operation.parameter(:id, :path, :string, "Note ID")
129         | admin_api_params()
130       ],
131       security: [%{"oAuth" => ["admin:write:reports"]}],
132       responses: %{
133         204 => no_content_response(),
134         404 => Operation.response("Not Found", "application/json", ApiError)
135       }
136     }
137   end
138
139   def report_state do
140     %Schema{type: :string, enum: ["open", "closed", "resolved"]}
141   end
142
143   def id_param do
144     Operation.parameter(:id, :path, FlakeID, "Report ID",
145       example: "9umDrYheeY451cQnEe",
146       required: true
147     )
148   end
149
150   defp report do
151     %Schema{
152       type: :object,
153       properties: %{
154         id: FlakeID,
155         state: report_state(),
156         account: account_admin(),
157         actor: account_admin(),
158         content: %Schema{type: :string},
159         created_at: %Schema{type: :string, format: :"date-time"},
160         statuses: %Schema{type: :array, items: Status},
161         notes: %Schema{
162           type: :array,
163           items: %Schema{
164             type: :object,
165             properties: %{
166               id: %Schema{type: :integer},
167               user_id: FlakeID,
168               content: %Schema{type: :string},
169               inserted_at: %Schema{type: :string, format: :"date-time"}
170             }
171           }
172         }
173       }
174     }
175   end
176
177   defp account_admin do
178     %Schema{
179       title: "Account",
180       description: "Account view for admins",
181       type: :object,
182       properties:
183         Map.merge(Account.schema().properties, %{
184           nickname: %Schema{type: :string},
185           is_active: %Schema{type: :boolean},
186           local: %Schema{type: :boolean},
187           roles: %Schema{
188             type: :object,
189             properties: %{
190               admin: %Schema{type: :boolean},
191               moderator: %Schema{type: :boolean}
192             }
193           },
194           is_confirmed: %Schema{type: :boolean}
195         })
196     }
197   end
198
199   defp update_request do
200     %Schema{
201       type: :object,
202       required: [:reports],
203       properties: %{
204         reports: %Schema{
205           type: :array,
206           items: %Schema{
207             type: :object,
208             properties: %{
209               id: %Schema{allOf: [FlakeID], description: "Required, report ID"},
210               state: %Schema{
211                 type: :string,
212                 description:
213                   "Required, the new state. Valid values are `open`, `closed` and `resolved`"
214               }
215             }
216           },
217           example: %{
218             "reports" => [
219               %{"id" => "123", "state" => "closed"},
220               %{"id" => "1337", "state" => "resolved"}
221             ]
222           }
223         }
224       }
225     }
226   end
227
228   defp update_400_response do
229     %Schema{
230       type: :array,
231       items: %Schema{
232         type: :object,
233         properties: %{
234           id: %Schema{allOf: [FlakeID], description: "Report ID"},
235           error: %Schema{type: :string, description: "Error message"}
236         }
237       }
238     }
239   end
240 end