From 05a60d85997c108d39bcfb14f1ffd4c74f8b1e93 Mon Sep 17 00:00:00 2001 From: Wicklow <123956049+wickloww@users.noreply.github.com> Date: Fri, 31 Mar 2023 07:12:21 +0000 Subject: Feature/Add replay privacy (#5692) * Add replay settings feature * Fix replay settings behaviour * Fix tests * Fix tests * Fix tests * Update openapi doc and fix tests * Add tests and fix code * Models correction * Add migration and update controller and middleware * Add check params tests * Fix video live middleware * Updated code based on review comments --- server/middlewares/validators/videos/video-live.ts | 54 +++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) (limited to 'server/middlewares') diff --git a/server/middlewares/validators/videos/video-live.ts b/server/middlewares/validators/videos/video-live.ts index 328760dde..e80fe1593 100644 --- a/server/middlewares/validators/videos/video-live.ts +++ b/server/middlewares/validators/videos/video-live.ts @@ -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 +} -- cgit v1.2.3