Speedup peertube startup
[oweals/peertube.git] / server / lib / user.ts
1 import * as Sequelize from 'sequelize'
2 import * as uuidv4 from 'uuid/v4'
3 import { ActivityPubActorType } from '../../shared/models/activitypub'
4 import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
5 import { AccountModel } from '../models/account/account'
6 import { UserModel } from '../models/account/user'
7 import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
8 import { createVideoChannel } from './video-channel'
9 import { VideoChannelModel } from '../models/video/video-channel'
10 import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
11 import { ActorModel } from '../models/activitypub/actor'
12
13 async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
14   const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
15     const userOptions = {
16       transaction: t,
17       validate: validateUser
18     }
19
20     const [ userCreated, accountCreated ] = await Promise.all([
21       userToCreate.save(userOptions),
22       createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
23     ])
24     userCreated.Account = accountCreated
25
26     let channelName = userCreated.username + '_channel'
27
28     // Conflict, generate uuid instead
29     const actor = await ActorModel.loadLocalByName(channelName)
30     if (actor) channelName = uuidv4()
31
32     const videoChannelDisplayName = `Main ${userCreated.username} channel`
33     const videoChannelInfo = {
34       name: channelName,
35       displayName: videoChannelDisplayName
36     }
37     const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
38
39     return { user: userCreated, account: accountCreated, videoChannel }
40   })
41
42   const [ accountKeys, channelKeys ] = await Promise.all([
43     setAsyncActorKeys(account.Actor),
44     setAsyncActorKeys(videoChannel.Actor)
45   ])
46
47   account.Actor = accountKeys
48   videoChannel.Actor = channelKeys
49
50   return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
51 }
52
53 async function createLocalAccountWithoutKeys (
54   name: string,
55   userId: number | null,
56   applicationId: number | null,
57   t: Sequelize.Transaction | undefined,
58   type: ActivityPubActorType= 'Person'
59 ) {
60   const url = getAccountActivityPubUrl(name)
61
62   const actorInstance = buildActorInstance(type, url, name)
63   const actorInstanceCreated = await actorInstance.save({ transaction: t })
64
65   const accountInstance = new AccountModel({
66     name,
67     userId,
68     applicationId,
69     actorId: actorInstanceCreated.id
70   } as FilteredModelAttributes<AccountModel>)
71
72   const accountInstanceCreated = await accountInstance.save({ transaction: t })
73   accountInstanceCreated.Actor = actorInstanceCreated
74
75   return accountInstanceCreated
76 }
77
78 async function createApplicationActor (applicationId: number) {
79   const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
80
81   accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
82
83   return accountCreated
84 }
85
86 // ---------------------------------------------------------------------------
87
88 export {
89   createApplicationActor,
90   createUserAccountAndChannel,
91   createLocalAccountWithoutKeys
92 }