First
[anni] / lib / pleroma / web / api_spec / operations / admin / invite_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.InviteOperation 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 index_operation do
18     %Operation{
19       tags: ["Invites"],
20       summary: "Get a list of generated invites",
21       operationId: "AdminAPI.InviteController.index",
22       security: [%{"oAuth" => ["admin:read:invites"]}],
23       parameters: admin_api_params(),
24       responses: %{
25         200 =>
26           Operation.response("Invites", "application/json", %Schema{
27             type: :object,
28             properties: %{
29               invites: %Schema{type: :array, items: invite()}
30             },
31             example: %{
32               "invites" => [
33                 %{
34                   "id" => 123,
35                   "token" => "kSQtDj_GNy2NZsL9AQDFIsHN5qdbguB6qRg3WHw6K1U=",
36                   "used" => true,
37                   "expires_at" => nil,
38                   "uses" => 0,
39                   "max_use" => nil,
40                   "invite_type" => "one_time"
41                 }
42               ]
43             }
44           })
45       }
46     }
47   end
48
49   def create_operation do
50     %Operation{
51       tags: ["Invites"],
52       summary: "Create an account registration invite token",
53       operationId: "AdminAPI.InviteController.create",
54       security: [%{"oAuth" => ["admin:write:invites"]}],
55       parameters: admin_api_params(),
56       requestBody:
57         request_body("Parameters", %Schema{
58           type: :object,
59           properties: %{
60             max_use: %Schema{type: :integer},
61             expires_at: %Schema{type: :string, format: :date, example: "2020-04-20"}
62           }
63         }),
64       responses: %{
65         200 => Operation.response("Invite", "application/json", invite())
66       }
67     }
68   end
69
70   def revoke_operation do
71     %Operation{
72       tags: ["Invites"],
73       summary: "Revoke invite by token",
74       operationId: "AdminAPI.InviteController.revoke",
75       security: [%{"oAuth" => ["admin:write:invites"]}],
76       parameters: admin_api_params(),
77       requestBody:
78         request_body(
79           "Parameters",
80           %Schema{
81             type: :object,
82             required: [:token],
83             properties: %{
84               token: %Schema{type: :string}
85             }
86           },
87           required: true
88         ),
89       responses: %{
90         200 => Operation.response("Invite", "application/json", invite()),
91         400 => Operation.response("Bad Request", "application/json", ApiError),
92         404 => Operation.response("Not Found", "application/json", ApiError)
93       }
94     }
95   end
96
97   def email_operation do
98     %Operation{
99       tags: ["Invites"],
100       summary: "Sends registration invite via email",
101       operationId: "AdminAPI.InviteController.email",
102       security: [%{"oAuth" => ["admin:write:invites"]}],
103       parameters: admin_api_params(),
104       requestBody:
105         request_body(
106           "Parameters",
107           %Schema{
108             type: :object,
109             required: [:email],
110             properties: %{
111               email: %Schema{type: :string, format: :email},
112               name: %Schema{type: :string}
113             }
114           },
115           required: true
116         ),
117       responses: %{
118         204 => no_content_response(),
119         400 => Operation.response("Bad Request", "application/json", ApiError),
120         403 => Operation.response("Forbidden", "application/json", ApiError)
121       }
122     }
123   end
124
125   defp invite do
126     %Schema{
127       title: "Invite",
128       type: :object,
129       properties: %{
130         id: %Schema{type: :integer},
131         token: %Schema{type: :string},
132         used: %Schema{type: :boolean},
133         expires_at: %Schema{type: :string, format: :date, nullable: true},
134         uses: %Schema{type: :integer},
135         max_use: %Schema{type: :integer, nullable: true},
136         invite_type: %Schema{
137           type: :string,
138           enum: ["one_time", "reusable", "date_limited", "reusable_date_limited"]
139         }
140       },
141       example: %{
142         "id" => 123,
143         "token" => "kSQtDj_GNy2NZsL9AQDFIsHN5qdbguB6qRg3WHw6K1U=",
144         "used" => true,
145         "expires_at" => nil,
146         "uses" => 0,
147         "max_use" => nil,
148         "invite_type" => "one_time"
149       }
150     }
151   end
152 end