More robust actor keys creation

This commit is contained in:
Chocobozzz
2026-07-13 15:59:18 +02:00
parent 83074e6b91
commit 172304686d
3 changed files with 16 additions and 24 deletions
+1 -6
View File
@@ -1,8 +1,7 @@
import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto.js'
import { MActor } from '@server/types/models/index.js'
// Set account keys, this could be long so process after the account creation and do not block the client
async function generateAndSaveActorKeys <T extends MActor> (actor: T) {
export async function generateAndSaveActorKeys<T extends MActor> (actor: T) {
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
actor.publicKey = publicKey
@@ -10,7 +9,3 @@ async function generateAndSaveActorKeys <T extends MActor> (actor: T) {
return actor.save()
}
export {
generateAndSaveActorKeys
}
@@ -4,7 +4,7 @@ import { ActorModel } from '@server/models/actor/actor.js'
import { ActorKeysPayload } from '@peertube/peertube-models'
import { logger } from '../../../helpers/logger.js'
async function processActorKeys (job: Job) {
export async function processActorKeys (job: Job) {
const payload = job.data as ActorKeysPayload
logger.info('Processing actor keys in job %s.', job.id)
@@ -12,9 +12,3 @@ async function processActorKeys (job: Job) {
await generateAndSaveActorKeys(actor)
}
// ---------------------------------------------------------------------------
export {
processActorKeys
}
+14 -11
View File
@@ -26,6 +26,7 @@ import { buildActorInstance, findAvailableLocalActorName } from './local-actor.j
import { Redis } from './redis.js'
import { createLocalVideoChannelWithoutKeys } from './video-channel.js'
import { createWatchLaterPlaylist } from './video-playlist.js'
import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto.js'
type ChannelNames = { name: string, displayName: string }
@@ -91,7 +92,10 @@ export async function createUserAccountAndChannelAndPlaylist (parameters: {
}): Promise<{ user: MUserDefault, account: MAccountDefault, videoChannel: MChannelActor }> {
const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters
const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
const accountKeys = await createPrivateAndPublicKeys()
const channelKeys = await createPrivateAndPublicKeys()
return sequelizeTypescript.transaction(async t => {
const userOptions = {
transaction: t,
validate: validateUser
@@ -107,25 +111,23 @@ export async function createUserAccountAndChannelAndPlaylist (parameters: {
applicationId: null,
t
})
accountCreated.Actor.publicKey = accountKeys.publicKey
accountCreated.Actor.privateKey = accountKeys.privateKey
await accountCreated.Actor.save({ transaction: t })
userCreated.Account = accountCreated
const channelAttributes = await buildChannelAttributes({ user: userCreated, transaction: t, channelNames })
const videoChannel = await createLocalVideoChannelWithoutKeys(channelAttributes, accountCreated, t)
videoChannel.Actor.publicKey = channelKeys.publicKey
videoChannel.Actor.privateKey = channelKeys.privateKey
await videoChannel.Actor.save({ transaction: t })
const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
})
const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([
generateAndSaveActorKeys(account.Actor),
generateAndSaveActorKeys(videoChannel.Actor)
])
account.Actor = accountActorWithKeys
videoChannel.Actor = channelActorWithKeys
return { user, account, videoChannel }
}
export async function createLocalAccountWithoutKeys (parameters: {
@@ -147,6 +149,7 @@ export async function createLocalAccountWithoutKeys (parameters: {
const url = getLocalAccountActivityPubUrl(name)
const actor = buildActorInstance(type, url, name)
actor.accountId = account.id
await actor.save({ transaction: t })
return Object.assign(account, { Actor: actor })