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)
20 userCreated.Account = accountCreated
22 const videoChannelDisplayName = `Default ${userCreated.username} channel`
23 const videoChannelInfo = {
24 displayName: videoChannelDisplayName
26 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
28 return { user: userCreated, account: accountCreated, videoChannel }
31 account.Actor = await setAsyncActorKeys(account.Actor)
32 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
34 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
37 async function createLocalAccountWithoutKeys (
39 userId: number | null,
40 applicationId: number | null,
41 t: Sequelize.Transaction | undefined,
42 type: ActivityPubActorType= 'Person'
44 const url = getAccountActivityPubUrl(name)
46 const actorInstance = buildActorInstance(type, url, name)
47 const actorInstanceCreated = await actorInstance.save({ transaction: t })
49 const accountInstance = new AccountModel({
53 actorId: actorInstanceCreated.id
54 } as FilteredModelAttributes<AccountModel>)
56 const accountInstanceCreated = await accountInstance.save({ transaction: t })
57 accountInstanceCreated.Actor = actorInstanceCreated
59 return accountInstanceCreated
62 async function createApplicationActor (applicationId: number) {
63 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
65 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
70 // ---------------------------------------------------------------------------
73 createApplicationActor,
74 createUserAccountAndChannel,
75 createLocalAccountWithoutKeys