]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/user.ts
Refractor activity pub lib/helpers
[github/Chocobozzz/PeerTube.git] / server / lib / user.ts
index a92f4777bbdfa74ab4e56165f6d3827de8b3ce3a..d54ffc916d34bee45960655a06ddde8370229361 100644 (file)
@@ -1,42 +1,66 @@
+import * as Sequelize from 'sequelize'
+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'
+import { getAccountActivityPubUrl } from '../helpers/activitypub'
 
-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 = getAccountActivityPubUrl(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
 }