1 import * as Sequelize from 'sequelize'
2 import { getActivityPubUrl } from '../helpers/activitypub'
3 import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
4 import { database as db } from '../initializers'
5 import { CONFIG } from '../initializers/constants'
6 import { UserInstance } from '../models'
7 import { createVideoChannel } from './video-channel'
9 async function createUserAccountAndChannel (user: UserInstance, validateUser = true) {
10 const res = await db.sequelize.transaction(async t => {
13 validate: validateUser
16 const userCreated = await user.save(userOptions)
17 const accountCreated = await createLocalAccount(user.username, user.id, null, t)
19 const videoChannelInfo = {
20 name: `Default ${userCreated.username} channel`
22 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
24 return { account: accountCreated, videoChannel }
30 async function createLocalAccount (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
31 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
32 const url = getActivityPubUrl('account', name)
34 const accountInstance = db.Account.build({
41 inboxUrl: url + '/inbox',
42 outboxUrl: url + '/outbox',
43 sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
44 followersUrl: url + '/followers',
45 followingUrl: url + '/following',
48 podId: null // It is our pod
51 return accountInstance.save({ transaction: t })
54 // ---------------------------------------------------------------------------
57 createUserAccountAndChannel,