Make it compile at least
[oweals/peertube.git] / server / lib / user.ts
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'
8
9 async function createUserAccountAndChannel (user: UserInstance, validateUser = true) {
10   const res = await db.sequelize.transaction(async t => {
11     const userOptions = {
12       transaction: t,
13       validate: validateUser
14     }
15
16     const userCreated = await user.save(userOptions)
17     const accountCreated = await createLocalAccount(user.username, user.id, null, t)
18
19     const videoChannelInfo = {
20       name: `Default ${userCreated.username} channel`
21     }
22     const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
23
24     return { account: accountCreated, videoChannel }
25   })
26
27   return res
28 }
29
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)
33
34   const accountInstance = db.Account.build({
35     name,
36     url,
37     publicKey,
38     privateKey,
39     followersCount: 0,
40     followingCount: 0,
41     inboxUrl: url + '/inbox',
42     outboxUrl: url + '/outbox',
43     sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
44     followersUrl: url + '/followers',
45     followingUrl: url + '/following',
46     userId,
47     applicationId,
48     podId: null // It is our pod
49   })
50
51   return accountInstance.save({ transaction: t })
52 }
53
54 // ---------------------------------------------------------------------------
55
56 export {
57   createUserAccountAndChannel,
58   createLocalAccount
59 }