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'
9 import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model'
11 async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) {
12 const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
15 validate: validateUser
18 const userCreated = await userToCreate.save(userOptions)
19 const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t)
21 const videoChannelDisplayName = `Default ${userCreated.username} channel`
22 const videoChannelInfo = {
23 displayName: videoChannelDisplayName
25 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
27 return { user: userCreated, account: accountCreated, videoChannel }
30 account.Actor = await setAsyncActorKeys(account.Actor)
31 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
33 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
36 async function createLocalAccountWithoutKeys (
38 userId: number | null,
39 applicationId: number | null,
40 t: Sequelize.Transaction | undefined,
41 type: ActivityPubActorType= 'Person'
43 const url = getAccountActivityPubUrl(name)
45 const actorInstance = buildActorInstance(type, url, name)
46 const actorInstanceCreated = await actorInstance.save({ transaction: t })
48 const accountInstance = new AccountModel({
52 actorId: actorInstanceCreated.id
53 } as FilteredModelAttributes<AccountModel>)
55 const accountInstanceCreated = await accountInstance.save({ transaction: t })
56 accountInstanceCreated.Actor = actorInstanceCreated
58 return accountInstanceCreated
61 async function createApplicationActor (applicationId: number) {
62 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
64 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
69 // ---------------------------------------------------------------------------
72 createApplicationActor,
73 createUserAccountAndChannel,
74 createLocalAccountWithoutKeys