]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/videos.ts
Add ability for uploaders to schedule video update
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.ts
index e181aebdbcdaa43e4431356952c6768d1b3cd561..9fe5a253b5d0a2f78030f5b4ad6f7d40517c240c 100644 (file)
@@ -2,8 +2,17 @@ import * as express from 'express'
 import 'express-validator'
 import { body, param, query } from 'express-validator/check'
 import { UserRight, VideoPrivacy } from '../../../shared'
-import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid, toIntOrNull, toValueOrNull } from '../../helpers/custom-validators/misc'
 import {
+  isBooleanValid,
+  isDateValid,
+  isIdOrUUIDValid,
+  isIdValid,
+  isUUIDValid,
+  toIntOrNull,
+  toValueOrNull
+} from '../../helpers/custom-validators/misc'
+import {
+  isScheduleVideoUpdatePrivacyValid,
   isVideoAbuseReasonValid,
   isVideoCategoryValid,
   isVideoChannelOfAccountExist,
@@ -84,14 +93,21 @@ const videosAddValidator = [
     .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
   body('channelId')
     .toInt()
-    .custom(isIdValid)
-    .withMessage('Should have correct video channel id'),
+    .custom(isIdValid).withMessage('Should have correct video channel id'),
+  body('scheduleUpdate.updateAt')
+    .optional()
+    .custom(isDateValid).withMessage('Should have a valid schedule update date'),
+  body('scheduleUpdate.privacy')
+    .optional()
+    .toInt()
+    .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy'),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
 
     if (areValidationErrors(req, res)) return
     if (areErrorsInVideoImageFiles(req, res)) return
+    if (areErrorsInScheduleUpdate(req, res)) return
 
     const videoFile: Express.Multer.File = req.files['videofile'][0]
     const user = res.locals.oauth.token.User
@@ -183,12 +199,20 @@ const videosUpdateValidator = [
     .optional()
     .toInt()
     .custom(isIdValid).withMessage('Should have correct video channel id'),
+  body('scheduleUpdate.updateAt')
+    .optional()
+    .custom(isDateValid).withMessage('Should have a valid schedule update date'),
+  body('scheduleUpdate.privacy')
+    .optional()
+    .toInt()
+    .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy'),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking videosUpdate parameters', { parameters: req.body })
 
     if (areValidationErrors(req, res)) return
     if (areErrorsInVideoImageFiles(req, res)) return
+    if (areErrorsInScheduleUpdate(req, res)) return
     if (!await isVideoExist(req.params.id, res)) return
 
     const video = res.locals.video
@@ -371,7 +395,7 @@ function areErrorsInVideoImageFiles (req: express.Request, res: express.Response
     const imageFile = req.files[ imageField ][ 0 ] as Express.Multer.File
     if (imageFile.size > CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max) {
       res.status(400)
-        .send({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` })
+        .json({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` })
         .end()
       return true
     }
@@ -379,3 +403,17 @@ function areErrorsInVideoImageFiles (req: express.Request, res: express.Response
 
   return false
 }
+
+function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
+  if (req.body.scheduleUpdate) {
+    if (!req.body.scheduleUpdate.updateAt) {
+      res.status(400)
+         .json({ error: 'Schedule update at is mandatory.' })
+         .end()
+
+      return true
+    }
+  }
+
+  return false
+}