Merge branch 'develop' into 'issue/2115'
[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 import_from_filesystem_operation do
179 %Operation{
180 tags: ["Emoji Packs"],
181 summary: "Imports packs from filesystem",
182 operationId: "PleromaAPI.EmojiPackController.import",
183 security: [%{"oAuth" => ["write"]}],
184 responses: %{
185 200 =>
186 Operation.response("Array of imported pack names", "application/json", %Schema{
187 type: :array,
188 items: %Schema{type: :string}
189 })
190 }
191 }
192 end
193
194 defp name_param do
195 Operation.parameter(:name, :path, :string, "Pack Name", example: "cofe", required: true)
196 end
197
198 defp url_param do
199 Operation.parameter(
200 :url,
201 :query,
202 %Schema{type: :string, format: :uri},
203 "URL of the instance",
204 required: true
205 )
206 end
207
208 defp ok_response do
209 Operation.response("Ok", "application/json", %Schema{type: :string, example: "ok"})
210 end
211
212 defp emoji_packs_response do
213 Operation.response(
214 "Object with pack names as keys and pack contents as values",
215 "application/json",
216 %Schema{
217 type: :object,
218 additionalProperties: emoji_pack(),
219 example: %{
220 "emojos" => emoji_pack().example
221 }
222 }
223 )
224 end
225
226 defp emoji_pack do
227 %Schema{
228 title: "EmojiPack",
229 type: :object,
230 properties: %{
231 files: files_object(),
232 pack: %Schema{
233 type: :object,
234 properties: %{
235 license: %Schema{type: :string},
236 homepage: %Schema{type: :string, format: :uri},
237 description: %Schema{type: :string},
238 "can-download": %Schema{type: :boolean},
239 "share-files": %Schema{type: :boolean},
240 "download-sha256": %Schema{type: :string}
241 }
242 }
243 },
244 example: %{
245 "files" => %{"emacs" => "emacs.png", "guix" => "guix.png"},
246 "pack" => %{
247 "license" => "Test license",
248 "homepage" => "https://pleroma.social",
249 "description" => "Test description",
250 "can-download" => true,
251 "share-files" => true,
252 "download-sha256" => "57482F30674FD3DE821FF48C81C00DA4D4AF1F300209253684ABA7075E5FC238"
253 }
254 }
255 }
256 end
257
258 defp files_object do
259 %Schema{
260 type: :object,
261 additionalProperties: %Schema{type: :string},
262 description: "Object with emoji names as keys and filenames as values"
263 }
264 end
265
266 defp update_request do
267 %Schema{
268 type: :object,
269 properties: %{
270 metadata: %Schema{
271 type: :object,
272 description: "Metadata to replace the old one",
273 properties: %{
274 license: %Schema{type: :string},
275 homepage: %Schema{type: :string, format: :uri},
276 description: %Schema{type: :string},
277 "fallback-src": %Schema{
278 type: :string,
279 format: :uri,
280 description: "Fallback url to download pack from"
281 },
282 "fallback-src-sha256": %Schema{
283 type: :string,
284 description: "SHA256 encoded for fallback pack archive"
285 },
286 "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
287 }
288 }
289 }
290 }
291 end
292
293 defp metadata do
294 %Schema{
295 type: :object,
296 properties: %{
297 license: %Schema{type: :string},
298 homepage: %Schema{type: :string, format: :uri},
299 description: %Schema{type: :string},
300 "fallback-src": %Schema{
301 type: :string,
302 format: :uri,
303 description: "Fallback url to download pack from"
304 },
305 "fallback-src-sha256": %Schema{
306 type: :string,
307 description: "SHA256 encoded for fallback pack archive"
308 },
309 "share-files": %Schema{type: :boolean, description: "Is pack allowed for sharing?"}
310 }
311 }
312 end
313 end