]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/user.ts
Retry transactions on user endpoints if needed
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
... / ...
CommitLineData
1import * as Sequelize from 'sequelize'
2import * as uuidv4 from 'uuid/v4'
3import { ActivityPubActorType } from '../../shared/models/activitypub'
4import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
5import { AccountModel } from '../models/account/account'
6import { UserModel } from '../models/account/user'
7import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
8import { createVideoChannel } from './video-channel'
9import { VideoChannelModel } from '../models/video/video-channel'
10import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
11import { ActorModel } from '../models/activitypub/actor'
12
13async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
14 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
15 const userOptions = {
16 transaction: t,
17 validate: validateUser
18 }
19
20 const userCreated = await userToCreate.save(userOptions)
21 const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
22 userCreated.Account = accountCreated
23
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`
31 const videoChannelInfo = {
32 name: channelName,
33 displayName: videoChannelDisplayName
34 }
35 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
36
37 return { user: userCreated, account: accountCreated, videoChannel }
38 })
39
40 account.Actor = await setAsyncActorKeys(account.Actor)
41 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
42
43 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
44}
45
46async function createLocalAccountWithoutKeys (
47 name: string,
48 userId: number | null,
49 applicationId: number | null,
50 t: Sequelize.Transaction | undefined,
51 type: ActivityPubActorType= 'Person'
52) {
53 const url = getAccountActivityPubUrl(name)
54
55 const actorInstance = buildActorInstance(type, url, name)
56 const actorInstanceCreated = await actorInstance.save({ transaction: t })
57
58 const accountInstance = new AccountModel({
59 name,
60 userId,
61 applicationId,
62 actorId: actorInstanceCreated.id
63 } as FilteredModelAttributes<AccountModel>)
64
65 const accountInstanceCreated = await accountInstance.save({ transaction: t })
66 accountInstanceCreated.Actor = actorInstanceCreated
67
68 return accountInstanceCreated
69}
70
71async function createApplicationActor (applicationId: number) {
72 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
73
74 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
75
76 return accountCreated
77}
78
79// ---------------------------------------------------------------------------
80
81export {
82 createApplicationActor,
83 createUserAccountAndChannel,
84 createLocalAccountWithoutKeys
85}