]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channel-sync.ts
Add missing job types to admin panel
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channel-sync.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4 import { logger } from '@server/helpers/logger'
5 import { CONFIG } from '@server/initializers/config'
6 import { VideoChannelModel } from '@server/models/video/video-channel'
7 import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
8 import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models'
9 import { areValidationErrors, doesVideoChannelIdExist } from '../shared'
10
11 export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
13 return res.fail({
14 status: HttpStatusCode.FORBIDDEN_403,
15 message: 'Synchronization is impossible as video channel synchronization is not enabled on the server'
16 })
17 }
18
19 return next()
20 }
21
22 export const videoChannelSyncValidator = [
23 body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'),
24 body('videoChannelId').isInt().withMessage('Should have a valid video channel id'),
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoChannelSync parameters', { parameters: req.body })
28
29 if (areValidationErrors(req, res)) return
30
31 const body: VideoChannelSyncCreate = req.body
32 if (!await doesVideoChannelIdExist(body.videoChannelId, res)) return
33
34 const count = await VideoChannelSyncModel.countByAccount(res.locals.videoChannel.accountId)
35 if (count >= CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER) {
36 return res.fail({
37 message: `You cannot create more than ${CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER} channel synchronizations`
38 })
39 }
40
41 return next()
42 }
43 ]
44
45 export const ensureSyncExists = [
46 param('id').exists().isInt().withMessage('Should have an sync id'),
47
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 if (areValidationErrors(req, res)) return
50
51 const syncId = parseInt(req.params.id, 10)
52 const sync = await VideoChannelSyncModel.loadWithChannel(syncId)
53
54 if (!sync) {
55 return res.fail({
56 status: HttpStatusCode.NOT_FOUND_404,
57 message: 'Synchronization not found'
58 })
59 }
60
61 res.locals.videoChannelSync = sync
62 res.locals.videoChannel = await VideoChannelModel.loadAndPopulateAccount(sync.videoChannelId)
63
64 return next()
65 }
66 ]