]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/videos/video-channel-sync.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channel-sync.ts
index b184982431c6efe3c5851f4038ddc994a19b474f..7e5b1247177cac9d574c6e52caad858e31fd11c1 100644 (file)
@@ -1,12 +1,11 @@
 import * as express from 'express'
 import { body, param } from 'express-validator'
 import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
-import { logger } from '@server/helpers/logger'
 import { CONFIG } from '@server/initializers/config'
-import { VideoChannelModel } from '@server/models/video/video-channel'
 import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
 import { HttpStatusCode, VideoChannelSyncCreate } from '@shared/models'
 import { areValidationErrors, doesVideoChannelIdExist } from '../shared'
+import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
 
 export const ensureSyncIsEnabled = (req: express.Request, res: express.Response, next: express.NextFunction) => {
   if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
@@ -20,12 +19,13 @@ export const ensureSyncIsEnabled = (req: express.Request, res: express.Response,
 }
 
 export const videoChannelSyncValidator = [
-  body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'),
-  body('videoChannelId').isInt().withMessage('Should have a valid video channel id'),
+  body('externalChannelUrl')
+    .custom(isUrlValid),
 
-  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
-    logger.debug('Checking videoChannelSync parameters', { parameters: req.body })
+  body('videoChannelId')
+    .isInt(),
 
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     if (areValidationErrors(req, res)) return
 
     const body: VideoChannelSyncCreate = req.body
@@ -48,18 +48,8 @@ export const ensureSyncExists = [
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     if (areValidationErrors(req, res)) return
 
-    const syncId = parseInt(req.params.id, 10)
-    const sync = await VideoChannelSyncModel.loadWithChannel(syncId)
-
-    if (!sync) {
-      return res.fail({
-        status: HttpStatusCode.NOT_FOUND_404,
-        message: 'Synchronization not found'
-      })
-    }
-
-    res.locals.videoChannelSync = sync
-    res.locals.videoChannel = await VideoChannelModel.loadAndPopulateAccount(sync.videoChannelId)
+    if (!await doesVideoChannelSyncIdExist(+req.params.id, res)) return
+    if (!await doesVideoChannelIdExist(res.locals.videoChannelSync.videoChannelId, res)) return
 
     return next()
   }