Skip to content

Commit

Permalink
Fix: Duplicate system users are created #858
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jan 31, 2021
1 parent 200e579 commit cc12577
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/services/create-system-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 username is already exists');

account = await transactionalEntityManager.insert(User, {
id: genId(),
createdAt: new Date(),
username: username,
Expand All @@ -35,24 +42,24 @@ export async function createSystemUser(username: string) {
isAdmin: false,
isLocked: true,
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;
Expand Down

0 comments on commit cc12577

Please sign in to comment.