]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
increase waitJobs pendingJobs timeout to 2000
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
CommitLineData
571389d4 1import * as Sequelize from 'sequelize'
8a19bee1 2import * as uuidv4 from 'uuid/v4'
50d6de9c
C
3import { ActivityPubActorType } from '../../shared/models/activitypub'
4import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
3fd3ab2d
C
5import { AccountModel } from '../models/account/account'
6import { UserModel } from '../models/account/user'
50d6de9c 7import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
72c7248b 8import { createVideoChannel } from './video-channel'
6b738c7a 9import { VideoChannelModel } from '../models/video/video-channel'
c1e791ba 10import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
8a19bee1 11import { ActorModel } from '../models/activitypub/actor'
72c7248b 12
f05a1c30
C
13async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
14 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
72c7248b
C
15 const userOptions = {
16 transaction: t,
17 validate: validateUser
18 }
19
d175a6f7
C
20 const userCreated = await userToCreate.save(userOptions)
21 const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t)
80e36cd9 22 userCreated.Account = accountCreated
f5028693 23
8a19bee1
C
24 let channelName = userCreated.username + '_channel'
25
26 // Conflict, generate uuid instead
27 const actor = await ActorModel.loadLocalByName(channelName)
28 if (actor) channelName = uuidv4()
29
30 const videoChannelDisplayName = `Main ${userCreated.username} channel`
f5028693 31 const videoChannelInfo = {
8a19bee1 32 name: channelName,
08c1efbe 33 displayName: videoChannelDisplayName
f5028693 34 }
e4f97bab 35 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
f5028693 36
f05a1c30 37 return { user: userCreated, account: accountCreated, videoChannel }
72c7248b 38 })
f5028693 39
0b2f03d3
C
40 const [ accountKeys, channelKeys ] = await Promise.all([
41 setAsyncActorKeys(account.Actor),
42 setAsyncActorKeys(videoChannel.Actor)
43 ])
44
45 account.Actor = accountKeys
46 videoChannel.Actor = channelKeys
47e0652b 47
6b738c7a 48 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
72c7248b
C
49}
50
50d6de9c
C
51async function createLocalAccountWithoutKeys (
52 name: string,
c1e791ba
RK
53 userId: number | null,
54 applicationId: number | null,
55 t: Sequelize.Transaction | undefined,
50d6de9c
C
56 type: ActivityPubActorType= 'Person'
57) {
54141398 58 const url = getAccountActivityPubUrl(name)
571389d4 59
50d6de9c 60 const actorInstance = buildActorInstance(type, url, name)
fadf619a
C
61 const actorInstanceCreated = await actorInstance.save({ transaction: t })
62
63 const accountInstance = new AccountModel({
64 name,
571389d4
C
65 userId,
66 applicationId,
c39ea24b 67 actorId: actorInstanceCreated.id
c1e791ba 68 } as FilteredModelAttributes<AccountModel>)
571389d4 69
fadf619a
C
70 const accountInstanceCreated = await accountInstance.save({ transaction: t })
71 accountInstanceCreated.Actor = actorInstanceCreated
72
73 return accountInstanceCreated
571389d4
C
74}
75
50d6de9c
C
76async function createApplicationActor (applicationId: number) {
77 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
78
79 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
80
81 return accountCreated
82}
83
72c7248b
C
84// ---------------------------------------------------------------------------
85
86export {
50d6de9c 87 createApplicationActor,
571389d4 88 createUserAccountAndChannel,
47e0652b 89 createLocalAccountWithoutKeys
72c7248b 90}