]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Use async/await in lib and initializers
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
CommitLineData
72c7248b
C
1import { database as db } from '../initializers'
2import { UserInstance } from '../models'
3import { addVideoAuthorToFriends } from './friends'
4import { createVideoChannel } from './video-channel'
5
f5028693
C
6async function createUserAuthorAndChannel (user: UserInstance, validateUser = true) {
7 const res = await db.sequelize.transaction(async t => {
72c7248b
C
8 const userOptions = {
9 transaction: t,
10 validate: validateUser
11 }
12
f5028693
C
13 const userCreated = await user.save(userOptions)
14 const authorInstance = db.Author.build({
15 name: userCreated.username,
16 podId: null, // It is our pod
17 userId: userCreated.id
18 })
19
20 const authorCreated = await authorInstance.save({ transaction: t })
21
22 const remoteVideoAuthor = authorCreated.toAddRemoteJSON()
23
24 // Now we'll add the video channel's meta data to our friends
25 const author = await addVideoAuthorToFriends(remoteVideoAuthor, t)
26
27 const videoChannelInfo = {
28 name: `Default ${userCreated.username} channel`
29 }
30 const videoChannel = await createVideoChannel(videoChannelInfo, authorCreated, t)
31
32 return { author, videoChannel }
72c7248b 33 })
f5028693
C
34
35 return res
72c7248b
C
36}
37
38// ---------------------------------------------------------------------------
39
40export {
41 createUserAuthorAndChannel
42}