]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/videos/videos.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / videos.ts
index 3d93bc62f6ddde822cb678f780e03cc58e9fc197..794e1d4f1efa199b5c55dfed9c202e0ea89ba50a 100644 (file)
@@ -6,15 +6,15 @@ import { Redis } from '@server/lib/redis'
 import { getServerActor } from '@server/models/application/application'
 import { ExpressPromiseHandler } from '@server/types/express-handler'
 import { MUserAccountId, MVideoFullLight } from '@server/types/models'
-import { getAllPrivacies } from '@shared/core-utils'
-import { HttpStatusCode, ServerErrorCode, UserRight, VideoInclude } from '@shared/models'
+import { arrayify, getAllPrivacies } from '@shared/core-utils'
+import { getVideoStreamDuration } from '@shared/ffmpeg'
+import { HttpStatusCode, ServerErrorCode, UserRight, VideoInclude, VideoState } from '@shared/models'
 import {
   exists,
   isBooleanValid,
   isDateValid,
   isFileValid,
   isIdValid,
-  toArray,
   toBooleanOrNull,
   toIntOrNull,
   toValueOrNull
@@ -38,7 +38,6 @@ import {
   isVideoSupportValid
 } from '../../../helpers/custom-validators/videos'
 import { cleanUpReqFiles } from '../../../helpers/express-utils'
-import { getVideoStreamDuration } from '../../../helpers/ffmpeg'
 import { logger } from '../../../helpers/logger'
 import { deleteFileAndCatch } from '../../../helpers/utils'
 import { getVideoWithAttributes } from '../../../helpers/video'
@@ -49,6 +48,7 @@ import { Hooks } from '../../../lib/plugins/hooks'
 import { VideoModel } from '../../../models/video/video'
 import {
   areValidationErrors,
+  checkCanAccessVideoStaticFiles,
   checkCanSeeVideo,
   checkUserCanManageVideo,
   checkUserQuota,
@@ -233,6 +233,11 @@ const videosUpdateValidator = getCommonVideoEditAttributes().concat([
     if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
     if (!await doesVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
 
+    const video = getVideoWithAttributes(res)
+    if (video.isLive && video.privacy !== req.body.privacy && video.state !== VideoState.WAITING_FOR_LIVE) {
+      return res.fail({ message: 'Cannot update privacy of a live that has already started' })
+    }
+
     // Check if the user who did the request is able to update the video
     const user = res.locals.oauth.token.User
     if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
@@ -272,10 +277,7 @@ async function checkVideoFollowConstraints (req: express.Request, res: express.R
   })
 }
 
-const videosCustomGetValidator = (
-  fetchType: 'for-api' | 'all' | 'only-video' | 'only-immutable-attributes',
-  authenticateInQuery = false
-) => {
+const videosCustomGetValidator = (fetchType: 'for-api' | 'all' | 'only-video' | 'only-immutable-attributes') => {
   return [
     isValidVideoIdParam('id'),
 
@@ -288,7 +290,7 @@ const videosCustomGetValidator = (
 
       const video = getVideoWithAttributes(res) as MVideoFullLight
 
-      if (!await checkCanSeeVideo({ req, res, video, paramId: req.params.id, authenticateInQuery })) return
+      if (!await checkCanSeeVideo({ req, res, video, paramId: req.params.id })) return
 
       return next()
     }
@@ -296,7 +298,6 @@ const videosCustomGetValidator = (
 }
 
 const videosGetValidator = videosCustomGetValidator('all')
-const videosDownloadValidator = videosCustomGetValidator('all', true)
 
 const videoFileMetadataGetValidator = getCommonVideoEditAttributes().concat([
   isValidVideoIdParam('id'),
@@ -312,6 +313,21 @@ const videoFileMetadataGetValidator = getCommonVideoEditAttributes().concat([
   }
 ])
 
+const videosDownloadValidator = [
+  isValidVideoIdParam('id'),
+
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    if (areValidationErrors(req, res)) return
+    if (!await doesVideoExist(req.params.id, res, 'all')) return
+
+    const video = getVideoWithAttributes(res)
+
+    if (!await checkCanAccessVideoStaticFiles({ req, res, video, paramId: req.params.id })) return
+
+    return next()
+  }
+]
+
 const videosRemoveValidator = [
   isValidVideoIdParam('id'),
 
@@ -373,7 +389,7 @@ function getCommonVideoEditAttributes () {
       .custom(isBooleanValid).withMessage('Should have a valid waitTranscoding boolean'),
     body('privacy')
       .optional()
-      .customSanitizer(toValueOrNull)
+      .customSanitizer(toIntOrNull)
       .custom(isVideoPrivacyValid),
     body('description')
       .optional()
@@ -419,27 +435,27 @@ function getCommonVideoEditAttributes () {
 const commonVideosFiltersValidator = [
   query('categoryOneOf')
     .optional()
-    .customSanitizer(toArray)
+    .customSanitizer(arrayify)
     .custom(isNumberArray).withMessage('Should have a valid categoryOneOf array'),
   query('licenceOneOf')
     .optional()
-    .customSanitizer(toArray)
+    .customSanitizer(arrayify)
     .custom(isNumberArray).withMessage('Should have a valid licenceOneOf array'),
   query('languageOneOf')
     .optional()
-    .customSanitizer(toArray)
+    .customSanitizer(arrayify)
     .custom(isStringArray).withMessage('Should have a valid languageOneOf array'),
   query('privacyOneOf')
     .optional()
-    .customSanitizer(toArray)
+    .customSanitizer(arrayify)
     .custom(isNumberArray).withMessage('Should have a valid privacyOneOf array'),
   query('tagsOneOf')
     .optional()
-    .customSanitizer(toArray)
+    .customSanitizer(arrayify)
     .custom(isStringArray).withMessage('Should have a valid tagsOneOf array'),
   query('tagsAllOf')
     .optional()
-    .customSanitizer(toArray)
+    .customSanitizer(arrayify)
     .custom(isStringArray).withMessage('Should have a valid tagsAllOf array'),
   query('nsfw')
     .optional()
@@ -473,6 +489,10 @@ const commonVideosFiltersValidator = [
   query('search')
     .optional()
     .custom(exists),
+  query('excludeAlreadyWatched')
+    .optional()
+    .customSanitizer(toBooleanOrNull)
+    .isBoolean().withMessage('Should be a valid excludeAlreadyWatched boolean'),
 
   (req: express.Request, res: express.Response, next: express.NextFunction) => {
     if (areValidationErrors(req, res)) return
@@ -504,6 +524,13 @@ const commonVideosFiltersValidator = [
       }
     }
 
+    if (!user && exists(req.query.excludeAlreadyWatched)) {
+      res.fail({
+        status: HttpStatusCode.BAD_REQUEST_400,
+        message: 'Cannot use excludeAlreadyWatched parameter when auth token is not provided'
+      })
+      return false
+    }
     return next()
   }
 ]