]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Make it compile at least
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
CommitLineData
571389d4
C
1import * as Sequelize from 'sequelize'
2import { getActivityPubUrl } from '../helpers/activitypub'
3import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
72c7248b 4import { database as db } from '../initializers'
571389d4 5import { CONFIG } from '../initializers/constants'
72c7248b 6import { UserInstance } from '../models'
72c7248b
C
7import { createVideoChannel } from './video-channel'
8
e4f97bab 9async function createUserAccountAndChannel (user: UserInstance, validateUser = true) {
f5028693 10 const res = await db.sequelize.transaction(async t => {
72c7248b
C
11 const userOptions = {
12 transaction: t,
13 validate: validateUser
14 }
15
f5028693 16 const userCreated = await user.save(userOptions)
571389d4 17 const accountCreated = await createLocalAccount(user.username, user.id, null, t)
f5028693
C
18
19 const videoChannelInfo = {
20 name: `Default ${userCreated.username} channel`
21 }
e4f97bab 22 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
f5028693 23
571389d4 24 return { account: accountCreated, videoChannel }
72c7248b 25 })
f5028693
C
26
27 return res
72c7248b
C
28}
29
571389d4
C
30async 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
72c7248b
C
54// ---------------------------------------------------------------------------
55
56export {
571389d4
C
57 createUserAccountAndChannel,
58 createLocalAccount
72c7248b 59}