total rebase
[anni] / lib / pleroma / web / api_spec / operations / twitter_util_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.TwitterUtilOperation do
6   alias OpenApiSpex.Operation
7   alias OpenApiSpex.Schema
8   alias Pleroma.Web.ApiSpec.Schemas.ApiError
9   alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
10
11   import Pleroma.Web.ApiSpec.Helpers
12
13   def open_api_operation(action) do
14     operation = String.to_existing_atom("#{action}_operation")
15     apply(__MODULE__, operation, [])
16   end
17
18   def emoji_operation do
19     %Operation{
20       tags: ["Custom emojis"],
21       summary: "List all custom emojis",
22       operationId: "UtilController.emoji",
23       parameters: [],
24       responses: %{
25         200 =>
26           Operation.response("List", "application/json", %Schema{
27             type: :object,
28             additionalProperties: %Schema{
29               type: :object,
30               properties: %{
31                 image_url: %Schema{type: :string},
32                 tags: %Schema{type: :array, items: %Schema{type: :string}}
33               },
34               extensions: %{"x-additionalPropertiesName": "Emoji name"}
35             },
36             example: %{
37               "firefox" => %{
38                 "image_url" => "/emoji/firefox.png",
39                 "tag" => ["Fun"]
40               }
41             }
42           })
43       }
44     }
45   end
46
47   def frontend_configurations_operation do
48     %Operation{
49       tags: ["Others"],
50       summary: "Dump frontend configurations",
51       operationId: "UtilController.frontend_configurations",
52       parameters: [],
53       responses: %{
54         200 =>
55           Operation.response("List", "application/json", %Schema{
56             type: :object,
57             additionalProperties: %Schema{
58               type: :object,
59               description:
60                 "Opaque object representing the instance-wide configuration for the frontend",
61               extensions: %{"x-additionalPropertiesName": "Frontend name"}
62             }
63           })
64       }
65     }
66   end
67
68   def change_password_operation do
69     %Operation{
70       tags: ["Account credentials"],
71       summary: "Change account password",
72       security: [%{"oAuth" => ["write:accounts"]}],
73       operationId: "UtilController.change_password",
74       requestBody: request_body("Parameters", change_password_request(), required: true),
75       responses: %{
76         200 =>
77           Operation.response("Success", "application/json", %Schema{
78             type: :object,
79             properties: %{status: %Schema{type: :string, example: "success"}}
80           }),
81         400 => Operation.response("Error", "application/json", ApiError),
82         403 => Operation.response("Error", "application/json", ApiError)
83       }
84     }
85   end
86
87   defp change_password_request do
88     %Schema{
89       title: "ChangePasswordRequest",
90       description: "POST body for changing the account's password",
91       type: :object,
92       required: [:password, :new_password, :new_password_confirmation],
93       properties: %{
94         password: %Schema{type: :string, description: "Current password"},
95         new_password: %Schema{type: :string, description: "New password"},
96         new_password_confirmation: %Schema{
97           type: :string,
98           description: "New password, confirmation"
99         }
100       }
101     }
102   end
103
104   def change_email_operation do
105     %Operation{
106       tags: ["Account credentials"],
107       summary: "Change account email",
108       security: [%{"oAuth" => ["write:accounts"]}],
109       operationId: "UtilController.change_email",
110       requestBody: request_body("Parameters", change_email_request(), required: true),
111       responses: %{
112         200 =>
113           Operation.response("Success", "application/json", %Schema{
114             type: :object,
115             properties: %{status: %Schema{type: :string, example: "success"}}
116           }),
117         400 => Operation.response("Error", "application/json", ApiError),
118         403 => Operation.response("Error", "application/json", ApiError)
119       }
120     }
121   end
122
123   defp change_email_request do
124     %Schema{
125       title: "ChangeEmailRequest",
126       description: "POST body for changing the account's email",
127       type: :object,
128       required: [:email, :password],
129       properties: %{
130         email: %Schema{
131           type: :string,
132           description: "New email. Set to blank to remove the user's email."
133         },
134         password: %Schema{type: :string, description: "Current password"}
135       }
136     }
137   end
138
139   def update_notification_settings_operation do
140     %Operation{
141       tags: ["Settings"],
142       summary: "Update Notification Settings",
143       security: [%{"oAuth" => ["write:accounts"]}],
144       operationId: "UtilController.update_notification_settings",
145       parameters: [
146         Operation.parameter(
147           :block_from_strangers,
148           :query,
149           BooleanLike.schema(),
150           "blocks notifications from accounts you do not follow"
151         ),
152         Operation.parameter(
153           :hide_notification_contents,
154           :query,
155           BooleanLike.schema(),
156           "removes the contents of a message from the push notification"
157         )
158       ],
159       requestBody: nil,
160       responses: %{
161         200 =>
162           Operation.response("Success", "application/json", %Schema{
163             type: :object,
164             properties: %{status: %Schema{type: :string, example: "success"}}
165           }),
166         400 => Operation.response("Error", "application/json", ApiError)
167       }
168     }
169   end
170
171   def disable_account_operation do
172     %Operation{
173       tags: ["Account credentials"],
174       summary: "Disable Account",
175       security: [%{"oAuth" => ["write:accounts"]}],
176       operationId: "UtilController.disable_account",
177       parameters: [
178         Operation.parameter(:password, :query, :string, "Password")
179       ],
180       responses: %{
181         200 =>
182           Operation.response("Success", "application/json", %Schema{
183             type: :object,
184             properties: %{status: %Schema{type: :string, example: "success"}}
185           }),
186         403 => Operation.response("Error", "application/json", ApiError)
187       }
188     }
189   end
190
191   def delete_account_operation do
192     %Operation{
193       tags: ["Account credentials"],
194       summary: "Delete Account",
195       security: [%{"oAuth" => ["write:accounts"]}],
196       operationId: "UtilController.delete_account",
197       parameters: [
198         Operation.parameter(:password, :query, :string, "Password")
199       ],
200       requestBody: request_body("Parameters", delete_account_request(), required: false),
201       responses: %{
202         200 =>
203           Operation.response("Success", "application/json", %Schema{
204             type: :object,
205             properties: %{status: %Schema{type: :string, example: "success"}}
206           }),
207         403 => Operation.response("Error", "application/json", ApiError)
208       }
209     }
210   end
211
212   def captcha_operation do
213     %Operation{
214       summary: "Get a captcha",
215       operationId: "UtilController.captcha",
216       tags: ["Others"],
217       parameters: [],
218       responses: %{
219         200 => Operation.response("Success", "application/json", %Schema{type: :object})
220       }
221     }
222   end
223
224   def move_account_operation do
225     %Operation{
226       tags: ["Account credentials"],
227       summary: "Move account",
228       security: [%{"oAuth" => ["write:accounts"]}],
229       operationId: "UtilController.move_account",
230       requestBody: request_body("Parameters", move_account_request(), required: true),
231       responses: %{
232         200 =>
233           Operation.response("Success", "application/json", %Schema{
234             type: :object,
235             properties: %{status: %Schema{type: :string, example: "success"}}
236           }),
237         400 => Operation.response("Error", "application/json", ApiError),
238         403 => Operation.response("Error", "application/json", ApiError),
239         404 => Operation.response("Error", "application/json", ApiError)
240       }
241     }
242   end
243
244   defp move_account_request do
245     %Schema{
246       title: "MoveAccountRequest",
247       description: "POST body for moving the account",
248       type: :object,
249       required: [:password, :target_account],
250       properties: %{
251         password: %Schema{type: :string, description: "Current password"},
252         target_account: %Schema{
253           type: :string,
254           description: "The nickname of the target account to move to"
255         }
256       }
257     }
258   end
259
260   def list_aliases_operation do
261     %Operation{
262       tags: ["Account credentials"],
263       summary: "List account aliases",
264       security: [%{"oAuth" => ["read:accounts"]}],
265       operationId: "UtilController.list_aliases",
266       responses: %{
267         200 =>
268           Operation.response("Success", "application/json", %Schema{
269             type: :object,
270             properties: %{
271               aliases: %Schema{
272                 type: :array,
273                 items: %Schema{type: :string},
274                 example: ["foo@example.org"]
275               }
276             }
277           }),
278         400 => Operation.response("Error", "application/json", ApiError),
279         403 => Operation.response("Error", "application/json", ApiError)
280       }
281     }
282   end
283
284   def add_alias_operation do
285     %Operation{
286       tags: ["Account credentials"],
287       summary: "Add an alias to this account",
288       security: [%{"oAuth" => ["write:accounts"]}],
289       operationId: "UtilController.add_alias",
290       requestBody: request_body("Parameters", add_alias_request(), required: true),
291       responses: %{
292         200 =>
293           Operation.response("Success", "application/json", %Schema{
294             type: :object,
295             properties: %{
296               status: %Schema{
297                 type: :string,
298                 example: "success"
299               }
300             }
301           }),
302         400 => Operation.response("Error", "application/json", ApiError),
303         403 => Operation.response("Error", "application/json", ApiError),
304         404 => Operation.response("Error", "application/json", ApiError)
305       }
306     }
307   end
308
309   defp add_alias_request do
310     %Schema{
311       title: "AddAliasRequest",
312       description: "PUT body for adding aliases",
313       type: :object,
314       required: [:alias],
315       properties: %{
316         alias: %Schema{
317           type: :string,
318           description: "The nickname of the account to add to aliases"
319         }
320       }
321     }
322   end
323
324   def delete_alias_operation do
325     %Operation{
326       tags: ["Account credentials"],
327       summary: "Delete an alias from this account",
328       security: [%{"oAuth" => ["write:accounts"]}],
329       operationId: "UtilController.delete_alias",
330       requestBody: request_body("Parameters", delete_alias_request(), required: true),
331       responses: %{
332         200 =>
333           Operation.response("Success", "application/json", %Schema{
334             type: :object,
335             properties: %{
336               status: %Schema{
337                 type: :string,
338                 example: "success"
339               }
340             }
341           }),
342         400 => Operation.response("Error", "application/json", ApiError),
343         403 => Operation.response("Error", "application/json", ApiError),
344         404 => Operation.response("Error", "application/json", ApiError)
345       }
346     }
347   end
348
349   defp delete_alias_request do
350     %Schema{
351       title: "DeleteAliasRequest",
352       description: "PUT body for deleting aliases",
353       type: :object,
354       required: [:alias],
355       properties: %{
356         alias: %Schema{
357           type: :string,
358           description: "The nickname of the account to delete from aliases"
359         }
360       }
361     }
362   end
363
364   def healthcheck_operation do
365     %Operation{
366       tags: ["Others"],
367       summary: "Quick status check on the instance",
368       security: [%{"oAuth" => ["write:accounts"]}],
369       operationId: "UtilController.healthcheck",
370       parameters: [],
371       responses: %{
372         200 => Operation.response("Healthy", "application/json", %Schema{type: :object}),
373         503 =>
374           Operation.response("Disabled or Unhealthy", "application/json", %Schema{type: :object})
375       }
376     }
377   end
378
379   def remote_subscribe_operation do
380     %Operation{
381       tags: ["Remote interaction"],
382       summary: "Remote Subscribe",
383       operationId: "UtilController.remote_subscribe",
384       parameters: [],
385       responses: %{200 => Operation.response("Web Page", "test/html", %Schema{type: :string})}
386     }
387   end
388
389   def remote_interaction_operation do
390     %Operation{
391       tags: ["Remote interaction"],
392       summary: "Remote interaction",
393       operationId: "UtilController.remote_interaction",
394       requestBody: request_body("Parameters", remote_interaction_request(), required: true),
395       responses: %{
396         200 =>
397           Operation.response("Remote interaction URL", "application/json", %Schema{type: :object})
398       }
399     }
400   end
401
402   defp remote_interaction_request do
403     %Schema{
404       title: "RemoteInteractionRequest",
405       description: "POST body for remote interaction",
406       type: :object,
407       required: [:ap_id, :profile],
408       properties: %{
409         ap_id: %Schema{type: :string, description: "Profile or status ActivityPub ID"},
410         profile: %Schema{type: :string, description: "Remote profile webfinger"}
411       }
412     }
413   end
414
415   def show_subscribe_form_operation do
416     %Operation{
417       tags: ["Remote interaction"],
418       summary: "Show remote subscribe form",
419       operationId: "UtilController.show_subscribe_form",
420       parameters: [],
421       responses: %{200 => Operation.response("Web Page", "test/html", %Schema{type: :string})}
422     }
423   end
424
425   defp delete_account_request do
426     %Schema{
427       title: "AccountDeleteRequest",
428       description: "POST body for deleting one's own account",
429       type: :object,
430       properties: %{
431         password: %Schema{
432           type: :string,
433           description: "The user's own password for confirmation.",
434           format: :password
435         }
436       },
437       example: %{
438         "password" => "prettyp0ony1313"
439       }
440     }
441   end
442 end