// UpdateUser updates user's information. func UpdateUser(u *User) error { has, err := x.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User)) if err != nil { return err } else if has { return ErrEmailAlreadyUsed } u.LowerName = strings.ToLower(u.Name) if len(u.Location) > 255 { u.Location = u.Location[:255] } if len(u.Website) > 255 { u.Website = u.Website[:255] } if len(u.Description) > 255 { u.Description = u.Description[:255] } if u.AvatarEmail == "" { u.AvatarEmail = u.Email } u.Avatar = avatar.HashEmail(u.AvatarEmail) u.FullName = base.Sanitizer.Sanitize(u.FullName) _, err = x.Id(u.Id).AllCols().Update(u) return err }
func TestFetch(t *testing.T) { os.Mkdir(TMPDIR, 0755) defer os.RemoveAll(TMPDIR) hash := avatar.HashEmail("*****@*****.**") a := avatar.New(hash, TMPDIR) a.UpdateTimeout(time.Millisecond * 200) }
// AvatarLink returns avatar link by given e-mail. func AvatarLink(email string) string { if setting.DisableGravatar || setting.OfflineMode { return setting.AppSubUrl + "/img/avatar_default.jpg" } gravatarHash := avatar.HashEmail(email) if setting.Service.EnableCacheAvatar { return setting.AppSubUrl + "/avatar/" + gravatarHash } return setting.GravatarSource + gravatarHash }
// CreateUser creates record of a new user. func CreateUser(u *User) error { if !IsLegalName(u.Name) { return ErrUserNameIllegal } isExist, err := IsUserExist(0, u.Name) if err != nil { return err } else if isExist { return ErrUserAlreadyExist } isExist, err = IsEmailUsed(u.Email) if err != nil { return err } else if isExist { return ErrEmailAlreadyUsed } u.LowerName = strings.ToLower(u.Name) u.AvatarEmail = u.Email u.Avatar = avatar.HashEmail(u.AvatarEmail) u.Rands = GetUserSalt() u.Salt = GetUserSalt() u.EncodePasswd() sess := x.NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err } if _, err = sess.Insert(u); err != nil { sess.Rollback() return err } else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil { sess.Rollback() return err } else if err = sess.Commit(); err != nil { return err } // Auto-set admin for user whose ID is 1. if u.Id == 1 { u.IsAdmin = true u.IsActive = true _, err = x.Id(u.Id).UseBool().Update(u) } return err }
func TestFetchMany(t *testing.T) { os.Mkdir(TMPDIR, 0755) defer os.RemoveAll(TMPDIR) t.Log("start") var n = 5 ch := make(chan bool, n) for i := 0; i < n; i++ { go func(i int) { hash := avatar.HashEmail(strconv.Itoa(i) + "*****@*****.**") a := avatar.New(hash, TMPDIR) a.Update() t.Log("finish", hash) ch <- true }(i) } for i := 0; i < n; i++ { <-ch } t.Log("end") }