Merge branch 'libmagic' into 'develop'
[akkoma] / lib / pleroma / web / api_spec / operations / pleroma_emoji_file_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.PleromaEmojiFileOperation 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 create_operation do
18 %Operation{
19 tags: ["Emoji Packs"],
20 summary: "Add new file to the pack",
21 operationId: "PleromaAPI.EmojiPackController.add_file",
22 security: [%{"oAuth" => ["write"]}],
23 requestBody: request_body("Parameters", create_request(), required: true),
24 parameters: [name_param()],
25 responses: %{
26 200 => Operation.response("Files Object", "application/json", files_object()),
27 422 => Operation.response("Unprocessable Entity", "application/json", ApiError),
28 404 => Operation.response("Not Found", "application/json", ApiError),
29 400 => Operation.response("Bad Request", "application/json", ApiError),
30 409 => Operation.response("Conflict", "application/json", ApiError)
31 }
32 }
33 end
34
35 defp create_request do
36 %Schema{
37 type: :object,
38 required: [:file],
39 properties: %{
40 file: %Schema{
41 description:
42 "File needs to be uploaded with the multipart request or link to remote file",
43 anyOf: [
44 %Schema{type: :string, format: :binary},
45 %Schema{type: :string, format: :uri}
46 ]
47 },
48 shortcode: %Schema{
49 type: :string,
50 description:
51 "Shortcode for new emoji, must be unique for all emoji. If not sended, shortcode will be taken from original filename."
52 },
53 filename: %Schema{
54 type: :string,
55 description:
56 "New emoji file name. If not specified will be taken from original filename."
57 }
58 }
59 }
60 end
61
62 def update_operation do
63 %Operation{
64 tags: ["Emoji Packs"],
65 summary: "Add new file to the pack",
66 operationId: "PleromaAPI.EmojiPackController.update_file",
67 security: [%{"oAuth" => ["write"]}],
68 requestBody: request_body("Parameters", update_request(), required: true),
69 parameters: [name_param()],
70 responses: %{
71 200 => Operation.response("Files Object", "application/json", files_object()),
72 404 => Operation.response("Not Found", "application/json", ApiError),
73 400 => Operation.response("Bad Request", "application/json", ApiError),
74 409 => Operation.response("Conflict", "application/json", ApiError),
75 422 => Operation.response("Unprocessable Entity", "application/json", ApiError)
76 }
77 }
78 end
79
80 defp update_request do
81 %Schema{
82 type: :object,
83 required: [:shortcode, :new_shortcode, :new_filename],
84 properties: %{
85 shortcode: %Schema{
86 type: :string,
87 description: "Emoji file shortcode"
88 },
89 new_shortcode: %Schema{
90 type: :string,
91 description: "New emoji file shortcode"
92 },
93 new_filename: %Schema{
94 type: :string,
95 description: "New filename for emoji file"
96 },
97 force: %Schema{
98 type: :boolean,
99 description: "With true value to overwrite existing emoji with new shortcode",
100 default: false
101 }
102 }
103 }
104 end
105
106 def delete_operation do
107 %Operation{
108 tags: ["Emoji Packs"],
109 summary: "Delete emoji file from pack",
110 operationId: "PleromaAPI.EmojiPackController.delete_file",
111 security: [%{"oAuth" => ["write"]}],
112 parameters: [
113 name_param(),
114 Operation.parameter(:shortcode, :query, :string, "File shortcode",
115 example: "cofe",
116 required: true
117 )
118 ],
119 responses: %{
120 200 => Operation.response("Files Object", "application/json", files_object()),
121 400 => Operation.response("Bad Request", "application/json", ApiError),
122 404 => Operation.response("Not Found", "application/json", ApiError),
123 422 => Operation.response("Unprocessable Entity", "application/json", ApiError)
124 }
125 }
126 end
127
128 defp name_param do
129 Operation.parameter(:name, :query, :string, "Pack Name", example: "cofe", required: true)
130 end
131
132 defp files_object do
133 %Schema{
134 type: :object,
135 additionalProperties: %Schema{type: :string},
136 description: "Object with emoji names as keys and filenames as values"
137 }
138 end
139 end