]>
Commit | Line | Data |
---|---|---|
1 | import * as Sequelize from 'sequelize' | |
2 | import { createPrivateAndPublicKeys, logger } from '../helpers' | |
3 | import { CONFIG, sequelizeTypescript } from '../initializers' | |
4 | import { AccountModel } from '../models/account/account' | |
5 | import { UserModel } from '../models/account/user' | |
6 | import { getAccountActivityPubUrl } from './activitypub' | |
7 | import { createVideoChannel } from './video-channel' | |
8 | ||
9 | async function createUserAccountAndChannel (user: UserModel, validateUser = true) { | |
10 | const { account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
11 | const userOptions = { | |
12 | transaction: t, | |
13 | validate: validateUser | |
14 | } | |
15 | ||
16 | const userCreated = await user.save(userOptions) | |
17 | const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t) | |
18 | ||
19 | const videoChannelName = `Default ${userCreated.username} channel` | |
20 | const videoChannelInfo = { | |
21 | name: videoChannelName | |
22 | } | |
23 | const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) | |
24 | ||
25 | return { account: accountCreated, videoChannel } | |
26 | }) | |
27 | ||
28 | // Set account keys, this could be long so process after the account creation and do not block the client | |
29 | const { publicKey, privateKey } = await createPrivateAndPublicKeys() | |
30 | account.set('publicKey', publicKey) | |
31 | account.set('privateKey', privateKey) | |
32 | account.save().catch(err => logger.error('Cannot set public/private keys of local account %d.', account.id, err)) | |
33 | ||
34 | return { account, videoChannel } | |
35 | } | |
36 | ||
37 | async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) { | |
38 | const url = getAccountActivityPubUrl(name) | |
39 | ||
40 | const accountInstance = new AccountModel({ | |
41 | name, | |
42 | url, | |
43 | publicKey: null, | |
44 | privateKey: null, | |
45 | followersCount: 0, | |
46 | followingCount: 0, | |
47 | inboxUrl: url + '/inbox', | |
48 | outboxUrl: url + '/outbox', | |
49 | sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox', | |
50 | followersUrl: url + '/followers', | |
51 | followingUrl: url + '/following', | |
52 | userId, | |
53 | applicationId, | |
54 | serverId: null // It is our server | |
55 | }) | |
56 | ||
57 | return accountInstance.save({ transaction: t }) | |
58 | } | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | export { | |
63 | createUserAccountAndChannel, | |
64 | createLocalAccountWithoutKeys | |
65 | } |