1 import * as Sequelize from 'sequelize'
2 import { createPrivateAndPublicKeys, logger } from '../helpers'
3 import { CONFIG, sequelizeTypescript } from '../initializers'
4 import { AccountModel } from '../models/account/account'
5 import { UserModel } from '../models/account/user'
6 import { getAccountActivityPubUrl } from './activitypub'
7 import { createVideoChannel } from './video-channel'
9 async function createUserAccountAndChannel (user: UserModel, validateUser = true) {
10 const { account, videoChannel } = await sequelizeTypescript.transaction(async t => {
13 validate: validateUser
16 const userCreated = await user.save(userOptions)
17 const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t)
19 const videoChannelName = `Default ${userCreated.username} channel`
20 const videoChannelInfo = {
21 name: videoChannelName
23 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
25 return { account: accountCreated, videoChannel }
28 // Set account keys, this could be long so process after the account creation and do not block the client
29 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
30 account.set('publicKey', publicKey)
31 account.set('privateKey', privateKey)
32 account.save().catch(err => logger.error('Cannot set public/private keys of local account %d.', account.id, err))
34 return { account, videoChannel }
37 async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
38 const url = getAccountActivityPubUrl(name)
40 const accountInstance = new AccountModel({
47 inboxUrl: url + '/inbox',
48 outboxUrl: url + '/outbox',
49 sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
50 followersUrl: url + '/followers',
51 followingUrl: url + '/following',
54 serverId: null // It is our server
57 return accountInstance.save({ transaction: t })
60 // ---------------------------------------------------------------------------
63 createUserAccountAndChannel,
64 createLocalAccountWithoutKeys