fix(backend): use to-be-set custom preferences when validating fields, remove constants

This commit is contained in:
Sam 2023-04-21 16:35:13 +02:00
parent 6dd3478ff9
commit 5594463a09
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
4 changed files with 47 additions and 52 deletions

View file

@ -7,15 +7,6 @@ import (
type WordStatus string 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 { func (w *WordStatus) UnmarshalJSON(src []byte) error {
if string(src) == "null" { if string(src) == "null" {
return nil return nil
@ -41,7 +32,7 @@ func (w *WordStatus) UnmarshalJSON(src []byte) error {
} }
func (w WordStatus) Valid(extra CustomPreferences) bool { 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 return true
} }

View file

@ -144,7 +144,7 @@ func (bot *Bot) userPronouns(w http.ResponseWriter, r *http.Request, ev *discord
var favs []db.FieldEntry var favs []db.FieldEntry
for _, e := range field.Entries { for _, e := range field.Entries {
if e.Status == db.StatusFavourite { if e.Status == "favourite" {
favs = append(favs, e) favs = append(favs, e)
} }
} }

View file

@ -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 // validate custom preferences
if req.CustomPreferences != nil { if req.CustomPreferences != nil {
if count := len(*req.CustomPreferences); count > db.MaxFields { 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 // update avatar
var avatarHash *string = nil var avatarHash *string = nil

View file

@ -55,11 +55,11 @@ func run(c *cli.Context) error {
} }
err = pg.SetUserNamesPronouns(ctx, tx, u.ID, []db.FieldEntry{ err = pg.SetUserNamesPronouns(ctx, tx, u.ID, []db.FieldEntry{
{Value: "testing 1", Status: db.StatusFavourite}, {Value: "testing 1", Status: "favourite"},
{Value: "testing 2", Status: db.StatusOkay}, {Value: "testing 2", Status: "okay"},
}, []db.PronounEntry{ }, []db.PronounEntry{
{Pronouns: "it/it/its/its/itself", DisplayText: ptr("it/its"), Status: db.StatusFavourite}, {Pronouns: "it/it/its/its/itself", DisplayText: ptr("it/its"), Status: "favourite"},
{Pronouns: "they/them/their/theirs/themself", Status: db.StatusOkay}, {Pronouns: "they/them/their/theirs/themself", Status: "okay"},
}) })
if err != nil { if err != nil {
fmt.Println("error setting pronouns:", err) fmt.Println("error setting pronouns:", err)
@ -70,51 +70,51 @@ func run(c *cli.Context) error {
{ {
Name: "Field 1", Name: "Field 1",
Entries: []db.FieldEntry{ Entries: []db.FieldEntry{
{Value: "Favourite 1", Status: db.StatusFavourite}, {Value: "Favourite 1", Status: "favourite"},
{Value: "Okay 1", Status: db.StatusOkay}, {Value: "Okay 1", Status: "okay"},
{Value: "Jokingly 1", Status: db.StatusJokingly}, {Value: "Jokingly 1", Status: "jokingly"},
{Value: "Friends only 1", Status: db.StatusFriendsOnly}, {Value: "Friends only 1", Status: "friends_only"},
{Value: "Avoid 1", Status: db.StatusAvoid}, {Value: "Avoid 1", Status: "avoid"},
}, },
}, },
{ {
Name: "Field 2", Name: "Field 2",
Entries: []db.FieldEntry{ Entries: []db.FieldEntry{
{Value: "Favourite 2", Status: db.StatusFavourite}, {Value: "Favourite 2", Status: "favourite"},
{Value: "Okay 2", Status: db.StatusOkay}, {Value: "Okay 2", Status: "okay"},
{Value: "Jokingly 2", Status: db.StatusJokingly}, {Value: "Jokingly 2", Status: "jokingly"},
{Value: "Friends only 2", Status: db.StatusFriendsOnly}, {Value: "Friends only 2", Status: "friends_only"},
{Value: "Avoid 2", Status: db.StatusAvoid}, {Value: "Avoid 2", Status: "avoid"},
}, },
}, },
{ {
Name: "Field 3", Name: "Field 3",
Entries: []db.FieldEntry{ Entries: []db.FieldEntry{
{Value: "Favourite 3", Status: db.StatusFavourite}, {Value: "Favourite 3", Status: "favourite"},
{Value: "Okay 3", Status: db.StatusOkay}, {Value: "Okay 3", Status: "okay"},
{Value: "Jokingly 3", Status: db.StatusJokingly}, {Value: "Jokingly 3", Status: "jokingly"},
{Value: "Friends only 3", Status: db.StatusFriendsOnly}, {Value: "Friends only 3", Status: "friends_only"},
{Value: "Avoid 3", Status: db.StatusAvoid}, {Value: "Avoid 3", Status: "avoid"},
}, },
}, },
{ {
Name: "Field 4", Name: "Field 4",
Entries: []db.FieldEntry{ Entries: []db.FieldEntry{
{Value: "Favourite 4", Status: db.StatusFavourite}, {Value: "Favourite 4", Status: "favourite"},
{Value: "Okay 4", Status: db.StatusOkay}, {Value: "Okay 4", Status: "okay"},
{Value: "Jokingly 4", Status: db.StatusJokingly}, {Value: "Jokingly 4", Status: "jokingly"},
{Value: "Friends only 4", Status: db.StatusFriendsOnly}, {Value: "Friends only 4", Status: "friends_only"},
{Value: "Avoid 4", Status: db.StatusAvoid}, {Value: "Avoid 4", Status: "avoid"},
}, },
}, },
{ {
Name: "Field 5", Name: "Field 5",
Entries: []db.FieldEntry{ Entries: []db.FieldEntry{
{Value: "Favourite 5", Status: db.StatusFavourite}, {Value: "Favourite 5", Status: "favourite"},
{Value: "Okay 5", Status: db.StatusOkay}, {Value: "Okay 5", Status: "okay"},
{Value: "Jokingly 5", Status: db.StatusJokingly}, {Value: "Jokingly 5", Status: "jokingly"},
{Value: "Friends only 5", Status: db.StatusFriendsOnly}, {Value: "Friends only 5", Status: "friends_only"},
{Value: "Avoid 5", Status: db.StatusAvoid}, {Value: "Avoid 5", Status: "avoid"},
}, },
}, },
}) })