]>
Commit | Line | Data |
---|---|---|
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' | |
12 | ||
13 | async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) { | |
14 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
15 | const userOptions = { | |
16 | transaction: t, | |
17 | validate: validateUser | |
18 | } | |
19 | ||
20 | const userCreated = await userToCreate.save(userOptions) | |
21 | const accountCreated = await createLocalAccountWithoutKeys(userCreated.username, userCreated.id, null, t) | |
22 | userCreated.Account = accountCreated | |
23 | ||
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` | |
31 | const videoChannelInfo = { | |
32 | name: channelName, | |
33 | displayName: videoChannelDisplayName | |
34 | } | |
35 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) | |
36 | ||
37 | return { user: userCreated, account: accountCreated, videoChannel } | |
38 | }) | |
39 | ||
40 | const [ accountKeys, channelKeys ] = await Promise.all([ | |
41 | setAsyncActorKeys(account.Actor), | |
42 | setAsyncActorKeys(videoChannel.Actor) | |
43 | ]) | |
44 | ||
45 | account.Actor = accountKeys | |
46 | videoChannel.Actor = channelKeys | |
47 | ||
48 | return { user, account, videoChannel } as { user: UserModel, account: AccountModel, videoChannel: VideoChannelModel } | |
49 | } | |
50 | ||
51 | async function createLocalAccountWithoutKeys ( | |
52 | name: string, | |
53 | userId: number | null, | |
54 | applicationId: number | null, | |
55 | t: Sequelize.Transaction | undefined, | |
56 | type: ActivityPubActorType= 'Person' | |
57 | ) { | |
58 | const url = getAccountActivityPubUrl(name) | |
59 | ||
60 | const actorInstance = buildActorInstance(type, url, name) | |
61 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) | |
62 | ||
63 | const accountInstance = new AccountModel({ | |
64 | name, | |
65 | userId, | |
66 | applicationId, | |
67 | actorId: actorInstanceCreated.id | |
68 | } as FilteredModelAttributes<AccountModel>) | |
69 | ||
70 | const accountInstanceCreated = await accountInstance.save({ transaction: t }) | |
71 | accountInstanceCreated.Actor = actorInstanceCreated | |
72 | ||
73 | return accountInstanceCreated | |
74 | } | |
75 | ||
76 | async function createApplicationActor (applicationId: number) { | |
77 | const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application') | |
78 | ||
79 | accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor) | |
80 | ||
81 | return accountCreated | |
82 | } | |
83 | ||
84 | // --------------------------------------------------------------------------- | |
85 | ||
86 | export { | |
87 | createApplicationActor, | |
88 | createUserAccountAndChannel, | |
89 | createLocalAccountWithoutKeys | |
90 | } |