First
[anni] / lib / pleroma / web / api_spec / operations / admin / instance_document_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.Admin.InstanceDocumentOperation 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 show_operation do
17     %Operation{
18       tags: ["Instance documents"],
19       summary: "Retrieve an instance document",
20       operationId: "AdminAPI.InstanceDocumentController.show",
21       security: [%{"oAuth" => ["admin:read"]}],
22       parameters: [
23         Operation.parameter(:name, :path, %Schema{type: :string}, "The document name",
24           required: true
25         )
26         | Helpers.admin_api_params()
27       ],
28       responses: %{
29         200 => document_content(),
30         400 => Operation.response("Bad Request", "application/json", ApiError),
31         403 => Operation.response("Forbidden", "application/json", ApiError),
32         404 => Operation.response("Not Found", "application/json", ApiError)
33       }
34     }
35   end
36
37   def update_operation do
38     %Operation{
39       tags: ["Instance documents"],
40       summary: "Update an instance document",
41       operationId: "AdminAPI.InstanceDocumentController.update",
42       security: [%{"oAuth" => ["admin:write"]}],
43       requestBody: Helpers.request_body("Parameters", update_request()),
44       parameters: [
45         Operation.parameter(:name, :path, %Schema{type: :string}, "The document name",
46           required: true
47         )
48         | Helpers.admin_api_params()
49       ],
50       responses: %{
51         200 => Operation.response("InstanceDocument", "application/json", instance_document()),
52         400 => Operation.response("Bad Request", "application/json", ApiError),
53         403 => Operation.response("Forbidden", "application/json", ApiError),
54         404 => Operation.response("Not Found", "application/json", ApiError)
55       }
56     }
57   end
58
59   defp update_request do
60     %Schema{
61       title: "UpdateRequest",
62       description: "POST body for uploading the file",
63       type: :object,
64       required: [:file],
65       properties: %{
66         file: %Schema{
67           type: :string,
68           format: :binary,
69           description: "The file to be uploaded, using multipart form data."
70         }
71       }
72     }
73   end
74
75   def delete_operation do
76     %Operation{
77       tags: ["Instance documents"],
78       summary: "Delete an instance document",
79       operationId: "AdminAPI.InstanceDocumentController.delete",
80       security: [%{"oAuth" => ["admin:write"]}],
81       parameters: [
82         Operation.parameter(:name, :path, %Schema{type: :string}, "The document name",
83           required: true
84         )
85         | Helpers.admin_api_params()
86       ],
87       responses: %{
88         200 => Operation.response("InstanceDocument", "application/json", instance_document()),
89         400 => Operation.response("Bad Request", "application/json", ApiError),
90         403 => Operation.response("Forbidden", "application/json", ApiError),
91         404 => Operation.response("Not Found", "application/json", ApiError)
92       }
93     }
94   end
95
96   defp instance_document do
97     %Schema{
98       title: "InstanceDocument",
99       type: :object,
100       properties: %{
101         url: %Schema{type: :string}
102       },
103       example: %{
104         "url" => "https://example.com/static/terms-of-service.html"
105       }
106     }
107   end
108
109   defp document_content do
110     Operation.response("InstanceDocumentContent", "text/html", %Schema{
111       type: :string,
112       example: "<h1>Instance panel</h1>"
113     })
114   end
115 end