From 3be7eafa6b01cfcaad46032404a874e8bd291167 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Sat, 6 Feb 2021 11:43:12 +0900 Subject: [PATCH] Fix: Duplicate system users are created (#7141) --- src/services/create-system-user.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/services/create-system-user.ts b/src/services/create-system-user.ts index 3c44c7427f..39ddc92abd 100644 --- a/src/services/create-system-user.ts +++ b/src/services/create-system-user.ts @@ -25,7 +25,14 @@ export async function createSystemUser(username: string) { // Start transaction await getConnection().transaction(async transactionalEntityManager => { - account = await transactionalEntityManager.save(new User({ + const exist = await transactionalEntityManager.findOne(User, { + usernameLower: username.toLowerCase(), + host: null + }); + + if (exist) throw new Error('the user is already exists'); + + account = await transactionalEntityManager.insert(User, { id: genId(), createdAt: new Date(), username: username, @@ -36,24 +43,24 @@ export async function createSystemUser(username: string) { isLocked: true, isExplorable: false, isBot: true, - })); + }).then(x => transactionalEntityManager.findOneOrFail(User, x.identifiers[0])); - await transactionalEntityManager.save(new UserKeypair({ + await transactionalEntityManager.insert(UserKeypair, { publicKey: keyPair.publicKey, privateKey: keyPair.privateKey, userId: account.id - })); + }); - await transactionalEntityManager.save(new UserProfile({ + await transactionalEntityManager.insert(UserProfile, { userId: account.id, autoAcceptFollowed: false, password: hash, - })); + }); - await transactionalEntityManager.save(new UsedUsername({ + await transactionalEntityManager.insert(UsedUsername, { createdAt: new Date(), username: username.toLowerCase(), - })); + }); }); return account;