1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 19:42:24 +02:00

Optimize account creation

This commit is contained in:
Chocobozzz 2017-11-16 18:40:50 +01:00
parent 41dbdb8acf
commit 47e0652b4a
No known key found for this signature in database
GPG key ID: 583A612D890159BE
8 changed files with 44 additions and 26 deletions

View file

@ -5,16 +5,17 @@ import { database as db } from '../initializers'
import { CONFIG } from '../initializers/constants'
import { UserInstance } from '../models'
import { createVideoChannel } from './video-channel'
import { logger } from '../helpers/logger'
async function createUserAccountAndChannel (user: UserInstance, validateUser = true) {
const res = await db.sequelize.transaction(async t => {
const { account, videoChannel } = await db.sequelize.transaction(async t => {
const userOptions = {
transaction: t,
validate: validateUser
}
const userCreated = await user.save(userOptions)
const accountCreated = await createLocalAccount(user.username, user.id, null, t)
const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t)
const videoChannelName = `Default ${userCreated.username} channel`
const videoChannelInfo = {
@ -25,18 +26,23 @@ async function createUserAccountAndChannel (user: UserInstance, validateUser = t
return { account: accountCreated, videoChannel }
})
return res
// Set account keys, this could be long so process after the account creation and do not block the client
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
account.set('publicKey', publicKey)
account.set('privateKey', privateKey)
account.save().catch(err => logger.error('Cannot set public/private keys of local account %d.', account.id, err))
return { account, videoChannel }
}
async function createLocalAccount (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
const url = getActivityPubUrl('account', name)
const accountInstance = db.Account.build({
name,
url,
publicKey,
privateKey,
publicKey: null,
privateKey: null,
followersCount: 0,
followingCount: 0,
inboxUrl: url + '/inbox',
@ -56,5 +62,5 @@ async function createLocalAccount (name: string, userId: number, applicationId:
export {
createUserAccountAndChannel,
createLocalAccount
createLocalAccountWithoutKeys
}