Add audit logs in various modules
[oweals/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     userCreated.Account = accountCreated
21
22     const videoChannelDisplayName = `Default ${userCreated.username} channel`
23     const videoChannelInfo = {
24       displayName: videoChannelDisplayName
25     }
26     const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
27
28     return { user: userCreated, account: accountCreated, videoChannel }
29   })
30
31   account.Actor = await setAsyncActorKeys(account.Actor)
32   videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
33
34   return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
35 }
36
37 async function createLocalAccountWithoutKeys (
38   name: string,
39   userId: number | null,
40   applicationId: number | null,
41   t: Sequelize.Transaction | undefined,
42   type: ActivityPubActorType= 'Person'
43 ) {
44   const url = getAccountActivityPubUrl(name)
45
46   const actorInstance = buildActorInstance(type, url, name)
47   const actorInstanceCreated = await actorInstance.save({ transaction: t })
48
49   const accountInstance = new AccountModel({
50     name,
51     userId,
52     applicationId,
53     actorId: actorInstanceCreated.id
54   } as FilteredModelAttributes<AccountModel>)
55
56   const accountInstanceCreated = await accountInstance.save({ transaction: t })
57   accountInstanceCreated.Actor = actorInstanceCreated
58
59   return accountInstanceCreated
60 }
61
62 async function createApplicationActor (applicationId: number) {
63   const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
64
65   accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
66
67   return accountCreated
68 }
69
70 // ---------------------------------------------------------------------------
71
72 export {
73   createApplicationActor,
74   createUserAccountAndChannel,
75   createLocalAccountWithoutKeys
76 }