8609e72d8ea1611b86347719f2e8b53544810e61
[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 function createUserAuthorAndChannel (user: UserInstance, validateUser = true) {
7   return db.sequelize.transaction(t => {
8     const userOptions = {
9       transaction: t,
10       validate: validateUser
11     }
12
13     return user.save(userOptions)
14       .then(user => {
15         const author = db.Author.build({
16           name: user.username,
17           podId: null, // It is our pod
18           userId: user.id
19         })
20
21         return author.save({ transaction: t })
22           .then(author => ({ author, user }))
23       })
24       .then(({ author, user }) => {
25         const remoteVideoAuthor = author.toAddRemoteJSON()
26
27         // Now we'll add the video channel's meta data to our friends
28         return addVideoAuthorToFriends(remoteVideoAuthor, t)
29           .then(() => ({ author, user }))
30       })
31       .then(({ author, user }) => {
32         const videoChannelInfo = {
33           name: `Default ${user.username} channel`
34         }
35
36         return createVideoChannel(videoChannelInfo, author, t)
37           .then(videoChannel => ({ author, user, videoChannel }))
38       })
39   })
40 }
41
42 // ---------------------------------------------------------------------------
43
44 export {
45   createUserAuthorAndChannel
46 }