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'
10 async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
11 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
14 validate: validateUser
17 const userCreated = await userToCreate.save(userOptions)
18 const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
20 const videoChannelDisplayName = `Default ${userCreated.username} channel`
21 const videoChannelInfo = {
22 displayName: videoChannelDisplayName
24 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
26 return { user: userCreated, account: accountCreated, videoChannel }
29 account.Actor = await setAsyncActorKeys(account.Actor)
30 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
32 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
35 async function createLocalAccountWithoutKeys (
38 applicationId: number,
39 t: Sequelize.Transaction,
40 type: ActivityPubActorType= 'Person'
42 const url = getAccountActivityPubUrl(name)
44 const actorInstance = buildActorInstance(type, url, name)
45 const actorInstanceCreated = await actorInstance.save({ transaction: t })
47 const accountInstance = new AccountModel({
51 actorId: actorInstanceCreated.id
54 const accountInstanceCreated = await accountInstance.save({ transaction: t })
55 accountInstanceCreated.Actor = actorInstanceCreated
57 return accountInstanceCreated
60 async function createApplicationActor (applicationId: number) {
61 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
63 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
68 // ---------------------------------------------------------------------------
71 createApplicationActor,
72 createUserAccountAndChannel,
73 createLocalAccountWithoutKeys