@@ -6,6 +6,8 @@ import { HttpStatus, Injectable } from '@nestjs/common';
66import { AppException } from 'omniboxd/common/exceptions/app.exception' ;
77import { I18nService } from 'nestjs-i18n' ;
88import { CacheService } from 'omniboxd/common/cache.service' ;
9+ import { NamespacesService } from 'omniboxd/namespaces/namespaces.service' ;
10+ import { isNameBlocked } from 'omniboxd/utils/blocked-names' ;
911
1012export interface UserSocialState {
1113 type : string ;
@@ -24,6 +26,7 @@ export class SocialService {
2426 private readonly userService : UserService ,
2527 private readonly i18n : I18nService ,
2628 private readonly cacheService : CacheService ,
29+ private readonly namespacesService : NamespacesService ,
2730 ) { }
2831
2932 /**
@@ -71,6 +74,33 @@ export class SocialService {
7174 ) ;
7275 }
7376
77+ private async isUsernameValid (
78+ username : string ,
79+ manager : EntityManager ,
80+ ) : Promise < boolean > {
81+ if ( isNameBlocked ( username ) ) {
82+ return false ;
83+ }
84+
85+ const user = await this . userService . findByUsername ( username , manager ) ;
86+ if ( user ) {
87+ return false ;
88+ }
89+
90+ const namespaceName = this . i18n . t ( 'namespace.userNamespaceName' , {
91+ args : { userName : username } ,
92+ } ) ;
93+ const namespace = await this . namespacesService . getNamespaceByName (
94+ namespaceName ,
95+ manager ,
96+ ) ;
97+ if ( namespace ) {
98+ return false ;
99+ }
100+
101+ return true ;
102+ }
103+
74104 async getValidUsername (
75105 nickname : string ,
76106 manager : EntityManager ,
@@ -81,21 +111,18 @@ export class SocialService {
81111 username = nickname . slice ( 0 , this . maxUsernameLength ) ;
82112 }
83113 if ( username . length >= this . minUsernameLength ) {
84- const user = await this . userService . findByUsername ( username , manager ) ;
85- if ( ! user ) {
114+ const ok = await this . isUsernameValid ( username , manager ) ;
115+ if ( ok ) {
86116 return username ;
87117 }
88118 }
89119
90120 username = nickname . slice ( 0 , this . maxUsernameLength - 5 ) ;
91121 for ( let i = 0 ; i < 5 ; i ++ ) {
92- const suffix = this . generateSuffix ( ) ;
93- const user = await this . userService . findByUsername (
94- username + suffix ,
95- manager ,
96- ) ;
97- if ( ! user ) {
98- return username + suffix ;
122+ const curUsername = username + this . generateSuffix ( ) ;
123+ const ok = await this . isUsernameValid ( curUsername , manager ) ;
124+ if ( ok ) {
125+ return curUsername ;
99126 }
100127 }
101128
0 commit comments