Search video channel handle/uri
[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 = await userToCreate.save(userOptions)
21     const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
22     userCreated.Account = accountCreated
23
24     let channelName = userCreated.username + '_channel'
25
26     // Conflict, generate uuid instead
27     const actor = await ActorModel.loadLocalByName(channelName)
28     if (actor) channelName = uuidv4()
29
30     const videoChannelDisplayName = `Main ${userCreated.username} channel`
31     const videoChannelInfo = {
32       name: channelName,
33       displayName: videoChannelDisplayName
34     }
35     const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
36
37     return { user: userCreated, account: accountCreated, videoChannel }
38   })
39
40   account.Actor = await setAsyncActorKeys(account.Actor)
41   videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
42
43   return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
44 }
45
46 async function createLocalAccountWithoutKeys (
47   name: string,
48   userId: number | null,
49   applicationId: number | null,
50   t: Sequelize.Transaction | undefined,
51   type: ActivityPubActorType= 'Person'
52 ) {
53   const url = getAccountActivityPubUrl(name)
54
55   const actorInstance = buildActorInstance(type, url, name)
56   const actorInstanceCreated = await actorInstance.save({ transaction: t })
57
58   const accountInstance = new AccountModel({
59     name,
60     userId,
61     applicationId,
62     actorId: actorInstanceCreated.id
63   } as FilteredModelAttributes<AccountModel>)
64
65   const accountInstanceCreated = await accountInstance.save({ transaction: t })
66   accountInstanceCreated.Actor = actorInstanceCreated
67
68   return accountInstanceCreated
69 }
70
71 async function createApplicationActor (applicationId: number) {
72   const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
73
74   accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
75
76   return accountCreated
77 }
78
79 // ---------------------------------------------------------------------------
80
81 export {
82   createApplicationActor,
83   createUserAccountAndChannel,
84   createLocalAccountWithoutKeys
85 }