]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Enh #106 : Add an autoPlayVideo user attribute (#159)
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
CommitLineData
571389d4 1import * as Sequelize from 'sequelize'
3fd3ab2d
C
2import { createPrivateAndPublicKeys, logger } from '../helpers'
3import { CONFIG, sequelizeTypescript } from '../initializers'
4import { AccountModel } from '../models/account/account'
5import { UserModel } from '../models/account/user'
6import { getAccountActivityPubUrl } from './activitypub'
72c7248b
C
7import { createVideoChannel } from './video-channel'
8
3fd3ab2d
C
9async function createUserAccountAndChannel (user: UserModel, validateUser = true) {
10 const { account, videoChannel } = await sequelizeTypescript.transaction(async t => {
72c7248b
C
11 const userOptions = {
12 transaction: t,
13 validate: validateUser
14 }
15
f5028693 16 const userCreated = await user.save(userOptions)
47e0652b 17 const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t)
f5028693 18
e34c85e5 19 const videoChannelName = `Default ${userCreated.username} channel`
f5028693 20 const videoChannelInfo = {
e34c85e5 21 name: videoChannelName
f5028693 22 }
e4f97bab 23 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
f5028693 24
571389d4 25 return { account: accountCreated, videoChannel }
72c7248b 26 })
f5028693 27
47e0652b
C
28 // Set account keys, this could be long so process after the account creation and do not block the client
29 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
30 account.set('publicKey', publicKey)
31 account.set('privateKey', privateKey)
32 account.save().catch(err => logger.error('Cannot set public/private keys of local account %d.', account.id, err))
33
34 return { account, videoChannel }
72c7248b
C
35}
36
47e0652b 37async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
54141398 38 const url = getAccountActivityPubUrl(name)
571389d4 39
3fd3ab2d 40 const accountInstance = new AccountModel({
571389d4
C
41 name,
42 url,
47e0652b
C
43 publicKey: null,
44 privateKey: null,
571389d4
C
45 followersCount: 0,
46 followingCount: 0,
47 inboxUrl: url + '/inbox',
48 outboxUrl: url + '/outbox',
49 sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox',
50 followersUrl: url + '/followers',
51 followingUrl: url + '/following',
52 userId,
53 applicationId,
60862425 54 serverId: null // It is our server
571389d4
C
55 })
56
57 return accountInstance.save({ transaction: t })
58}
59
72c7248b
C
60// ---------------------------------------------------------------------------
61
62export {
571389d4 63 createUserAccountAndChannel,
47e0652b 64 createLocalAccountWithoutKeys
72c7248b 65}