Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / api_spec / operations / pleroma_emoji_pack_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.PleromaEmojiPackOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8 alias Pleroma.Web.ApiSpec.Schemas.ApiError
9
10 import Pleroma.Web.ApiSpec.Helpers
11
12 def open_api_operation(action) do
13 operation = String.to_existing_atom("#{action}_operation")
14 apply(__MODULE__, operation, [])
15 end
16
17 def remote_operation do
18 %Operation{
19 tags: ["Emoji Packs"],
20 summary: "Make request to another instance for emoji packs list",
21 security: [%{"oAuth" => ["write"]}],
22 parameters: [url_param()],
23 operationId: "PleromaAPI.EmojiPackController.remote",
24 responses: %{
25 200 => emoji_packs_response(),
26 500 => Operation.response("Error", "application/json", ApiError)
27 }
28 }
29 end
30
31 def index_operation do
32 %Operation{
33 tags: ["Emoji Packs"],
34 summary: "Lists local custom emoji packs",
35 operationId: "PleromaAPI.EmojiPackController.index",
36 responses: %{
37 200 => emoji_packs_response()
38 }
39 }
40 end
41
42 def show_operation do
43 %Operation{
44 tags: ["Emoji Packs"],
45 summary: "Show emoji pack",
46 operationId: "PleromaAPI.EmojiPackController.show",
47 parameters: [name_param()],
48 responses: %{
49 200 => Operation.response("Emoji Pack", "application/json", emoji_pack()),
50 400 => Operation.response("Bad Request", "application/json", ApiError),
51 404 => Operation.response("Not Found", "application/json", ApiError)
52 }
53 }
54 end
55
56 def archive_operation do
57 %Operation{
58 tags: ["Emoji Packs"],
59 summary: "Requests a local pack archive from the instance",
60 operationId: "PleromaAPI.EmojiPackController.archive",
61 parameters: [name_param()],
62 responses: %{
63 200 =>
64 Operation.response("Archive file", "application/octet-stream", %Schema{
65 type: :string,
66 format: :binary
67 }),
68 403 => Operation.response("Forbidden", "application/json", ApiError),
69 404 => Operation.response("Not Found", "application/json", ApiError)
70 }
71 }
72 end
73
74 def download_operation do
75 %Operation{
76 tags: ["Emoji Packs"],
77 summary: "Download pack from another instance",
78 operationId: "PleromaAPI.EmojiPackController.download",
79 security: [%{"oAuth" => ["write"]}],
80 requestBody: request_body("Parameters", download_request(), required: true),
81 responses: %{
82 200 => ok_response(),
83 500 => Operation.response("Error", "application/json", ApiError)
84 }
85 }
86 end
87
88 defp download_request do
89 %Schema{
90 type: :object,
91 required: [:url, :name],
92 properties: %{
93 url: %Schema{
94 type: :string,
95 format: :uri,
96 description: "URL of the instance to download from"
97 },
98 name: %Schema{type: :string, format: :uri, description: "Pack Name"},
99 as: %Schema{type: :string, format: :uri, description: "Save as"}
100 }
101 }
102 end
103
104 def create_operation do
105 %Operation{
106 tags: ["Emoji Packs"],
107 summary: "Create an empty pack",
108 operationId: "PleromaAPI.EmojiPackController.create",
109 security: [%{"oAuth" => ["write"]}],
110 parameters: [name_param()],
111 responses: %{
112 200 => ok_response(),
113 400 => Operation.response("Not Found", "application/json", ApiError),
114 409 => Operation.response("Conflict", "application/json", ApiError),
115 500 => Operation.response("Error", "application/json", ApiError)
116 }
117 }
118 end
119
120 def delete_operation do
121 %Operation{
122 tags: ["Emoji Packs"],
123 summary: "Delete a custom emoji pack",
124 operationId: "PleromaAPI.EmojiPackController.delete",
125 security: [%{"oAuth" => ["write"]}],
126 parameters: [name_param()],
127 responses: %{
128 200 => ok_response(),
129 400 => Operation.response("Bad Request", "application/json", ApiError),
130 404 => Operation.response("Not Found", "application/json", ApiError)
131 }
132 }
133 end
134
135 def update_operation do
136 %Operation{
137 tags: ["Emoji Packs"],
138 summary: "Updates (replaces) pack metadata",
139 operationId: "PleromaAPI.EmojiPackController.update",
140 security: [%{"oAuth" => ["write"]}],
141 requestBody: request_body("Parameters", update_request(), required: true),
142 parameters: [name_param()],
143 responses: %{
144 200 => Operation.response("Metadata", "application/json", metadata()),
145 400 => Operation.response("Bad Request", "application/json", ApiError)
146 }
147 }
148 end
149
150 def add_file_operation do
151 %Operation{
152 tags: ["Emoji Packs"],
153 summary: "Add new file to the pack",
154 operationId: "PleromaAPI.EmojiPackController.add_file",
155 security: [%{"oAuth" => ["write"]}],
156 requestBody: request_body("Parameters", add_file_request(), required: true),
157 parameters: [name_param()],
158 responses: %{
159 200 => Operation.response("Files Object", "application/json", files_object()),
160 400 => Operation.response("Bad Request", "application/json", ApiError),
161 409 => Operation.response("Conflict", "application/json", ApiError)
162 }
163 }
164 end
165
166 defp add_file_request do
167 %Schema{
168 type: :object,
169 required: [:file],
170 properties: %{
171 file: %Schema{
172 description:
173 "File needs to be uploaded with the multipart request or link to remote file",
174 anyOf: [
175 %Schema{type: :string, format: :binary},
176 %Schema{type: :string, format: :uri}
177 ]
178 },
179 shortcode: %Schema{
180 type: :string,
181 description:
182 "Shortcode for new emoji, must be unique for all emoji. If not sended, shortcode will be taken from original filename."
183 },
184 filename: %Schema{
185 type: :string,
186 description:
187 "New emoji file name. If not specified will be taken from original filename."
188 }
189 }
190 }
191 end
192
193 def update_file_operation do
194 %Operation{
195 tags: ["Emoji Packs"],
196 summary: "Add new file to the pack",
197 operationId: "PleromaAPI.EmojiPackController.update_file",
198 security: [%{"oAuth" => ["write"]}],
199 requestBody: request_body("Parameters", update_file_request(), required: true),
200 parameters: [name_param()],
201 responses: %{
202 200 => Operation.response("Files Object", "application/json", files_object()),
203 400 => Operation.response("Bad Request", "application/json", ApiError),
204 409 => Operation.response("Conflict", "application/json", ApiError)
205 }
206 }
207 end
208
209 defp update_file_request do
210 %Schema{
211 type: :object,
212 required: [:shortcode, :new_shortcode, :new_filename],
213 properties: %{
214 shortcode: %Schema{
215 type: :string,
216 description: "Emoji file shortcode"
217 },
218 new_shortcode: %Schema{
219 type: :string,
220 description: "New emoji file shortcode"
221 },
222 new_filename: %Schema{
223 type: :string,
224 description: "New filename for emoji file"
225 },
226 force: %Schema{
227 type: :boolean,
228 description: "With true value to overwrite existing emoji with new shortcode",
229 default: false
230 }
231 }
232 }
233 end
234
235 def delete_file_operation do
236 %Operation{
237 tags: ["Emoji Packs"],
238 summary: "Delete emoji file from pack",
239 operationId: "PleromaAPI.EmojiPackController.delete_file",
240 security: [%{"oAuth" => ["write"]}],
241 parameters: [
242 name_param(),
243 Operation.parameter(:shortcode, :query, :string, "File shortcode",
244 example: "cofe",
245 required: true
246 )
247 ],
248 responses: %{
249 200 => Operation.response("Files Object", "application/json", files_object()),
250 400 => Operation.response("Bad Request", "application/json", ApiError)
251 }
252 }
253 end
254
255 def import_from_filesystem_operation do
256 %Operation{
257 tags: ["Emoji Packs"],
258 summary: "Imports packs from filesystem",
259 operationId: "PleromaAPI.EmojiPackController.import",
260 security: [%{"oAuth" => ["write"]}],
261 responses: %{
262 200 =>
263 Operation.response("Array of imported pack names", "application/json", %Schema{
264 type: :array,
265 items: %Schema{type: :string}
266 })
267 }
268 }
269 end
270
271 defp name_param do
272 Operation.parameter(:name, :path, :string, "Pack Name", example: "cofe", required: true)
273 end
274
275 defp url_param do
276 Operation.parameter(
277 :url,
278 :query,
279 %Schema{type: :string, format: :uri},
280 "URL of the instance",
281 required: true
282 )
283 end
284
285 defp ok_response do
286 Operation.response("Ok", "application/json", %Schema{type: :string, example: "ok"})
287 end
288
289 defp emoji_packs_response do
290 Operation.response(
291 "Object with pack names as keys and pack contents as values",
292 "application/json",
293 %Schema{
294 type: :object,
295 additionalProperties: emoji_pack(),
296 example: %{
297 "emojos" => emoji_pack().example
298 }
299 }
300 )
301 end
302
303 defp emoji_pack do
304 %Schema{
305 title: "EmojiPack",
306 type: :object,
307 properties: %{
308 files: files_object(),
309 pack: %Schema{
310 type: :object,
311 properties: %{
312 license: %Schema{type: :string},
313 homepage: %Schema{type: :string, format: :uri},
314 description: %Schema{type: :string},
315 "can-download": %Schema{type: :boolean},
316 "share-files": %Schema{type: :boolean},
317 "download-sha256": %Schema{type: :string}
318 }
319 }
320 },
321 example: %{
322 "files" => %{"emacs" => "emacs.png", "guix" => "guix.png"},
323 "pack" => %{
324 "license" => "Test license",
325 "homepage" => "https://pleroma.social",
326 "description" => "Test description",
327 "can-download" => true,
328 "share-files" => true,
329 "download-sha256" => "57482F30674FD3DE821FF48C81C00DA4D4AF1F300209253684ABA7075E5FC238"
330 }
331 }
332 }
333 end
334
335 defp files_object do
336 %Schema{
337 type: :object,
338 additionalProperties: %Schema{type: :string},
339 description: "Object with emoji names as keys and filenames as values"
340 }
341 end
342
343 defp update_request do
344 %Schema{
345 type: :object,
346 properties: %{
347 metadata: %Schema{
348 type: :object,
349 description: "Metadata to replace the old one",
350 properties: %{
351 license: %Schema{type: :string},
352 homepage: %Schema{type: :string, format: :uri},
353 description: %Schema{type: :string},
354 "fallback-src": %Schema{
355 type: :string,
356 format: :uri,
357 description: "Fallback url to download pack from"
358 },
359 "fallback-src-sha256": %Schema{
360 type: :string,
361 description: "SHA256 encoded for fallback pack archive"
362 },
363 "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
364 }
365 }
366 }
367 }
368 end
369
370 defp metadata do
371 %Schema{
372 type: :object,
373 properties: %{
374 license: %Schema{type: :string},
375 homepage: %Schema{type: :string, format: :uri},
376 description: %Schema{type: :string},
377 "fallback-src": %Schema{
378 type: :string,
379 format: :uri,
380 description: "Fallback url to download pack from"
381 },
382 "fallback-src-sha256": %Schema{
383 type: :string,
384 description: "SHA256 encoded for fallback pack archive"
385 },
386 "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
387 }
388 }
389 end
390 end