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 videoChannelName = `Default ${userCreated.username} channel`
20 const videoChannelInfo = {
21 name: videoChannelName
23 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
25 return { account: accountCreated, videoChannel }
31 async function createLocalAccount (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
32 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
33 const url = getActivityPubUrl('account', name)
35 const accountInstance = db.Account.build({
42 inboxUrl: url + '/inbox',
43 outboxUrl: url + '/outbox',
44 sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
45 followersUrl: url + '/followers',
46 followingUrl: url + '/following',
49 podId: null // It is our pod
52 return accountInstance.save({ transaction: t })
55 // ---------------------------------------------------------------------------
58 createUserAccountAndChannel,