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