]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/user.ts
Enh #106 : Add an autoPlayVideo user attribute (#159)
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
... / ...
CommitLineData
1import * as Sequelize from 'sequelize'
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'
7import { createVideoChannel } from './video-channel'
8
9async function createUserAccountAndChannel (user: UserModel, validateUser = true) {
10 const { account, videoChannel } = await sequelizeTypescript.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 createLocalAccountWithoutKeys(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 // 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 }
35}
36
37async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
38 const url = getAccountActivityPubUrl(name)
39
40 const accountInstance = new AccountModel({
41 name,
42 url,
43 publicKey: null,
44 privateKey: null,
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,
54 serverId: null // It is our server
55 })
56
57 return accountInstance.save({ transaction: t })
58}
59
60// ---------------------------------------------------------------------------
61
62export {
63 createUserAccountAndChannel,
64 createLocalAccountWithoutKeys
65}