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(userToCreate.username, userToCreate.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 account.Actor = await setAsyncActorKeys(account.Actor)
41 videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor)
43 return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel }
46 async function createLocalAccountWithoutKeys (
48 userId: number | null,
49 applicationId: number | null,
50 t: Sequelize.Transaction | undefined,
51 type: ActivityPubActorType= 'Person'
53 const url = getAccountActivityPubUrl(name)
55 const actorInstance = buildActorInstance(type, url, name)
56 const actorInstanceCreated = await actorInstance.save({ transaction: t })
58 const accountInstance = new AccountModel({
62 actorId: actorInstanceCreated.id
63 } as FilteredModelAttributes<AccountModel>)
65 const accountInstanceCreated = await accountInstance.save({ transaction: t })
66 accountInstanceCreated.Actor = actorInstanceCreated
68 return accountInstanceCreated
71 async function createApplicationActor (applicationId: number) {
72 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application')
74 accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor)
79 // ---------------------------------------------------------------------------
82 createApplicationActor,
83 createUserAccountAndChannel,
84 createLocalAccountWithoutKeys