MCPcopy Index your code
hub / github.com/apache/answer / MakeUsername

Method MakeUsername

internal/service/user_common/user.go:195–224  ·  view source on GitHub ↗

MakeUsername Generate a unique Username based on the displayName

(ctx context.Context, displayName string)

Source from the content-addressed store, hash-verified

193// MakeUsername
194// Generate a unique Username based on the displayName
195func (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
226func (us *UserCommon) CacheLoginUserInfo(ctx context.Context, userID string, userStatus, emailStatus int, externalID string) (
227 accessToken string, userCacheInfo *entity.UserCacheInfo, err error) {

Callers 5

registerNewUserMethod · 0.80
registerNewUserMethod · 0.80
AddUserMethod · 0.80
formatBulkAddUsersMethod · 0.80
UserRegisterByEmailMethod · 0.80

Calls 5

IsChineseFunction · 0.92
IsInvalidUsernameFunction · 0.92
IsReservedUsernameFunction · 0.92
UsernameSuffixFunction · 0.92
GetByUsernameMethod · 0.65

Tested by

no test coverage detected