1 import * as Sequelize from 'sequelize'
2 import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
3 import { database as db } from '../initializers'
4 import { CONFIG } from '../initializers/constants'
5 import { UserInstance } from '../models'
6 import { createVideoChannel } from './video-channel'
7 import { logger } from '../helpers/logger'
8 import { getAccountActivityPubUrl } from './activitypub/url'
10 async function createUserAccountAndChannel (user: UserInstance, validateUser = true) {
11 const { account, videoChannel } = await db.sequelize.transaction(async t => {
14 validate: validateUser
17 const userCreated = await user.save(userOptions)
18 const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t)
20 const videoChannelName = `Default ${userCreated.username} channel`
21 const videoChannelInfo = {
22 name: videoChannelName
24 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
26 return { account: accountCreated, videoChannel }
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))
35 return { account, videoChannel }
38 async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
39 const url = getAccountActivityPubUrl(name)
41 const accountInstance = db.Account.build({
48 inboxUrl: url + '/inbox',
49 outboxUrl: url + '/outbox',
50 sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
51 followersUrl: url + '/followers',
52 followingUrl: url + '/following',
55 serverId: null // It is our server
58 return accountInstance.save({ transaction: t })
61 // ---------------------------------------------------------------------------
64 createUserAccountAndChannel,
65 createLocalAccountWithoutKeys