total rebase
[anni] / lib / pleroma / web / api_spec / operations / admin / announcement_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.AnnouncementOperation do
6   alias OpenApiSpex.Operation
7   alias OpenApiSpex.Schema
8   alias Pleroma.Web.ApiSpec.Schemas.Announcement
9   alias Pleroma.Web.ApiSpec.Schemas.ApiError
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 index_operation do
19     %Operation{
20       tags: ["Announcement management"],
21       summary: "Retrieve a list of announcements",
22       operationId: "AdminAPI.AnnouncementController.index",
23       security: [%{"oAuth" => ["admin:read"]}],
24       parameters: [
25         Operation.parameter(
26           :limit,
27           :query,
28           %Schema{type: :integer, minimum: 1},
29           "the maximum number of announcements to return"
30         ),
31         Operation.parameter(
32           :offset,
33           :query,
34           %Schema{type: :integer, minimum: 0},
35           "the offset of the first announcement to return"
36         )
37         | admin_api_params()
38       ],
39       responses: %{
40         200 => Operation.response("Response", "application/json", list_of_announcements()),
41         400 => Operation.response("Forbidden", "application/json", ApiError),
42         403 => Operation.response("Forbidden", "application/json", ApiError)
43       }
44     }
45   end
46
47   def show_operation do
48     %Operation{
49       tags: ["Announcement management"],
50       summary: "Display one announcement",
51       operationId: "AdminAPI.AnnouncementController.show",
52       security: [%{"oAuth" => ["admin:read"]}],
53       parameters: [
54         Operation.parameter(
55           :id,
56           :path,
57           :string,
58           "announcement id"
59         )
60         | admin_api_params()
61       ],
62       responses: %{
63         200 => Operation.response("Response", "application/json", Announcement),
64         403 => Operation.response("Forbidden", "application/json", ApiError),
65         404 => Operation.response("Not Found", "application/json", ApiError)
66       }
67     }
68   end
69
70   def delete_operation do
71     %Operation{
72       tags: ["Announcement management"],
73       summary: "Delete one announcement",
74       operationId: "AdminAPI.AnnouncementController.delete",
75       security: [%{"oAuth" => ["admin:write"]}],
76       parameters: [
77         Operation.parameter(
78           :id,
79           :path,
80           :string,
81           "announcement id"
82         )
83         | admin_api_params()
84       ],
85       responses: %{
86         200 => Operation.response("Response", "application/json", %Schema{type: :object}),
87         403 => Operation.response("Forbidden", "application/json", ApiError),
88         404 => Operation.response("Not Found", "application/json", ApiError)
89       }
90     }
91   end
92
93   def create_operation do
94     %Operation{
95       tags: ["Announcement management"],
96       summary: "Create one announcement",
97       operationId: "AdminAPI.AnnouncementController.create",
98       security: [%{"oAuth" => ["admin:write"]}],
99       requestBody: request_body("Parameters", create_request(), required: true),
100       responses: %{
101         200 => Operation.response("Response", "application/json", Announcement),
102         400 => Operation.response("Bad Request", "application/json", ApiError),
103         403 => Operation.response("Forbidden", "application/json", ApiError)
104       }
105     }
106   end
107
108   def change_operation do
109     %Operation{
110       tags: ["Announcement management"],
111       summary: "Change one announcement",
112       operationId: "AdminAPI.AnnouncementController.change",
113       security: [%{"oAuth" => ["admin:write"]}],
114       parameters: [
115         Operation.parameter(
116           :id,
117           :path,
118           :string,
119           "announcement id"
120         )
121         | admin_api_params()
122       ],
123       requestBody: request_body("Parameters", change_request(), required: true),
124       responses: %{
125         200 => Operation.response("Response", "application/json", Announcement),
126         400 => Operation.response("Bad Request", "application/json", ApiError),
127         403 => Operation.response("Forbidden", "application/json", ApiError),
128         404 => Operation.response("Not Found", "application/json", ApiError)
129       }
130     }
131   end
132
133   defp create_or_change_props do
134     %{
135       content: %Schema{type: :string},
136       starts_at: %Schema{type: :string, format: "date-time", nullable: true},
137       ends_at: %Schema{type: :string, format: "date-time", nullable: true},
138       all_day: %Schema{type: :boolean}
139     }
140   end
141
142   def create_request do
143     %Schema{
144       title: "AnnouncementCreateRequest",
145       type: :object,
146       required: [:content],
147       properties: create_or_change_props()
148     }
149   end
150
151   def change_request do
152     %Schema{
153       title: "AnnouncementChangeRequest",
154       type: :object,
155       properties: create_or_change_props()
156     }
157   end
158
159   def list_of_announcements do
160     %Schema{
161       type: :array,
162       items: Announcement
163     }
164   end
165 end