aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/user.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/user.ts')
-rw-r--r--server/lib/user.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/server/lib/user.ts b/server/lib/user.ts
new file mode 100644
index 000000000..8609e72d8
--- /dev/null
+++ b/server/lib/user.ts
@@ -0,0 +1,46 @@
1import { database as db } from '../initializers'
2import { UserInstance } from '../models'
3import { addVideoAuthorToFriends } from './friends'
4import { createVideoChannel } from './video-channel'
5
6function 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
44export {
45 createUserAuthorAndChannel
46}