refactor(model): update user method. (#3360)

This commit is contained in:
Bo-Yi Wu 2018-01-13 17:45:16 +08:00 committed by Lauris BH
parent f7088292bb
commit 7a2a3fbafc

View File

@ -138,6 +138,22 @@ func (u *User) BeforeUpdate() {
if u.MaxRepoCreation < -1 { if u.MaxRepoCreation < -1 {
u.MaxRepoCreation = -1 u.MaxRepoCreation = -1
} }
// Organization does not need email
u.Email = strings.ToLower(u.Email)
if !u.IsOrganization() {
if len(u.AvatarEmail) == 0 {
u.AvatarEmail = u.Email
}
if len(u.AvatarEmail) > 0 {
u.Avatar = base.HashEmail(u.AvatarEmail)
}
}
u.LowerName = strings.ToLower(u.Name)
u.Location = base.TruncateString(u.Location, 255)
u.Website = base.TruncateString(u.Website, 255)
u.Description = base.TruncateString(u.Description, 255)
} }
// SetLastLogin set time to last login // SetLastLogin set time to last login
@ -840,22 +856,6 @@ func checkDupEmail(e Engine, u *User) error {
} }
func updateUser(e Engine, u *User) error { func updateUser(e Engine, u *User) error {
// Organization does not need email
u.Email = strings.ToLower(u.Email)
if !u.IsOrganization() {
if len(u.AvatarEmail) == 0 {
u.AvatarEmail = u.Email
}
if len(u.AvatarEmail) > 0 {
u.Avatar = base.HashEmail(u.AvatarEmail)
}
}
u.LowerName = strings.ToLower(u.Name)
u.Location = base.TruncateString(u.Location, 255)
u.Website = base.TruncateString(u.Website, 255)
u.Description = base.TruncateString(u.Description, 255)
_, err := e.ID(u.ID).AllCols().Update(u) _, err := e.ID(u.ID).AllCols().Update(u)
return err return err
} }
@ -871,22 +871,6 @@ func UpdateUserCols(u *User, cols ...string) error {
} }
func updateUserCols(e Engine, u *User, cols ...string) error { func updateUserCols(e Engine, u *User, cols ...string) error {
// Organization does not need email
u.Email = strings.ToLower(u.Email)
if !u.IsOrganization() {
if len(u.AvatarEmail) == 0 {
u.AvatarEmail = u.Email
}
if len(u.AvatarEmail) > 0 {
u.Avatar = base.HashEmail(u.AvatarEmail)
}
}
u.LowerName = strings.ToLower(u.Name)
u.Location = base.TruncateString(u.Location, 255)
u.Website = base.TruncateString(u.Website, 255)
u.Description = base.TruncateString(u.Description, 255)
_, err := e.ID(u.ID).Cols(cols...).Update(u) _, err := e.ID(u.ID).Cols(cols...).Update(u)
return err return err
} }