aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/user.ts
blob: a92f4777bbdfa74ab4e56165f6d3827de8b3ce3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { database as db } from '../initializers'
import { UserInstance } from '../models'
import { addVideoAuthorToFriends } from './friends'
import { createVideoChannel } from './video-channel'

async function createUserAuthorAndChannel (user: UserInstance, validateUser = true) {
  const res = await db.sequelize.transaction(async t => {
    const userOptions = {
      transaction: t,
      validate: validateUser
    }

    const userCreated = await user.save(userOptions)
    const authorInstance = db.Author.build({
      name: userCreated.username,
      podId: null, // It is our pod
      userId: userCreated.id
    })

    const authorCreated = await authorInstance.save({ transaction: t })

    const remoteVideoAuthor = authorCreated.toAddRemoteJSON()

    // Now we'll add the video channel's meta data to our friends
    const author = await addVideoAuthorToFriends(remoteVideoAuthor, t)

    const videoChannelInfo = {
      name: `Default ${userCreated.username} channel`
    }
    const videoChannel = await createVideoChannel(videoChannelInfo, authorCreated, t)

    return { author, videoChannel }
  })

  return res
}

// ---------------------------------------------------------------------------

export {
  createUserAuthorAndChannel
}