aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/user.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-16 18:40:50 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:52 +0100
commit47e0652b4a98916d4a1d012fbec61afd73a30565 (patch)
tree8d400cce95b50d18921b30fea1d5d2d89b5da740 /server/lib/user.ts
parent41dbdb8acff3ac56187e8149eab0ff82e2377597 (diff)
downloadPeerTube-47e0652b4a98916d4a1d012fbec61afd73a30565.tar.gz
PeerTube-47e0652b4a98916d4a1d012fbec61afd73a30565.tar.zst
PeerTube-47e0652b4a98916d4a1d012fbec61afd73a30565.zip
Optimize account creation
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r--server/lib/user.ts22
1 files changed, 14 insertions, 8 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts
index 9884e566f..2d7b36b4f 100644
--- a/server/lib/user.ts
+++ b/server/lib/user.ts
@@ -5,16 +5,17 @@ import { database as db } from '../initializers'
5import { CONFIG } from '../initializers/constants' 5import { CONFIG } from '../initializers/constants'
6import { UserInstance } from '../models' 6import { UserInstance } from '../models'
7import { createVideoChannel } from './video-channel' 7import { createVideoChannel } from './video-channel'
8import { logger } from '../helpers/logger'
8 9
9async function createUserAccountAndChannel (user: UserInstance, validateUser = true) { 10async function createUserAccountAndChannel (user: UserInstance, validateUser = true) {
10 const res = await db.sequelize.transaction(async t => { 11 const { account, videoChannel } = await db.sequelize.transaction(async t => {
11 const userOptions = { 12 const userOptions = {
12 transaction: t, 13 transaction: t,
13 validate: validateUser 14 validate: validateUser
14 } 15 }
15 16
16 const userCreated = await user.save(userOptions) 17 const userCreated = await user.save(userOptions)
17 const accountCreated = await createLocalAccount(user.username, user.id, null, t) 18 const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t)
18 19
19 const videoChannelName = `Default ${userCreated.username} channel` 20 const videoChannelName = `Default ${userCreated.username} channel`
20 const videoChannelInfo = { 21 const videoChannelInfo = {
@@ -25,18 +26,23 @@ async function createUserAccountAndChannel (user: UserInstance, validateUser = t
25 return { account: accountCreated, videoChannel } 26 return { account: accountCreated, videoChannel }
26 }) 27 })
27 28
28 return res 29 // Set account keys, this could be long so process after the account creation and do not block the client
30 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
31 account.set('publicKey', publicKey)
32 account.set('privateKey', privateKey)
33 account.save().catch(err => logger.error('Cannot set public/private keys of local account %d.', account.id, err))
34
35 return { account, videoChannel }
29} 36}
30 37
31async function createLocalAccount (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) { 38async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
32 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
33 const url = getActivityPubUrl('account', name) 39 const url = getActivityPubUrl('account', name)
34 40
35 const accountInstance = db.Account.build({ 41 const accountInstance = db.Account.build({
36 name, 42 name,
37 url, 43 url,
38 publicKey, 44 publicKey: null,
39 privateKey, 45 privateKey: null,
40 followersCount: 0, 46 followersCount: 0,
41 followingCount: 0, 47 followingCount: 0,
42 inboxUrl: url + '/inbox', 48 inboxUrl: url + '/inbox',
@@ -56,5 +62,5 @@ async function createLocalAccount (name: string, userId: number, applicationId:
56 62
57export { 63export {
58 createUserAccountAndChannel, 64 createUserAccountAndChannel,
59 createLocalAccount 65 createLocalAccountWithoutKeys
60} 66}