MakeUsername Generate a unique Username based on the displayName
(ctx context.Context, displayName string)
| 193 | // MakeUsername |
| 194 | // Generate a unique Username based on the displayName |
| 195 | func (us *UserCommon) MakeUsername(ctx context.Context, displayName string) (username string, err error) { |
| 196 | // Chinese processing |
| 197 | if has := checker.IsChinese(displayName); has { |
| 198 | displayName = strings.Join(pinyin.LazyConvert(displayName, nil), "") |
| 199 | } |
| 200 | |
| 201 | username = strings.ReplaceAll(displayName, " ", "-") |
| 202 | username = strings.ToLower(username) |
| 203 | suffix := "" |
| 204 | |
| 205 | if checker.IsInvalidUsername(username) { |
| 206 | return "", errors.BadRequest(reason.UsernameInvalid) |
| 207 | } |
| 208 | |
| 209 | if checker.IsReservedUsername(username) { |
| 210 | return "", errors.BadRequest(reason.UsernameInvalid) |
| 211 | } |
| 212 | |
| 213 | for { |
| 214 | _, has, err := us.userRepo.GetByUsername(ctx, username+suffix) |
| 215 | if err != nil { |
| 216 | return "", err |
| 217 | } |
| 218 | if !has { |
| 219 | break |
| 220 | } |
| 221 | suffix = random.UsernameSuffix() |
| 222 | } |
| 223 | return username + suffix, nil |
| 224 | } |
| 225 | |
| 226 | func (us *UserCommon) CacheLoginUserInfo(ctx context.Context, userID string, userStatus, emailStatus int, externalID string) ( |
| 227 | accessToken string, userCacheInfo *entity.UserCacheInfo, err error) { |
no test coverage detected