fix(backend): use to-be-set custom preferences when validating fields, remove constants
This commit is contained in:
parent
6dd3478ff9
commit
5594463a09
4 changed files with 47 additions and 52 deletions
|
@ -7,15 +7,6 @@ import (
|
|||
|
||||
type WordStatus string
|
||||
|
||||
const (
|
||||
StatusUnknown WordStatus = ""
|
||||
StatusFavourite WordStatus = "favourite"
|
||||
StatusOkay WordStatus = "okay"
|
||||
StatusJokingly WordStatus = "jokingly"
|
||||
StatusFriendsOnly WordStatus = "friends_only"
|
||||
StatusAvoid WordStatus = "avoid"
|
||||
)
|
||||
|
||||
func (w *WordStatus) UnmarshalJSON(src []byte) error {
|
||||
if string(src) == "null" {
|
||||
return nil
|
||||
|
@ -41,7 +32,7 @@ func (w *WordStatus) UnmarshalJSON(src []byte) error {
|
|||
}
|
||||
|
||||
func (w WordStatus) Valid(extra CustomPreferences) bool {
|
||||
if w == StatusFavourite || w == StatusOkay || w == StatusJokingly || w == StatusFriendsOnly || w == StatusAvoid {
|
||||
if w == "favourite" || w == "okay" || w == "jokingly" || w == "friends_only" || w == "avoid" {
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -144,7 +144,7 @@ func (bot *Bot) userPronouns(w http.ResponseWriter, r *http.Request, ev *discord
|
|||
var favs []db.FieldEntry
|
||||
|
||||
for _, e := range field.Entries {
|
||||
if e.Status == db.StatusFavourite {
|
||||
if e.Status == "favourite" {
|
||||
favs = append(favs, e)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,18 +106,6 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
|||
}
|
||||
}
|
||||
|
||||
if err := validateSlicePtr("name", req.Names, u.CustomPreferences); err != nil {
|
||||
return *err
|
||||
}
|
||||
|
||||
if err := validateSlicePtr("pronoun", req.Pronouns, u.CustomPreferences); err != nil {
|
||||
return *err
|
||||
}
|
||||
|
||||
if err := validateSlicePtr("field", req.Fields, u.CustomPreferences); err != nil {
|
||||
return *err
|
||||
}
|
||||
|
||||
// validate custom preferences
|
||||
if req.CustomPreferences != nil {
|
||||
if count := len(*req.CustomPreferences); count > db.MaxFields {
|
||||
|
@ -134,6 +122,22 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
|||
}
|
||||
}
|
||||
}
|
||||
customPreferences := u.CustomPreferences
|
||||
if req.CustomPreferences != nil {
|
||||
customPreferences = *req.CustomPreferences
|
||||
}
|
||||
|
||||
if err := validateSlicePtr("name", req.Names, customPreferences); err != nil {
|
||||
return *err
|
||||
}
|
||||
|
||||
if err := validateSlicePtr("pronoun", req.Pronouns, customPreferences); err != nil {
|
||||
return *err
|
||||
}
|
||||
|
||||
if err := validateSlicePtr("field", req.Fields, customPreferences); err != nil {
|
||||
return *err
|
||||
}
|
||||
|
||||
// update avatar
|
||||
var avatarHash *string = nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue