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