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