fixed bug: ws message is sent to all users also when user has nothing changed

main
alex 2023-06-25 23:32:36 +02:00
parent 8b056c3191
commit fab7ce7da1
1 changed files with 61 additions and 26 deletions

View File

@ -221,7 +221,6 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
var user structs.User var user structs.User
var updates = make(map[string]interface{}) var updates = make(map[string]interface{})
var changesResult = make(map[string]uint8)
if changes["username"] != nil { if changes["username"] != nil {
username := changes["username"].(string) username := changes["username"].(string)
@ -230,9 +229,18 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
if isUsernameAvailable(username) { if isUsernameAvailable(username) {
user.Username = username user.Username = username
updates["Username"] = username updates["Username"] = username
changesResult["Username"] = 0
} else { } else {
changesResult["Username"] = 1 SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{
Cmd: utils.SentCmdUserProfileUpdated,
Body: struct {
UserId string
Result uint8
}{
UserId: userId,
Result: 0,
},
})
return
} }
} }
} }
@ -243,9 +251,18 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
if isEmailAvailable(email) { if isEmailAvailable(email) {
user.Email = email user.Email = email
updates["Email"] = email updates["Email"] = email
changesResult["Email"] = 0
} else { } else {
changesResult["Email"] = 1 SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{
Cmd: utils.SentCmdUserProfileUpdated,
Body: struct {
UserId string
Result uint8
}{
UserId: userId,
Result: 1,
},
})
return
} }
} }
@ -256,7 +273,22 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
decodedOldPassword, err := base64.StdEncoding.DecodeString(oldPassword) decodedOldPassword, err := base64.StdEncoding.DecodeString(oldPassword)
decodedNewPassword, err1 := base64.StdEncoding.DecodeString(newPassword) decodedNewPassword, err1 := base64.StdEncoding.DecodeString(newPassword)
if err == nil && err1 == nil { if err != nil || err1 != nil {
log.Error().Msgf("Error decoding old or new password %s %s", err.Error(), err1.Error())
SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{
Cmd: utils.SentCmdUserProfileUpdated,
Body: struct {
UserId string
Result uint8
}{
UserId: userId,
Result: 2,
},
})
return
}
if utils.IsPasswordLengthValid(string(decodedOldPassword)) { // only affected if username was manipulated as min and max is provided in web ui if utils.IsPasswordLengthValid(string(decodedOldPassword)) { // only affected if username was manipulated as min and max is provided in web ui
database.DB.Select("password").First(&user, "id = ?", userId) database.DB.Select("password").First(&user, "id = ?", userId)
@ -270,19 +302,24 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
} }
} else { } else {
log.Error().Msg("Incorrect password") log.Error().Msg("Incorrect password")
changesResult["Password"] = 1
} SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{
} Cmd: utils.SentCmdUserProfileUpdated,
} else { Body: struct {
if err != nil { UserId string
log.Error().Msgf("Failed to decode old password %s", err.Error()) Result uint8
} }{
if err1 != nil { UserId: userId,
log.Error().Msgf("Failed to decode new password %s", err1.Error()) Result: 2,
},
})
return
} }
} }
} }
log.Debug().Msgf("changes %v %v", len(changes), changes)
if len(changes) > 0 { if len(changes) > 0 {
user.UpdatedAt = time.Now() user.UpdatedAt = time.Now()
@ -298,11 +335,9 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
Body: struct { Body: struct {
UserId string UserId string
Changes map[string]interface{} Changes map[string]interface{}
Result map[string]uint8
}{ }{
UserId: userId, UserId: userId,
Changes: updates, Changes: updates,
Result: changesResult,
}, },
}) })
} }