Use async/await in lib and initializers
[oweals/peertube.git] / server / lib / user.ts
1 import { database as db } from '../initializers'
2 import { UserInstance } from '../models'
3 import { addVideoAuthorToFriends } from './friends'
4 import { createVideoChannel } from './video-channel'
5
6 async function createUserAuthorAndChannel (user: UserInstance, validateUser = true) {
7   const res = await db.sequelize.transaction(async t => {
8     const userOptions = {
9       transaction: t,
10       validate: validateUser
11     }
12
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 }
33   })
34
35   return res
36 }
37
38 // ---------------------------------------------------------------------------
39
40 export {
41   createUserAuthorAndChannel
42 }