1 import * as Sequelize from 'sequelize'
2 import * as uuidv4 from 'uuid/v4'
3 import { ActivityPubActorType } from '../../shared/models/activitypub'
4 import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers'
5 import { AccountModel } from '../models/account/account'
6 import { UserModel } from '../models/account/user'
7 import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub'
8 import { createVideoChannel } from './video-channel'
9 import { VideoChannelModel } from '../models/video/video-channel'
10 import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
11 import { ActorModel } from '../models/activitypub/actor'
13 async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
14 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
17 validate: validateUser
20 const userCreated = await userToCreate.save(userOptions)
21 const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t)
22 userCreated.Account = accountCreated
24 let channelName = userCreated.username + '_channel'
26 // Conflict, generate uuid instead
27 const actor = await ActorModel.loadLocalByName(channelName)
28 if (actor) channelName = uuidv4()
30 const videoChannelDisplayName = `Main ${userCreated.username} channel`
31 const videoChannelInfo = {
33 displayName: videoChannelDisplayName
35 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
37 return { user: userCreated, account: accountCreated, videoChannel }
40 const [ accountKeys, channelKeys ] = await Promise.all([
41 setAsyncActorKeys(account.Actor),
42 setAsyncActorKeys(videoChannel.Actor)
45 account.Actor = accountKeys
46 videoChannel.Actor = channelKeys
48 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
51 async function createLocalAccountWithoutKeys (
53 userId: number | null,
54 applicationId: number | null,
55 t: Sequelize.Transaction | undefined,
56 type: ActivityPubActorType= 'Person'
58 const url = getAccountActivityPubUrl(name)
60 const actorInstance = buildActorInstance(type, url, name)
61 const actorInstanceCreated = await actorInstance.save({ transaction: t })
63 const accountInstance = new AccountModel({
67 actorId: actorInstanceCreated.id
68 } as FilteredModelAttributes<AccountModel>)
70 const accountInstanceCreated = await accountInstance.save({ transaction: t })
71 accountInstanceCreated.Actor = actorInstanceCreated
73 return accountInstanceCreated
76 async function createApplicationActor (applicationId: number) {
77 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
79 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
84 // ---------------------------------------------------------------------------
87 createApplicationActor,
88 createUserAccountAndChannel,
89 createLocalAccountWithoutKeys