]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Add script to parse log files
[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 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
C
27
28 return res
72c7248b
C
29}
30
571389d4
C
31async 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,
60862425 49 serverId: null // It is our server
571389d4
C
50 })
51
52 return accountInstance.save({ transaction: t })
53}
54
72c7248b
C
55// ---------------------------------------------------------------------------
56
57export {
571389d4
C
58 createUserAccountAndChannel,
59 createLocalAccount
72c7248b 60}