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 { ActorModel } from '../models/activitypub/actor'
7 import { getAccountActivityPubUrl } from './activitypub'
8 import { createVideoChannel } from './video-channel'
10 async function createUserAccountAndChannel (user: UserModel, validateUser = true) {
11 const { account, videoChannel } = await sequelizeTypescript.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 const actor = account.Actor
32 actor.set('publicKey', publicKey)
33 actor.set('privateKey', privateKey)
34 actor.save().catch(err => logger.error('Cannot set public/private keys of actor %d.', actor.uuid, err))
36 return { account, videoChannel }
39 async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
40 const url = getAccountActivityPubUrl(name)
42 const actorInstance = new ActorModel({
48 inboxUrl: url + '/inbox',
49 outboxUrl: url + '/outbox',
50 sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
51 followersUrl: url + '/followers',
52 followingUrl: url + '/following'
54 const actorInstanceCreated = await actorInstance.save({ transaction: t })
56 const accountInstance = new AccountModel({
60 actorId: actorInstanceCreated.id,
61 serverId: null // It is our server
64 const accountInstanceCreated = await accountInstance.save({ transaction: t })
65 accountInstanceCreated.Actor = actorInstanceCreated
67 return accountInstanceCreated
70 // ---------------------------------------------------------------------------
73 createUserAccountAndChannel,
74 createLocalAccountWithoutKeys