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