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