Merge branch 'issue/2256' into 'develop'
[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 alias Pleroma.Web.ApiSpec.Schemas.Attachment
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: ["media"],
20 summary: "Upload media as attachment",
21 description: "Creates an attachment to be used with a new status.",
22 operationId: "MediaController.create",
23 security: [%{"oAuth" => ["write:media"]}],
24 requestBody: Helpers.request_body("Parameters", create_request()),
25 responses: %{
26 200 => Operation.response("Media", "application/json", 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 required: [:file],
39 properties: %{
40 file: %Schema{
41 type: :string,
42 format: :binary,
43 description: "The file to be attached, using multipart form data."
44 },
45 description: %Schema{
46 type: :string,
47 description: "A plain-text description of the media, for accessibility purposes."
48 },
49 focus: %Schema{
50 type: :string,
51 description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
52 }
53 }
54 }
55 end
56
57 def update_operation do
58 %Operation{
59 tags: ["media"],
60 summary: "Upload media as attachment",
61 description: "Creates an attachment to be used with a new status.",
62 operationId: "MediaController.update",
63 security: [%{"oAuth" => ["write:media"]}],
64 parameters: [id_param()],
65 requestBody: Helpers.request_body("Parameters", update_request()),
66 responses: %{
67 200 => Operation.response("Media", "application/json", Attachment),
68 400 => Operation.response("Media", "application/json", ApiError),
69 401 => Operation.response("Media", "application/json", ApiError),
70 422 => Operation.response("Media", "application/json", ApiError)
71 }
72 }
73 end
74
75 defp update_request do
76 %Schema{
77 title: "MediaUpdateRequest",
78 description: "POST body for updating an attachment",
79 type: :object,
80 properties: %{
81 file: %Schema{
82 type: :string,
83 format: :binary,
84 description: "The file to be attached, using multipart form data."
85 },
86 description: %Schema{
87 type: :string,
88 description: "A plain-text description of the media, for accessibility purposes."
89 },
90 focus: %Schema{
91 type: :string,
92 description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
93 }
94 }
95 }
96 end
97
98 def show_operation do
99 %Operation{
100 tags: ["media"],
101 summary: "Show Uploaded media attachment",
102 operationId: "MediaController.show",
103 parameters: [id_param()],
104 security: [%{"oAuth" => ["read:media"]}],
105 responses: %{
106 200 => Operation.response("Media", "application/json", Attachment),
107 401 => Operation.response("Media", "application/json", ApiError),
108 422 => Operation.response("Media", "application/json", ApiError)
109 }
110 }
111 end
112
113 def create2_operation do
114 %Operation{
115 tags: ["media"],
116 summary: "Upload media as attachment",
117 description: "Creates an attachment to be used with a new status.",
118 operationId: "MediaController.create2",
119 security: [%{"oAuth" => ["write:media"]}],
120 requestBody: Helpers.request_body("Parameters", create_request()),
121 responses: %{
122 202 => Operation.response("Media", "application/json", Attachment),
123 422 => Operation.response("Media", "application/json", ApiError),
124 500 => Operation.response("Media", "application/json", ApiError)
125 }
126 }
127 end
128
129 defp id_param do
130 Operation.parameter(:id, :path, :string, "The ID of the Attachment entity")
131 end
132 end