2022-09-20 03:55:00 -07:00
|
|
|
package member
|
|
|
|
|
|
|
|
import (
|
2022-10-03 01:59:30 -07:00
|
|
|
"fmt"
|
2022-09-20 03:55:00 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/db"
|
2022-10-03 01:59:30 -07:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/log"
|
2022-09-20 03:55:00 -07:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/server"
|
2022-10-03 01:59:30 -07:00
|
|
|
"emperror.dev/errors"
|
2022-09-20 03:55:00 -07:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateMemberRequest struct {
|
2022-11-20 12:09:29 -08:00
|
|
|
Name string `json:"name"`
|
|
|
|
DisplayName *string `json:"display_name"`
|
|
|
|
Bio string `json:"bio"`
|
|
|
|
Avatar string `json:"avatar"`
|
|
|
|
Links []string `json:"links"`
|
|
|
|
Names []db.Name `json:"names"`
|
|
|
|
Pronouns []db.Pronoun `json:"pronouns"`
|
|
|
|
Fields []db.Field `json:"fields"`
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) createMember(w http.ResponseWriter, r *http.Request) (err error) {
|
|
|
|
ctx := r.Context()
|
2022-10-03 01:59:30 -07:00
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
|
|
|
u, err := s.DB.User(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting user")
|
|
|
|
}
|
|
|
|
|
|
|
|
memberCount, err := s.DB.MemberCount(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting member count")
|
|
|
|
}
|
|
|
|
if memberCount > db.MaxMemberCount {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrMemberLimitReached,
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
|
|
|
|
var cmr CreateMemberRequest
|
|
|
|
err = render.Decode(r, &cmr)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(server.APIError); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return server.APIError{Code: server.ErrBadRequest}
|
|
|
|
}
|
|
|
|
|
2022-10-03 01:59:30 -07:00
|
|
|
// validate everything
|
|
|
|
if cmr.Name == "" {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
2022-11-20 12:09:29 -08:00
|
|
|
Details: "Name may not be empty",
|
|
|
|
}
|
|
|
|
} else if len(cmr.Name) > 100 {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: "Name may not be longer than 100 characters",
|
2022-10-03 01:59:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSlicePtr("name", &cmr.Names); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSlicePtr("pronoun", &cmr.Pronouns); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSlicePtr("field", &cmr.Fields); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := s.DB.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "starting transaction")
|
|
|
|
}
|
|
|
|
defer tx.Rollback(ctx)
|
|
|
|
|
2022-11-20 12:09:29 -08:00
|
|
|
m, err := s.DB.CreateMember(ctx, tx, claims.UserID, cmr.Name, cmr.DisplayName, cmr.Bio, cmr.Links)
|
2022-10-03 01:59:30 -07:00
|
|
|
if err != nil {
|
2022-11-20 12:09:29 -08:00
|
|
|
if errors.Cause(err) == db.ErrMemberNameInUse {
|
|
|
|
return server.APIError{Code: server.ErrMemberNameInUse}
|
|
|
|
}
|
|
|
|
|
2022-10-03 01:59:30 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set names, pronouns, fields
|
2022-11-20 12:09:29 -08:00
|
|
|
err = s.DB.SetMemberNames(ctx, tx, m.ID, cmr.Names)
|
2022-10-03 01:59:30 -07:00
|
|
|
if err != nil {
|
2022-11-20 12:09:29 -08:00
|
|
|
log.Errorf("setting names for member %v: %v", m.ID, err)
|
2022-10-03 01:59:30 -07:00
|
|
|
return err
|
|
|
|
}
|
2022-11-20 12:09:29 -08:00
|
|
|
err = s.DB.SetMemberPronouns(ctx, tx, m.ID, cmr.Pronouns)
|
2022-10-03 01:59:30 -07:00
|
|
|
if err != nil {
|
2022-11-20 12:09:29 -08:00
|
|
|
log.Errorf("setting pronouns for member %v: %v", m.ID, err)
|
2022-10-03 01:59:30 -07:00
|
|
|
return err
|
|
|
|
}
|
2022-11-20 12:09:29 -08:00
|
|
|
err = s.DB.SetMemberFields(ctx, tx, m.ID, cmr.Fields)
|
2022-10-03 01:59:30 -07:00
|
|
|
if err != nil {
|
2022-11-20 12:09:29 -08:00
|
|
|
log.Errorf("setting fields for member %v: %v", m.ID, err)
|
2022-10-03 01:59:30 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmr.Avatar != "" {
|
|
|
|
webp, jpg, err := s.DB.ConvertAvatar(cmr.Avatar)
|
|
|
|
if err != nil {
|
|
|
|
if err == db.ErrInvalidDataURI {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: "invalid avatar data URI",
|
|
|
|
}
|
|
|
|
} else if err == db.ErrInvalidContentType {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: "invalid avatar content type",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("converting member avatar: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
webpURL, jpgURL, err := s.DB.WriteMemberAvatar(ctx, m.ID, webp, jpg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("uploading member avatar: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.QueryRow(ctx, "UPDATE members SET avatar_urls = $1 WHERE id = $2", []string{webpURL, jpgURL}, m.ID).Scan(&m.AvatarURLs)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "setting avatar urls in db")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "committing transaction")
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, dbMemberToMember(u, m, cmr.Names, cmr.Pronouns, cmr.Fields))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type validator interface {
|
|
|
|
Validate() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateSlicePtr validates a slice of validators.
|
|
|
|
// If the slice is nil, a nil error is returned (assuming that the field is not required)
|
|
|
|
func validateSlicePtr[T validator](typ string, slice *[]T) *server.APIError {
|
|
|
|
if slice == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
max := db.MaxFields
|
|
|
|
if typ != "field" {
|
|
|
|
max = db.FieldEntriesLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
// max 25 fields
|
|
|
|
if len(*slice) > max {
|
|
|
|
return &server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: fmt.Sprintf("Too many %ss (max %d, current %d)", typ, max, len(*slice)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate all fields
|
|
|
|
for i, pronouns := range *slice {
|
|
|
|
if s := pronouns.Validate(); s != "" {
|
|
|
|
return &server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: fmt.Sprintf("%s %d: %s", typ, i, s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|