]>
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 C |
7 | import { createVideoChannel } from './video-channel' |
8 | ||
f05a1c30 C |
9 | async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) { |
10 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
72c7248b C |
11 | const userOptions = { |
12 | transaction: t, | |
13 | validate: validateUser | |
14 | } | |
15 | ||
f05a1c30 C |
16 | const userCreated = await userToCreate.save(userOptions) |
17 | const accountCreated = await createLocalAccountWithoutKeys(userToCreate.username, userToCreate.id, null, t) | |
f5028693 | 18 | |
e34c85e5 | 19 | const videoChannelName = `Default ${userCreated.username} channel` |
f5028693 | 20 | const videoChannelInfo = { |
e34c85e5 | 21 | name: videoChannelName |
f5028693 | 22 | } |
e4f97bab | 23 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) |
f5028693 | 24 | |
f05a1c30 | 25 | return { user: userCreated, account: accountCreated, videoChannel } |
72c7248b | 26 | }) |
f5028693 | 27 | |
50d6de9c C |
28 | account.Actor = await setAsyncActorKeys(account.Actor) |
29 | videoChannel.Actor = await setAsyncActorKeys(videoChannel.Actor) | |
47e0652b | 30 | |
f05a1c30 | 31 | return { user, account, videoChannel } |
72c7248b C |
32 | } |
33 | ||
50d6de9c C |
34 | async function createLocalAccountWithoutKeys ( |
35 | name: string, | |
36 | userId: number, | |
37 | applicationId: number, | |
38 | t: Sequelize.Transaction, | |
39 | type: ActivityPubActorType= 'Person' | |
40 | ) { | |
54141398 | 41 | const url = getAccountActivityPubUrl(name) |
571389d4 | 42 | |
50d6de9c | 43 | const actorInstance = buildActorInstance(type, url, name) |
fadf619a C |
44 | const actorInstanceCreated = await actorInstance.save({ transaction: t }) |
45 | ||
46 | const accountInstance = new AccountModel({ | |
47 | name, | |
571389d4 C |
48 | userId, |
49 | applicationId, | |
fadf619a | 50 | actorId: actorInstanceCreated.id, |
60862425 | 51 | serverId: null // It is our server |
571389d4 C |
52 | }) |
53 | ||
fadf619a C |
54 | const accountInstanceCreated = await accountInstance.save({ transaction: t }) |
55 | accountInstanceCreated.Actor = actorInstanceCreated | |
56 | ||
57 | return accountInstanceCreated | |
571389d4 C |
58 | } |
59 | ||
50d6de9c C |
60 | async function createApplicationActor (applicationId: number) { |
61 | const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationId, undefined, 'Application') | |
62 | ||
63 | accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor) | |
64 | ||
65 | return accountCreated | |
66 | } | |
67 | ||
72c7248b C |
68 | // --------------------------------------------------------------------------- |
69 | ||
70 | export { | |
50d6de9c | 71 | createApplicationActor, |
571389d4 | 72 | createUserAccountAndChannel, |
47e0652b | 73 | createLocalAccountWithoutKeys |
72c7248b | 74 | } |