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