this repo has no description
1package server 2 3import ( 4 "github.com/Azure/go-autorest/autorest/to" 5 "github.com/google/uuid" 6 "github.com/haileyok/cocoon/internal/helpers" 7 "github.com/haileyok/cocoon/models" 8 "github.com/labstack/echo/v4" 9) 10 11type ComAtprotoServerCreateInviteCodesRequest struct { 12 CodeCount *int `json:"codeCount,omitempty"` 13 UseCount int `json:"useCount" validate:"required"` 14 ForAccounts *[]string `json:"forAccounts,omitempty"` 15} 16 17type ComAtprotoServerCreateInviteCodesResponse []ComAtprotoServerCreateInviteCodesItem 18 19type ComAtprotoServerCreateInviteCodesItem struct { 20 Account string `json:"account"` 21 Codes []string `json:"codes"` 22} 23 24func (s *Server) handleCreateInviteCodes(e echo.Context) error { 25 ctx := e.Request().Context() 26 27 var req ComAtprotoServerCreateInviteCodesRequest 28 if err := e.Bind(&req); err != nil { 29 s.logger.Error("error binding", "error", err) 30 return helpers.ServerError(e, nil) 31 } 32 33 if err := e.Validate(req); err != nil { 34 s.logger.Error("error validating", "error", err) 35 return helpers.InputError(e, nil) 36 } 37 38 if req.CodeCount == nil { 39 req.CodeCount = to.IntPtr(1) 40 } 41 42 if req.ForAccounts == nil { 43 req.ForAccounts = to.StringSlicePtr([]string{"admin"}) 44 } 45 46 var codes []ComAtprotoServerCreateInviteCodesItem 47 48 for _, did := range *req.ForAccounts { 49 var ics []string 50 51 for range *req.CodeCount { 52 ic := uuid.NewString() 53 ics = append(ics, ic) 54 55 if err := s.db.Create(ctx, &models.InviteCode{ 56 Code: ic, 57 Did: did, 58 RemainingUseCount: req.UseCount, 59 }, nil).Error; err != nil { 60 s.logger.Error("error creating invite code", "error", err) 61 return helpers.ServerError(e, nil) 62 } 63 } 64 65 codes = append(codes, ComAtprotoServerCreateInviteCodesItem{ 66 Account: did, 67 Codes: ics, 68 }) 69 } 70 71 return e.JSON(200, codes) 72}