]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/videos/video-live.ts
Feature/Add replay privacy (#5692)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-live.ts
index 328760dde3f58bc890b69dae202596c6a09588ee..e80fe15937b54606d54c5a5b3b225ee47b6d915b 100644 (file)
@@ -17,7 +17,7 @@ import {
   VideoState
 } from '@shared/models'
 import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
-import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
+import { isVideoNameValid, isVideoPrivacyValid } from '../../../helpers/custom-validators/videos'
 import { cleanUpReqFiles } from '../../../helpers/express-utils'
 import { logger } from '../../../helpers/logger'
 import { CONFIG } from '../../../initializers/config'
@@ -66,6 +66,11 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
     .customSanitizer(toBooleanOrNull)
     .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
 
+  body('replaySettings.privacy')
+    .optional()
+    .customSanitizer(toIntOrNull)
+    .custom(isVideoPrivacyValid),
+
   body('permanentLive')
     .optional()
     .customSanitizer(toBooleanOrNull)
@@ -153,6 +158,11 @@ const videoLiveUpdateValidator = [
     .customSanitizer(toBooleanOrNull)
     .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
 
+  body('replaySettings.privacy')
+    .optional()
+    .customSanitizer(toIntOrNull)
+    .custom(isVideoPrivacyValid),
+
   body('latencyMode')
     .optional()
     .customSanitizer(toIntOrNull)
@@ -177,6 +187,8 @@ const videoLiveUpdateValidator = [
       })
     }
 
+    if (!checkLiveSettingsReplayConsistency({ res, body })) return
+
     if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
       return res.fail({ message: 'Cannot update a live that has already started' })
     }
@@ -272,3 +284,43 @@ function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
 
   return true
 }
+
+function checkLiveSettingsReplayConsistency (options: {
+  res: express.Response
+  body: LiveVideoUpdate
+}) {
+  const { res, body } = options
+
+  // We now save replays of this live, so replay settings are mandatory
+  if (res.locals.videoLive.saveReplay !== true && body.saveReplay === true) {
+
+    if (!exists(body.replaySettings)) {
+      res.fail({
+        status: HttpStatusCode.BAD_REQUEST_400,
+        message: 'Replay settings are missing now the live replay is saved'
+      })
+      return false
+    }
+
+    if (!exists(body.replaySettings.privacy)) {
+      res.fail({
+        status: HttpStatusCode.BAD_REQUEST_400,
+        message: 'Privacy replay setting is missing now the live replay is saved'
+      })
+      return false
+    }
+  }
+
+  // Save replay was and is not enabled, so send an error the user if it specified replay settings
+  if ((!exists(body.saveReplay) && res.locals.videoLive.saveReplay === false) || body.saveReplay === false) {
+    if (exists(body.replaySettings)) {
+      res.fail({
+        status: HttpStatusCode.BAD_REQUEST_400,
+        message: 'Cannot save replay settings since live replay is not enabled'
+      })
+      return false
+    }
+  }
+
+  return true
+}