Something went wrong. Try again.
An atproto PDS written in Go
Something went wrong. Try again.
Go
at push-based
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374package server
import ( "github.com/Azure/go-autorest/autorest/to" "github.com/google/uuid" "github.com/haileyok/cocoon/internal/helpers" "github.com/haileyok/cocoon/models" "github.com/labstack/echo/v4")
type ComAtprotoServerCreateInviteCodesRequest struct { CodeCount *int `json:"codeCount,omitempty"` UseCount int `json:"useCount" validate:"required"` ForAccounts *[]string `json:"forAccounts,omitempty"`}
type ComAtprotoServerCreateInviteCodesResponse []ComAtprotoServerCreateInviteCodesItem
type ComAtprotoServerCreateInviteCodesItem struct { Account string `json:"account"` Codes []string `json:"codes"`}
func (s *Server) handleCreateInviteCodes(e echo.Context) error { ctx := e.Request().Context() logger := s.logger.With("name", "handleServerCreateInviteCodes")
var req ComAtprotoServerCreateInviteCodesRequest if err := e.Bind(&req); err != nil { logger.Error("error binding", "error", err) return helpers.ServerError(e, nil) }
if err := e.Validate(req); err != nil { logger.Error("error validating", "error", err) return helpers.InputError(e, nil) }
if req.CodeCount == nil { req.CodeCount = to.IntPtr(1) }
if req.ForAccounts == nil { req.ForAccounts = to.StringSlicePtr([]string{"admin"}) }
codes := make([]ComAtprotoServerCreateInviteCodesItem, 0, len(*req.ForAccounts))
for _, did := range *req.ForAccounts { ics := make([]string, 0, *req.CodeCount)
for range *req.CodeCount { ic := uuid.NewString() ics = append(ics, ic)
if err := s.db.Create(ctx, &models.InviteCode{ Code: ic, Did: did, RemainingUseCount: req.UseCount, }, nil).Error; err != nil { logger.Error("error creating invite code", "error", err) return helpers.ServerError(e, nil) } }
codes = append(codes, ComAtprotoServerCreateInviteCodesItem{ Account: did, Codes: ics, }) }
return e.JSON(200, codes)}