From f3e43a0fc63d9beee74dfd1f29679ff95ef1e130 Mon Sep 17 00:00:00 2001 From: tamaina Date: Tue, 2 May 2023 10:26:18 +0000 Subject: [PATCH] refactor --- packages/backend/src/core/SignupService.ts | 10 +++++----- packages/backend/src/server/api/SignupApiService.ts | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/core/SignupService.ts b/packages/backend/src/core/SignupService.ts index 2b8387f89..364bcf0f8 100644 --- a/packages/backend/src/core/SignupService.ts +++ b/packages/backend/src/core/SignupService.ts @@ -80,14 +80,16 @@ export class SignupService { throw new Error('USED_USERNAME'); } - if (!opts.ignorePreservedUsernames) { + const isTheFirstUser = (await this.usersRepository.countBy({ host: IsNull() })) === 0; + + if (!opts.ignorePreservedUsernames || !isTheFirstUser) { const instance = await this.metaService.fetch(true); const isPreserved = instance.preservedUsernames.map(x => x.toLowerCase()).includes(username.toLowerCase()); if (isPreserved) { throw new Error('USED_USERNAME'); } } - + const keyPair = await new Promise((res, rej) => generateKeyPair('rsa', { modulusLength: 4096, @@ -123,9 +125,7 @@ export class SignupService { usernameLower: username.toLowerCase(), host: this.utilityService.toPunyNullable(host), token: secret, - isRoot: (await this.usersRepository.countBy({ - host: IsNull(), - })) === 0, + isRoot: isTheFirstUser, })); await transactionalEntityManager.save(new UserKeypair({ diff --git a/packages/backend/src/server/api/SignupApiService.ts b/packages/backend/src/server/api/SignupApiService.ts index f3a1d406a..b2bd7d82e 100644 --- a/packages/backend/src/server/api/SignupApiService.ts +++ b/packages/backend/src/server/api/SignupApiService.ts @@ -169,7 +169,6 @@ export class SignupApiService { try { const { account, secret } = await this.signupService.signup({ username, password, host, - ignorePreservedUsernames: (await this.usersRepository.countBy({ host: IsNull() })) === 0, }); const res = await this.userEntityService.pack(account, account, {