Add OpenAPISpex for MediaController
[akkoma] / lib / pleroma / web / api_spec / operations / media_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.MediaOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8 alias Pleroma.Web.ApiSpec.Helpers
9 alias Pleroma.Web.ApiSpec.Schemas.ApiError
10
11 def open_api_operation(action) do
12 operation = String.to_existing_atom("#{action}_operation")
13 apply(__MODULE__, operation, [])
14 end
15
16 def create_operation do
17 %Operation{
18 tags: ["media"],
19 summary: "Upload media as attachment",
20 description: "Creates an attachment to be used with a new status.",
21 operationId: "MediaController.create",
22 security: [%{"oAuth" => ["write:media"]}],
23 requestBody: Helpers.request_body("Parameters", create_request()),
24 responses: %{
25 200 =>
26 Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
27 401 => Operation.response("Media", "application/json", ApiError),
28 422 => Operation.response("Media", "application/json", ApiError)
29 }
30 }
31 end
32
33 defp create_request() do
34 %Schema{
35 title: "MediaCreateRequest",
36 description: "POST body for creating an attachment",
37 type: :object,
38 properties: %{
39 file: %Schema{
40 type: :binary,
41 description: "The file to be attached, using multipart form data.",
42 required: true
43 },
44 description: %Schema{
45 type: :string,
46 description: "A plain-text description of the media, for accessibility purposes."
47 },
48 focus: %Schema{
49 type: :string,
50 description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
51 }
52 }
53 }
54 end
55
56 def update_operation do
57 %Operation{
58 tags: ["media"],
59 summary: "Upload media as attachment",
60 description: "Creates an attachment to be used with a new status.",
61 operationId: "MediaController.update",
62 security: [%{"oAuth" => ["write:media"]}],
63 requestBody: Helpers.request_body("Parameters", update_request()),
64 responses: %{
65 200 =>
66 Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
67 401 => Operation.response("Media", "application/json", ApiError),
68 422 => Operation.response("Media", "application/json", ApiError)
69 }
70 }
71 end
72
73 defp update_request() do
74 %Schema{
75 title: "MediaCreateRequest",
76 description: "POST body for creating an attachment",
77 type: :object,
78 properties: %{
79 id: %Schema{
80 type: :string,
81 description: "The id of the Attachment entity to be updated",
82 required: true
83 },
84 file: %Schema{
85 type: :binary,
86 description: "The file to be attached, using multipart form data."
87 },
88 description: %Schema{
89 type: :string,
90 description: "A plain-text description of the media, for accessibility purposes."
91 },
92 focus: %Schema{
93 type: :string,
94 description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
95 }
96 }
97 }
98 end
99
100 def show_operation do
101 %Operation{
102 tags: ["media"],
103 summary: "Show Uploaded media attachment",
104 operationId: "MediaController.show",
105 security: [%{"oAuth" => ["read:media"]}],
106 responses: %{
107 200 =>
108 Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
109 401 => Operation.response("Media", "application/json", ApiError),
110 422 => Operation.response("Media", "application/json", ApiError)
111 }
112 }
113 end
114
115 def create2_operation do
116 %Operation{
117 tags: ["media"],
118 summary: "Upload media as attachment",
119 description: "Creates an attachment to be used with a new status.",
120 operationId: "MediaController.create2",
121 security: [%{"oAuth" => ["write:media"]}],
122 requestBody: Helpers.request_body("Parameters", create_request()),
123 responses: %{
124 202 =>
125 Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
126 422 => Operation.response("Media", "application/json", ApiError),
127 500 => Operation.response("Media", "application/json", ApiError)
128 }
129 }
130 end
131 end