]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channel-sync.ts
Cleanup useless express validator messages
[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 { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
7 import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models'
8 import { areValidationErrors, doesVideoChannelIdExist } from '../shared'
9 import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
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')
24 .custom(isUrlValid),
25
26 body('videoChannelId')
27 .isInt(),
28
29 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 logger.debug('Checking videoChannelSync parameters', { parameters: req.body })
31
32 if (areValidationErrors(req, res)) return
33
34 const body: VideoChannelSyncCreate = req.body
35 if (!await doesVideoChannelIdExist(body.videoChannelId, res)) return
36
37 const count = await VideoChannelSyncModel.countByAccount(res.locals.videoChannel.accountId)
38 if (count >= CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER) {
39 return res.fail({
40 message: `You cannot create more than ${CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER} channel synchronizations`
41 })
42 }
43
44 return next()
45 }
46 ]
47
48 export const ensureSyncExists = [
49 param('id').exists().isInt().withMessage('Should have an sync id'),
50
51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
52 if (areValidationErrors(req, res)) return
53
54 if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return
55 if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return
56
57 return next()
58 }
59 ]