Fix: Duplicate system users are created (#7141)

This commit is contained in:
MeiMei 2021-02-06 11:43:12 +09:00 committed by GitHub
parent 9dd1a8fb2e
commit 3be7eafa6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 8 deletions

View File

@ -25,7 +25,14 @@ export async function createSystemUser(username: string) {
// Start transaction // Start transaction
await getConnection().transaction(async transactionalEntityManager => { 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(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
username: username, username: username,
@ -36,24 +43,24 @@ export async function createSystemUser(username: string) {
isLocked: true, isLocked: true,
isExplorable: false, isExplorable: false,
isBot: true, isBot: true,
})); }).then(x => transactionalEntityManager.findOneOrFail(User, x.identifiers[0]));
await transactionalEntityManager.save(new UserKeypair({ await transactionalEntityManager.insert(UserKeypair, {
publicKey: keyPair.publicKey, publicKey: keyPair.publicKey,
privateKey: keyPair.privateKey, privateKey: keyPair.privateKey,
userId: account.id userId: account.id
})); });
await transactionalEntityManager.save(new UserProfile({ await transactionalEntityManager.insert(UserProfile, {
userId: account.id, userId: account.id,
autoAcceptFollowed: false, autoAcceptFollowed: false,
password: hash, password: hash,
})); });
await transactionalEntityManager.save(new UsedUsername({ await transactionalEntityManager.insert(UsedUsername, {
createdAt: new Date(), createdAt: new Date(),
username: username.toLowerCase(), username: username.toLowerCase(),
})); });
}); });
return account; return account;