]>
Commit | Line | Data |
---|---|---|
571389d4 | 1 | import * as Sequelize from 'sequelize' |
50d6de9c C |
2 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
3 | import { sequelizeTypescript, SERVER_ACTOR_NAME } from '../initializers' | |
3fd3ab2d C |
4 | import { AccountModel } from '../models/account/account' |
5 | import { UserModel } from '../models/account/user' | |
50d6de9c | 6 | import { buildActorInstance, getAccountActivityPubUrl, setAsyncActorKeys } from './activitypub' |
72c7248b | 7 | import { createVideoChannel } from './video-channel' |
6b738c7a | 8 | import { VideoChannelModel } from '../models/video/video-channel' |
c1e791ba | 9 | import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model' |
72c7248b | 10 | |
f05a1c30 C |
11 | async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) { |
12 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
72c7248b C |
13 | const userOptions = { |
14 | transaction: t, | |
15 | validate: validateUser | |
16 | } | |
17 | ||
f05a1c30 C |
18 | const userCreated = await userToCreate.save(userOptions) |
19 | const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t) | |
80e36cd9 | 20 | userCreated.Account = accountCreated |
f5028693 | 21 | |
08c1efbe | 22 | const videoChannelDisplayName = `Default ${userCreated.username} channel` |
f5028693 | 23 | const videoChannelInfo = { |
08c1efbe | 24 | displayName: videoChannelDisplayName |
f5028693 | 25 | } |
e4f97bab | 26 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) |
f5028693 | 27 | |
f05a1c30 | 28 | return { user: userCreated, account: accountCreated, videoChannel } |
72c7248b | 29 | }) |
f5028693 | 30 | |
50d6de9c C |
31 | account.Actor = await setAsyncActorKeys(account.Actor) |
32 | videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor) | |
47e0652b | 33 | |
6b738c7a | 34 | return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } |
72c7248b C |
35 | } |
36 | ||
50d6de9c C |
37 | async function createLocalAccountWithoutKeys ( |
38 | name: string, | |
c1e791ba RK |
39 | userId: number | null, |
40 | applicationId: number | null, | |
41 | t: Sequelize.Transaction | undefined, | |
50d6de9c C |
42 | type: ActivityPubActorType= 'Person' |
43 | ) { | |
54141398 | 44 | const url = getAccountActivityPubUrl(name) |
571389d4 | 45 | |
50d6de9c | 46 | const actorInstance = buildActorInstance(type, url, name) |
fadf619a C |
47 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) |
48 | ||
49 | const accountInstance = new AccountModel({ | |
50 | name, | |
571389d4 C |
51 | userId, |
52 | applicationId, | |
c39ea24b | 53 | actorId: actorInstanceCreated.id |
c1e791ba | 54 | } as FilteredModelAttributes<AccountModel>) |
571389d4 | 55 | |
fadf619a C |
56 | const accountInstanceCreated = await accountInstance.save({ transaction: t }) |
57 | accountInstanceCreated.Actor = actorInstanceCreated | |
58 | ||
59 | return accountInstanceCreated | |
571389d4 C |
60 | } |
61 | ||
50d6de9c C |
62 | async function createApplicationActor (applicationId: number) { |
63 | const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application') | |
64 | ||
65 | accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor) | |
66 | ||
67 | return accountCreated | |
68 | } | |
69 | ||
72c7248b C |
70 | // --------------------------------------------------------------------------- |
71 | ||
72 | export { | |
50d6de9c | 73 | createApplicationActor, |
571389d4 | 74 | createUserAccountAndChannel, |
47e0652b | 75 | createLocalAccountWithoutKeys |
72c7248b | 76 | } |