1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos'
4 import { VideoLiveModel } from '@server/models/video/video-live'
5 import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
6 import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
7 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
8 import { cleanUpReqFiles } from '../../../helpers/express-utils'
9 import { logger } from '../../../helpers/logger'
10 import { CONFIG } from '../../../initializers/config'
11 import { areValidationErrors } from '../utils'
12 import { getCommonVideoEditAttributes } from './videos'
13 import { VideoModel } from '@server/models/video/video'
15 const videoLiveGetValidator = [
16 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.body })
21 if (areValidationErrors(req, res)) return
22 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
24 // Check if the user who did the request is able to update the video
25 const user = res.locals.oauth.token.User
26 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return
28 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
29 if (!videoLive) return res.sendStatus(404)
31 res.locals.videoLive = videoLive
37 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
39 .customSanitizer(toIntOrNull)
40 .custom(isIdValid).withMessage('Should have correct video channel id'),
43 .custom(isVideoNameValid).withMessage('Should have a valid name'),
47 .customSanitizer(toBooleanOrNull)
48 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
53 if (CONFIG.LIVE.ENABLED !== true) {
56 return res.status(403)
57 .json({ error: 'Live is not enabled on this instance' })
60 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
63 return res.status(403)
64 .json({ error: 'Saving live replay is not allowed instance' })
67 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
69 const user = res.locals.oauth.token.User
70 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
72 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
73 const totalInstanceLives = await VideoModel.countLocalLives()
75 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
78 return res.status(403)
80 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
81 error: 'Cannot create this live because the max instance lives limit is reached.'
86 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
87 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
89 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
92 return res.status(403)
94 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
95 error: 'Cannot create this live because the max user lives limit is reached.'
104 const videoLiveUpdateValidator = [
107 .customSanitizer(toBooleanOrNull)
108 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
110 (req: express.Request, res: express.Response, next: express.NextFunction) => {
111 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
113 if (areValidationErrors(req, res)) return
115 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
116 return res.status(403)
117 .json({ error: 'Saving live replay is not allowed instance' })
120 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
121 return res.status(400)
122 .json({ error: 'Cannot update a live that has already started' })
129 // ---------------------------------------------------------------------------
132 videoLiveAddValidator,
133 videoLiveUpdateValidator,
134 videoLiveGetValidator