]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/user.ts
ac5f55260ea5b68e290b463cbcd072f13578ff30
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
1 import * as Sequelize from 'sequelize'
2 import { ActivityPubActorType } from '../../shared/models/activitypub'
3 import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
4 import { AccountModel } from '../models/account/account'
5 import { UserModel } from '../models/account/user'
6 import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
7 import { createVideoChannel } from './video-channel'
8 import { VideoChannelModel } from '../models/video/video-channel'
9 import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
10
11 async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
12 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
13 const userOptions = {
14 transaction: t,
15 validate: validateUser
16 }
17
18 const userCreated = await userToCreate.save(userOptions)
19 const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
20
21 const videoChannelDisplayName = `Default ${userCreated.username} channel`
22 const videoChannelInfo = {
23 displayName: videoChannelDisplayName
24 }
25 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
26
27 return { user: userCreated, account: accountCreated, videoChannel }
28 })
29
30 account.Actor = await setAsyncActorKeys(account.Actor)
31 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
32
33 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
34 }
35
36 async function createLocalAccountWithoutKeys (
37 name: string,
38 userId: number | null,
39 applicationId: number | null,
40 t: Sequelize.Transaction | undefined,
41 type: ActivityPubActorType= 'Person'
42 ) {
43 const url = getAccountActivityPubUrl(name)
44
45 const actorInstance = buildActorInstance(type, url, name)
46 const actorInstanceCreated = await actorInstance.save({ transaction: t })
47
48 const accountInstance = new AccountModel({
49 name,
50 userId,
51 applicationId,
52 actorId: actorInstanceCreated.id
53 } as FilteredModelAttributes<AccountModel>)
54
55 const accountInstanceCreated = await accountInstance.save({ transaction: t })
56 accountInstanceCreated.Actor = actorInstanceCreated
57
58 return accountInstanceCreated
59 }
60
61 async function createApplicationActor (applicationId: number) {
62 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
63
64 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
65
66 return accountCreated
67 }
68
69 // ---------------------------------------------------------------------------
70
71 export {
72 createApplicationActor,
73 createUserAccountAndChannel,
74 createLocalAccountWithoutKeys
75 }