]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/user.ts
Refractor activity pub lib/helpers
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
CommitLineData
571389d4 1import * as Sequelize from 'sequelize'
571389d4 2import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
72c7248b 3import { database as db } from '../initializers'
571389d4 4import { CONFIG } from '../initializers/constants'
72c7248b 5import { UserInstance } from '../models'
72c7248b 6import { createVideoChannel } from './video-channel'
47e0652b 7import { logger } from '../helpers/logger'
54141398 8import { getAccountActivityPubUrl } from '../helpers/activitypub'
72c7248b 9
e4f97bab 10async function createUserAccountAndChannel (user: UserInstance, validateUser = true) {
47e0652b 11 const { account, videoChannel } = await db.sequelize.transaction(async t => {
72c7248b
C
12 const userOptions = {
13 transaction: t,
14 validate: validateUser
15 }
16
f5028693 17 const userCreated = await user.save(userOptions)
47e0652b 18 const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t)
f5028693 19
e34c85e5 20 const videoChannelName = `Default ${userCreated.username} channel`
f5028693 21 const videoChannelInfo = {
e34c85e5 22 name: videoChannelName
f5028693 23 }
e4f97bab 24 const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t)
f5028693 25
571389d4 26 return { account: accountCreated, videoChannel }
72c7248b 27 })
f5028693 28
47e0652b
C
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 }
72c7248b
C
36}
37
47e0652b 38async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
54141398 39 const url = getAccountActivityPubUrl(name)
571389d4
C
40
41 const accountInstance = db.Account.build({
42 name,
43 url,
47e0652b
C
44 publicKey: null,
45 privateKey: null,
571389d4
C
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,
60862425 55 serverId: null // It is our server
571389d4
C
56 })
57
58 return accountInstance.save({ transaction: t })
59}
60
72c7248b
C
61// ---------------------------------------------------------------------------
62
63export {
571389d4 64 createUserAccountAndChannel,
47e0652b 65 createLocalAccountWithoutKeys
72c7248b 66}