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'
9 async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
10 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
13 validate: validateUser
16 const userCreated = await userToCreate.save(userOptions)
17 const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
19 const videoChannelName = `Default ${userCreated.username} channel`
20 const videoChannelInfo = {
21 name: videoChannelName
23 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
25 return { user: userCreated, account: accountCreated, videoChannel }
28 account.Actor = await setAsyncActorKeys(account.Actor)
29 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
31 return { user, account, videoChannel }
34 async function createLocalAccountWithoutKeys (
37 applicationId: number,
38 t: Sequelize.Transaction,
39 type: ActivityPubActorType= 'Person'
41 const url = getAccountActivityPubUrl(name)
43 const actorInstance = buildActorInstance(type, url, name)
44 const actorInstanceCreated = await actorInstance.save({ transaction: t })
46 const accountInstance = new AccountModel({
50 actorId: actorInstanceCreated.id
53 const accountInstanceCreated = await accountInstance.save({ transaction: t })
54 accountInstanceCreated.Actor = actorInstanceCreated
56 return accountInstanceCreated
59 async function createApplicationActor (applicationId: number) {
60 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
62 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
67 // ---------------------------------------------------------------------------
70 createApplicationActor,
71 createUserAccountAndChannel,
72 createLocalAccountWithoutKeys