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