front: Don't pass maxPlayerCount to UpdateUser unless we're setting it

This commit is contained in:
Evan Goode 2025-02-27 10:43:51 -05:00
parent fbc8f9d45a
commit faa2bf9f75

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/samber/mo"
"gorm.io/gorm" "gorm.io/gorm"
"html/template" "html/template"
"io" "io"
@ -607,6 +608,15 @@ func nilIfEmpty(str string) *string {
return &str return &str
} }
func getFormValue(c *echo.Context, key string) mo.Option[string] {
// Call FormValue first to parse the form appropriately
value := (*c).FormValue(key)
if (*c).Request().Form.Has(key) {
return mo.Some(value)
}
return mo.None[string]()
}
// POST /update-user // POST /update-user
func FrontUpdateUser(app *App) func(c echo.Context) error { func FrontUpdateUser(app *App) func(c echo.Context) error {
return withBrowserAuthentication(app, true, func(c echo.Context, user *User) error { return withBrowserAuthentication(app, true, func(c echo.Context, user *User) error {
@ -616,7 +626,7 @@ func FrontUpdateUser(app *App) func(c echo.Context) error {
password := nilIfEmpty(c.FormValue("password")) password := nilIfEmpty(c.FormValue("password"))
resetAPIToken := c.FormValue("resetApiToken") == "on" resetAPIToken := c.FormValue("resetApiToken") == "on"
preferredLanguage := nilIfEmpty(c.FormValue("preferredLanguage")) preferredLanguage := nilIfEmpty(c.FormValue("preferredLanguage"))
maxPlayerCountString := c.FormValue("maxPlayerCount") maybeMaxPlayerCountString := getFormValue(&c, "maxPlayerCount")
var targetUser *User var targetUser *User
if targetUUID == nil || *targetUUID == user.UUID { if targetUUID == nil || *targetUUID == user.UUID {
@ -633,15 +643,18 @@ func FrontUpdateUser(app *App) func(c echo.Context) error {
} }
} }
maxPlayerCount := targetUser.MaxPlayerCount maybeMaxPlayerCount := mo.None[int]()
if maxPlayerCountString, ok := maybeMaxPlayerCountString.Get(); ok {
if maxPlayerCountString == "" { if maxPlayerCountString == "" {
maxPlayerCount = app.Constants.MaxPlayerCountUseDefault maybeMaxPlayerCount = mo.Some(app.Constants.MaxPlayerCountUseDefault)
} else { } else {
var err error var err error
maxPlayerCount, err = strconv.Atoi(maxPlayerCountString) maxPlayerCount, err := strconv.Atoi(maxPlayerCountString)
if err != nil { if err != nil {
return NewWebError(returnURL, "Max player count must be an integer.") return NewWebError(returnURL, "Max player count must be an integer.")
} }
maybeMaxPlayerCount = mo.Some(maxPlayerCount)
}
} }
_, err := app.UpdateUser( _, err := app.UpdateUser(
@ -653,7 +666,7 @@ func FrontUpdateUser(app *App) func(c echo.Context) error {
nil, // isLocked nil, // isLocked
resetAPIToken, resetAPIToken,
preferredLanguage, preferredLanguage,
&maxPlayerCount, maybeMaxPlayerCount.ToPointer(),
) )
if err != nil { if err != nil {
var userError *UserError var userError *UserError