X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fuser.ts;h=2d7b36b4faae0e6dc380267ff78664db991df5c0;hb=eb8b27c93e61a896a08923dc1ca3c87ba8cf4948;hp=a92f4777bbdfa74ab4e56165f6d3827de8b3ce3a;hpb=f5028693a896a3076dd286ac0030e3d8f78f5ebf;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/user.ts b/server/lib/user.ts index a92f4777b..2d7b36b4f 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -1,42 +1,66 @@ +import * as Sequelize from 'sequelize' +import { getActivityPubUrl } from '../helpers/activitypub' +import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto' import { database as db } from '../initializers' +import { CONFIG } from '../initializers/constants' import { UserInstance } from '../models' -import { addVideoAuthorToFriends } from './friends' import { createVideoChannel } from './video-channel' +import { logger } from '../helpers/logger' -async function createUserAuthorAndChannel (user: UserInstance, validateUser = true) { - const res = await db.sequelize.transaction(async t => { +async function createUserAccountAndChannel (user: UserInstance, validateUser = true) { + const { account, videoChannel } = await db.sequelize.transaction(async t => { const userOptions = { transaction: t, validate: validateUser } const userCreated = await user.save(userOptions) - const authorInstance = db.Author.build({ - name: userCreated.username, - podId: null, // It is our pod - userId: userCreated.id - }) + const accountCreated = await createLocalAccountWithoutKeys(user.username, user.id, null, t) - const authorCreated = await authorInstance.save({ transaction: t }) + const videoChannelName = `Default ${userCreated.username} channel` + const videoChannelInfo = { + name: videoChannelName + } + const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) - const remoteVideoAuthor = authorCreated.toAddRemoteJSON() + return { account: accountCreated, videoChannel } + }) - // Now we'll add the video channel's meta data to our friends - const author = await addVideoAuthorToFriends(remoteVideoAuthor, t) + // Set account keys, this could be long so process after the account creation and do not block the client + const { publicKey, privateKey } = await createPrivateAndPublicKeys() + account.set('publicKey', publicKey) + account.set('privateKey', privateKey) + account.save().catch(err => logger.error('Cannot set public/private keys of local account %d.', account.id, err)) - const videoChannelInfo = { - name: `Default ${userCreated.username} channel` - } - const videoChannel = await createVideoChannel(videoChannelInfo, authorCreated, t) + return { account, videoChannel } +} + +async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) { + const url = getActivityPubUrl('account', name) - return { author, videoChannel } + const accountInstance = db.Account.build({ + name, + url, + publicKey: null, + privateKey: null, + followersCount: 0, + followingCount: 0, + inboxUrl: url + '/inbox', + outboxUrl: url + '/outbox', + sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox', + followersUrl: url + '/followers', + followingUrl: url + '/following', + userId, + applicationId, + serverId: null // It is our server }) - return res + return accountInstance.save({ transaction: t }) } // --------------------------------------------------------------------------- export { - createUserAuthorAndChannel + createUserAccountAndChannel, + createLocalAccountWithoutKeys }