From 4039013344c008a7aa1baada654727eb33fc7ec1 Mon Sep 17 00:00:00 2001 From: mkwcat Date: Sat, 16 Dec 2023 13:56:49 -0500 Subject: [PATCH] Database: Only set last name field when needed --- database/login.go | 17 ++++++++++++++--- database/user.go | 17 ++++++++++------- gpcm/profile.go | 2 +- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/database/login.go b/database/login.go index 80481f5..d36117a 100644 --- a/database/login.go +++ b/database/login.go @@ -37,11 +37,21 @@ func LoginUserToGPCM(pool *pgxpool.Pool, ctx context.Context, userId uint64, gsb logging.Notice("DATABASE", "Created new GPCM user:", aurora.Cyan(userId), aurora.Cyan(gsbrcd), aurora.Cyan(user.ProfileId)) } else { var expectedNgId *uint32 - err := pool.QueryRow(ctx, GetUserProfileID, userId, gsbrcd).Scan(&user.ProfileId, &expectedNgId, &user.Email, &user.UniqueNick, &user.FirstName, &user.LastName) + var firstName *string + var lastName *string + err := pool.QueryRow(ctx, GetUserProfileID, userId, gsbrcd).Scan(&user.ProfileId, &expectedNgId, &user.Email, &user.UniqueNick, &firstName, &lastName) if err != nil { panic(err) } + if firstName != nil { + user.FirstName = *firstName + } + + if lastName != nil { + user.LastName = *lastName + } + if expectedNgId != nil { user.NgDeviceId = *expectedNgId if ngDeviceId != 0 && user.NgDeviceId != ngDeviceId { @@ -68,8 +78,9 @@ func LoginUserToGPCM(pool *pgxpool.Pool, ctx context.Context, userId uint64, gsb logging.Notice("DATABASE", "Log in GPCM user:", aurora.Cyan(userId), aurora.Cyan(user.GsbrCode), "-", aurora.Cyan(user.ProfileId)) } - if user.LastName == "" { - user = UpdateProfile(pool, ctx, user.ProfileId, map[string]string{ + // This should be set if the user already knows its own profile ID + if profileId != 0 && user.LastName == "" { + user.UpdateProfile(pool, ctx, map[string]string{ "lastname": "000000000" + gsbrcd, }) } diff --git a/database/user.go b/database/user.go index a63f9f0..d8e62c9 100644 --- a/database/user.go +++ b/database/user.go @@ -10,7 +10,7 @@ import ( const ( InsertUser = `INSERT INTO users (user_id, gsbrcd, password, ng_device_id, email, unique_nick) VALUES ($1, $2, $3, $4, $5, $6) RETURNING profile_id` InsertUserWithProfileID = `INSERT INTO users (profile_id, user_id, gsbrcd, password, ng_device_id, email, unique_nick) VALUES ($1, $2, $3, $4, $5, $6, $7)` - UpdateUserTable = `UPDATE users SET firstname = CASE WHEN $3 THEN $2 ELSE firstname END, lastname = CASE WHEN $5 THEN $4 ELSE lastname END WHERE profile_id = $1 RETURNING user_id, gsbrcd, email, unique_nick, firstname, lastname` + UpdateUserTable = `UPDATE users SET firstname = CASE WHEN $3 THEN $2 ELSE firstname END, lastname = CASE WHEN $5 THEN $4 ELSE lastname END WHERE profile_id = $1` UpdateUserProfileID = `UPDATE users SET profile_id = $3 WHERE user_id = $1 AND gsbrcd = $2` UpdateUserNGDeviceID = `UPDATE users SET ng_device_id = $2 WHERE profile_id = $1` GetUser = `SELECT user_id, gsbrcd, email, unique_nick, firstname, lastname FROM users WHERE profile_id = $1` @@ -90,19 +90,22 @@ func GetUniqueUserID() uint64 { return uint64(rand.Int63n(0x80000000000)) } -func UpdateProfile(pool *pgxpool.Pool, ctx context.Context, profileId uint32, data map[string]string) User { +func (user *User) UpdateProfile(pool *pgxpool.Pool, ctx context.Context, data map[string]string) { firstName, firstNameExists := data["firstname"] lastName, lastNameExists := data["lastname"] - user := User{} - row := pool.QueryRow(ctx, UpdateUserTable, profileId, firstName, firstNameExists, lastName, lastNameExists) - err := row.Scan(&user.UserId, &user.GsbrCode, &user.Email, &user.UniqueNick, &user.FirstName, &user.LastName) + _, err := pool.Exec(ctx, UpdateUserTable, user.ProfileId, firstName, firstNameExists, lastName, lastNameExists) if err != nil { panic(err) } - user.ProfileId = profileId - return user + if firstNameExists { + user.FirstName = firstName + } + + if lastNameExists { + user.LastName = lastName + } } func GetProfile(pool *pgxpool.Pool, ctx context.Context, profileId uint32) (User, bool) { diff --git a/gpcm/profile.go b/gpcm/profile.go index 1e7df69..178809d 100644 --- a/gpcm/profile.go +++ b/gpcm/profile.go @@ -61,5 +61,5 @@ func (g *GameSpySession) getProfile(command common.GameSpyCommand) { } func (g *GameSpySession) updateProfile(command common.GameSpyCommand) { - g.User = database.UpdateProfile(pool, ctx, g.User.ProfileId, command.OtherValues) + g.User.UpdateProfile(pool, ctx, command.OtherValues) }