1package server
2
3import (
4 "github.com/google/uuid"
5 "github.com/haileyok/cocoon/internal/helpers"
6 "github.com/haileyok/cocoon/models"
7 "github.com/labstack/echo/v4"
8)
9
10type ComAtprotoServerCreateInviteCodeRequest struct {
11 UseCount int `json:"useCount" validate:"required"`
12 ForAccount *string `json:"forAccount,omitempty"`
13}
14
15type ComAtprotoServerCreateInviteCodeResponse struct {
16 Code string `json:"code"`
17}
18
19func (s *Server) handleCreateInviteCode(e echo.Context) error {
20 var req ComAtprotoServerCreateInviteCodeRequest
21 if err := e.Bind(&req); err != nil {
22 s.logger.Error("error binding", "error", err)
23 return helpers.ServerError(e, nil)
24 }
25
26 if err := e.Validate(req); err != nil {
27 s.logger.Error("error validating", "error", err)
28 return helpers.InputError(e, nil)
29 }
30
31 ic := uuid.NewString()
32
33 var acc string
34 if req.ForAccount == nil {
35 acc = "admin"
36 } else {
37 acc = *req.ForAccount
38 }
39
40 if err := s.db.Create(&models.InviteCode{
41 Code: ic,
42 Did: acc,
43 RemainingUseCount: req.UseCount,
44 }, nil).Error; err != nil {
45 s.logger.Error("error creating invite code", "error", err)
46 return helpers.ServerError(e, nil)
47 }
48
49 return e.JSON(200, ComAtprotoServerCreateInviteCodeResponse{
50 Code: ic,
51 })
52}