]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/user.ts
Begin moving video channel to actor
[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
9 async function createUserAccountAndChannel (user: UserModel, validateUser = true) {
10 const { account, videoChannel } = await sequelizeTypescript.transaction(async t => {
11 const userOptions = {
12 transaction: t,
13 validate: validateUser
14 }
15
16 const userCreated = await user.save(userOptions)
17 const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t)
18
19 const videoChannelName = `Default ${userCreated.username} channel`
20 const videoChannelInfo = {
21 name: videoChannelName
22 }
23 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
24
25 return { account: accountCreated, videoChannel }
26 })
27
28 account.Actor = await setAsyncActorKeys(account.Actor)
29 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
30
31 return { account, videoChannel }
32 }
33
34 async function createLocalAccountWithoutKeys (
35 name: string,
36 userId: number,
37 applicationId: number,
38 t: Sequelize.Transaction,
39 type: ActivityPubActorType= 'Person'
40 ) {
41 const url = getAccountActivityPubUrl(name)
42
43 const actorInstance = buildActorInstance(type, url, name)
44 const actorInstanceCreated = await actorInstance.save({ transaction: t })
45
46 const accountInstance = new AccountModel({
47 name,
48 userId,
49 applicationId,
50 actorId: actorInstanceCreated.id,
51 serverId: null // It is our server
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 }