]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channel-sync.ts
Bumped to version v5.2.1
[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 { CONFIG } from '@server/initializers/config'
5 import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
6 import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models'
7 import { areValidationErrors, doesVideoChannelIdExist } from '../shared'
8 import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
9
10 export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => {
11 if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
12 return res.fail({
13 status: HttpStatusCode.FORBIDDEN_403,
14 message: 'Synchronization is impossible as video channel synchronization is not enabled on the server'
15 })
16 }
17
18 return next()
19 }
20
21 export const videoChannelSyncValidator = [
22 body('externalChannelUrl')
23 .custom(isUrlValid),
24
25 body('videoChannelId')
26 .isInt(),
27
28 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
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 if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return
52 if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return
53
54 return next()
55 }
56 ]