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