]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/user.ts
Rename Pod -> Server
[github/Chocobozzz/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 videoChannelName = `Default ${userCreated.username} channel`
20 const videoChannelInfo = {
21 name: videoChannelName
22 }
23 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
24
25 return { account: accountCreated, videoChannel }
26 })
27
28 return res
29 }
30
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)
34
35 const accountInstance = db.Account.build({
36 name,
37 url,
38 publicKey,
39 privateKey,
40 followersCount: 0,
41 followingCount: 0,
42 inboxUrl: url + '/inbox',
43 outboxUrl: url + '/outbox',
44 sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
45 followersUrl: url + '/followers',
46 followingUrl: url + '/following',
47 userId,
48 applicationId,
49 serverId: null // It is our server
50 })
51
52 return accountInstance.save({ transaction: t })
53 }
54
55 // ---------------------------------------------------------------------------
56
57 export {
58 createUserAccountAndChannel,
59 createLocalAccount
60 }