summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/api_spec/operations/admin/invite_operation.ex
blob: 704f082bafa0dba71f5c37028d8058e77593abb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ApiSpec.Admin.InviteOperation do
  alias OpenApiSpex.Operation
  alias OpenApiSpex.Schema
  alias Pleroma.Web.ApiSpec.Schemas.ApiError

  import Pleroma.Web.ApiSpec.Helpers

  def open_api_operation(action) do
    operation = String.to_existing_atom("#{action}_operation")
    apply(__MODULE__, operation, [])
  end

  def index_operation do
    %Operation{
      tags: ["Invites"],
      summary: "Get a list of generated invites",
      operationId: "AdminAPI.InviteController.index",
      security: [%{"oAuth" => ["admin:read:invites"]}],
      parameters: admin_api_params(),
      responses: %{
        200 =>
          Operation.response("Invites", "application/json", %Schema{
            type: :object,
            properties: %{
              invites: %Schema{type: :array, items: invite()}
            },
            example: %{
              "invites" => [
                %{
                  "id" => 123,
                  "token" => "kSQtDj_GNy2NZsL9AQDFIsHN5qdbguB6qRg3WHw6K1U=",
                  "used" => true,
                  "expires_at" => nil,
                  "uses" => 0,
                  "max_use" => nil,
                  "invite_type" => "one_time"
                }
              ]
            }
          })
      }
    }
  end

  def create_operation do
    %Operation{
      tags: ["Invites"],
      summary: "Create an account registration invite token",
      operationId: "AdminAPI.InviteController.create",
      security: [%{"oAuth" => ["admin:write:invites"]}],
      parameters: admin_api_params(),
      requestBody:
        request_body("Parameters", %Schema{
          type: :object,
          properties: %{
            max_use: %Schema{type: :integer},
            expires_at: %Schema{type: :string, format: :date, example: "2020-04-20"}
          }
        }),
      responses: %{
        200 => Operation.response("Invite", "application/json", invite())
      }
    }
  end

  def revoke_operation do
    %Operation{
      tags: ["Invites"],
      summary: "Revoke invite by token",
      operationId: "AdminAPI.InviteController.revoke",
      security: [%{"oAuth" => ["admin:write:invites"]}],
      parameters: admin_api_params(),
      requestBody:
        request_body(
          "Parameters",
          %Schema{
            type: :object,
            required: [:token],
            properties: %{
              token: %Schema{type: :string}
            }
          },
          required: true
        ),
      responses: %{
        200 => Operation.response("Invite", "application/json", invite()),
        400 => Operation.response("Bad Request", "application/json", ApiError),
        404 => Operation.response("Not Found", "application/json", ApiError)
      }
    }
  end

  def email_operation do
    %Operation{
      tags: ["Invites"],
      summary: "Sends registration invite via email",
      operationId: "AdminAPI.InviteController.email",
      security: [%{"oAuth" => ["admin:write:invites"]}],
      parameters: admin_api_params(),
      requestBody:
        request_body(
          "Parameters",
          %Schema{
            type: :object,
            required: [:email],
            properties: %{
              email: %Schema{type: :string, format: :email},
              name: %Schema{type: :string}
            }
          },
          required: true
        ),
      responses: %{
        204 => no_content_response(),
        400 => Operation.response("Bad Request", "application/json", ApiError),
        403 => Operation.response("Forbidden", "application/json", ApiError)
      }
    }
  end

  defp invite do
    %Schema{
      title: "Invite",
      type: :object,
      properties: %{
        id: %Schema{type: :integer},
        token: %Schema{type: :string},
        used: %Schema{type: :boolean},
        expires_at: %Schema{type: :string, format: :date, nullable: true},
        uses: %Schema{type: :integer},
        max_use: %Schema{type: :integer, nullable: true},
        invite_type: %Schema{
          type: :string,
          enum: ["one_time", "reusable", "date_limited", "reusable_date_limited"]
        }
      },
      example: %{
        "id" => 123,
        "token" => "kSQtDj_GNy2NZsL9AQDFIsHN5qdbguB6qRg3WHw6K1U=",
        "used" => true,
        "expires_at" => nil,
        "uses" => 0,
        "max_use" => nil,
        "invite_type" => "one_time"
      }
    }
  end
end