First
[anni] / lib / pleroma / web / api_spec / operations / pleroma_emoji_pack_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.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 pack administration"],
20       summary: "Make request to another instance for emoji packs list",
21       security: [%{"oAuth" => ["admin:write"]}],
22       parameters: [
23         url_param(),
24         Operation.parameter(
25           :page,
26           :query,
27           %Schema{type: :integer, default: 1},
28           "Page"
29         ),
30         Operation.parameter(
31           :page_size,
32           :query,
33           %Schema{type: :integer, default: 30},
34           "Number of emoji to return"
35         )
36       ],
37       operationId: "PleromaAPI.EmojiPackController.remote",
38       responses: %{
39         200 => emoji_packs_response(),
40         500 => Operation.response("Error", "application/json", ApiError)
41       }
42     }
43   end
44
45   def index_operation do
46     %Operation{
47       tags: ["Emoji packs"],
48       summary: "Lists local custom emoji packs",
49       operationId: "PleromaAPI.EmojiPackController.index",
50       parameters: [
51         Operation.parameter(
52           :page,
53           :query,
54           %Schema{type: :integer, default: 1},
55           "Page"
56         ),
57         Operation.parameter(
58           :page_size,
59           :query,
60           %Schema{type: :integer, default: 50},
61           "Number of emoji packs to return"
62         )
63       ],
64       responses: %{
65         200 => emoji_packs_response()
66       }
67     }
68   end
69
70   def show_operation do
71     %Operation{
72       tags: ["Emoji packs"],
73       summary: "Show emoji pack",
74       operationId: "PleromaAPI.EmojiPackController.show",
75       parameters: [
76         name_param(),
77         Operation.parameter(
78           :page,
79           :query,
80           %Schema{type: :integer, default: 1},
81           "Page"
82         ),
83         Operation.parameter(
84           :page_size,
85           :query,
86           %Schema{type: :integer, default: 30},
87           "Number of emoji to return"
88         )
89       ],
90       responses: %{
91         200 => Operation.response("Emoji Pack", "application/json", emoji_pack()),
92         400 => Operation.response("Bad Request", "application/json", ApiError),
93         404 => Operation.response("Not Found", "application/json", ApiError)
94       }
95     }
96   end
97
98   def archive_operation do
99     %Operation{
100       tags: ["Emoji packs"],
101       summary: "Requests a local pack archive from the instance",
102       operationId: "PleromaAPI.EmojiPackController.archive",
103       parameters: [name_param()],
104       responses: %{
105         200 =>
106           Operation.response("Archive file", "application/octet-stream", %Schema{
107             type: :string,
108             format: :binary
109           }),
110         403 => Operation.response("Forbidden", "application/json", ApiError),
111         404 => Operation.response("Not Found", "application/json", ApiError)
112       }
113     }
114   end
115
116   def download_operation do
117     %Operation{
118       tags: ["Emoji pack administration"],
119       summary: "Download pack from another instance",
120       operationId: "PleromaAPI.EmojiPackController.download",
121       security: [%{"oAuth" => ["admin:write"]}],
122       requestBody: request_body("Parameters", download_request(), required: true),
123       responses: %{
124         200 => ok_response(),
125         500 => Operation.response("Error", "application/json", ApiError)
126       }
127     }
128   end
129
130   defp download_request do
131     %Schema{
132       type: :object,
133       required: [:url, :name],
134       properties: %{
135         url: %Schema{
136           type: :string,
137           format: :uri,
138           description: "URL of the instance to download from"
139         },
140         name: %Schema{type: :string, format: :uri, description: "Pack Name"},
141         as: %Schema{type: :string, format: :uri, description: "Save as"}
142       }
143     }
144   end
145
146   def create_operation do
147     %Operation{
148       tags: ["Emoji pack administration"],
149       summary: "Create an empty pack",
150       operationId: "PleromaAPI.EmojiPackController.create",
151       security: [%{"oAuth" => ["admin:write"]}],
152       parameters: [name_param()],
153       responses: %{
154         200 => ok_response(),
155         400 => Operation.response("Not Found", "application/json", ApiError),
156         409 => Operation.response("Conflict", "application/json", ApiError),
157         500 => Operation.response("Error", "application/json", ApiError)
158       }
159     }
160   end
161
162   def delete_operation do
163     %Operation{
164       tags: ["Emoji pack administration"],
165       summary: "Delete a custom emoji pack",
166       operationId: "PleromaAPI.EmojiPackController.delete",
167       security: [%{"oAuth" => ["admin:write"]}],
168       parameters: [name_param()],
169       responses: %{
170         200 => ok_response(),
171         400 => Operation.response("Bad Request", "application/json", ApiError),
172         404 => Operation.response("Not Found", "application/json", ApiError),
173         500 => Operation.response("Error", "application/json", ApiError)
174       }
175     }
176   end
177
178   def update_operation do
179     %Operation{
180       tags: ["Emoji pack administration"],
181       summary: "Updates (replaces) pack metadata",
182       operationId: "PleromaAPI.EmojiPackController.update",
183       security: [%{"oAuth" => ["admin:write"]}],
184       requestBody: request_body("Parameters", update_request(), required: true),
185       parameters: [name_param()],
186       responses: %{
187         200 => Operation.response("Metadata", "application/json", metadata()),
188         400 => Operation.response("Bad Request", "application/json", ApiError),
189         500 => Operation.response("Error", "application/json", ApiError)
190       }
191     }
192   end
193
194   def import_from_filesystem_operation do
195     %Operation{
196       tags: ["Emoji pack administration"],
197       summary: "Imports packs from filesystem",
198       operationId: "PleromaAPI.EmojiPackController.import",
199       security: [%{"oAuth" => ["admin:write"]}],
200       responses: %{
201         200 =>
202           Operation.response("Array of imported pack names", "application/json", %Schema{
203             type: :array,
204             items: %Schema{type: :string}
205           })
206       }
207     }
208   end
209
210   defp name_param do
211     Operation.parameter(:name, :query, :string, "Pack Name", example: "cofe", required: true)
212   end
213
214   defp url_param do
215     Operation.parameter(
216       :url,
217       :query,
218       %Schema{type: :string, format: :uri},
219       "URL of the instance",
220       required: true
221     )
222   end
223
224   defp ok_response do
225     Operation.response("Ok", "application/json", %Schema{type: :string, example: "ok"})
226   end
227
228   defp emoji_packs_response do
229     Operation.response(
230       "Object with pack names as keys and pack contents as values",
231       "application/json",
232       %Schema{
233         type: :object,
234         additionalProperties: emoji_pack(),
235         example: %{
236           "emojos" => emoji_pack().example
237         }
238       }
239     )
240   end
241
242   defp emoji_pack do
243     %Schema{
244       title: "EmojiPack",
245       type: :object,
246       properties: %{
247         files: files_object(),
248         pack: %Schema{
249           type: :object,
250           properties: %{
251             license: %Schema{type: :string},
252             homepage: %Schema{type: :string, format: :uri},
253             description: %Schema{type: :string},
254             "can-download": %Schema{type: :boolean},
255             "share-files": %Schema{type: :boolean},
256             "download-sha256": %Schema{type: :string}
257           }
258         }
259       },
260       example: %{
261         "files" => %{"emacs" => "emacs.png", "guix" => "guix.png"},
262         "pack" => %{
263           "license" => "Test license",
264           "homepage" => "https://pleroma.social",
265           "description" => "Test description",
266           "can-download" => true,
267           "share-files" => true,
268           "download-sha256" => "57482F30674FD3DE821FF48C81C00DA4D4AF1F300209253684ABA7075E5FC238"
269         }
270       }
271     }
272   end
273
274   defp files_object do
275     %Schema{
276       type: :object,
277       additionalProperties: %Schema{type: :string},
278       description: "Object with emoji names as keys and filenames as values"
279     }
280   end
281
282   defp update_request do
283     %Schema{
284       type: :object,
285       properties: %{
286         metadata: %Schema{
287           type: :object,
288           description: "Metadata to replace the old one",
289           properties: %{
290             license: %Schema{type: :string},
291             homepage: %Schema{type: :string, format: :uri},
292             description: %Schema{type: :string},
293             "fallback-src": %Schema{
294               type: :string,
295               format: :uri,
296               description: "Fallback url to download pack from"
297             },
298             "fallback-src-sha256": %Schema{
299               type: :string,
300               description: "SHA256 encoded for fallback pack archive"
301             },
302             "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
303           }
304         }
305       }
306     }
307   end
308
309   defp metadata do
310     %Schema{
311       type: :object,
312       properties: %{
313         license: %Schema{type: :string},
314         homepage: %Schema{type: :string, format: :uri},
315         description: %Schema{type: :string},
316         "fallback-src": %Schema{
317           type: :string,
318           format: :uri,
319           description: "Fallback url to download pack from"
320         },
321         "fallback-src-sha256": %Schema{
322           type: :string,
323           description: "SHA256 encoded for fallback pack archive"
324         },
325         "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
326       }
327     }
328   end
329 end