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