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'
14 import { Hooks } from '@server/lib/plugins/hooks'
15 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
17 const videoLiveGetValidator = [
18 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
23 if (areValidationErrors(req, res)) return
24 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
26 // Check if the user who did the request is able to get the live info
27 const user = res.locals.oauth.token.User
28 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
30 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
31 if (!videoLive) return res.sendStatus(404)
33 res.locals.videoLive = videoLive
39 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
41 .customSanitizer(toIntOrNull)
42 .custom(isIdValid).withMessage('Should have correct video channel id'),
45 .custom(isVideoNameValid).withMessage('Should have a valid name'),
49 .customSanitizer(toBooleanOrNull)
50 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
55 if (CONFIG.LIVE.ENABLED !== true) {
58 return res.status(403)
59 .json({ error: 'Live is not enabled on this instance' })
62 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
65 return res.status(403)
66 .json({ error: 'Saving live replay is not allowed instance' })
69 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
71 const user = res.locals.oauth.token.User
72 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
74 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
75 const totalInstanceLives = await VideoModel.countLocalLives()
77 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
80 return res.status(403)
82 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
83 error: 'Cannot create this live because the max instance lives limit is reached.'
88 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
89 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
91 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
94 return res.status(403)
96 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
97 error: 'Cannot create this live because the max user lives limit is reached.'
102 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
108 const videoLiveUpdateValidator = [
111 .customSanitizer(toBooleanOrNull)
112 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
114 (req: express.Request, res: express.Response, next: express.NextFunction) => {
115 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
117 if (areValidationErrors(req, res)) return
119 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
120 return res.status(403)
121 .json({ error: 'Saving live replay is not allowed instance' })
124 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
125 return res.status(400)
126 .json({ error: 'Cannot update a live that has already started' })
129 // Check the user can manage the live
130 const user = res.locals.oauth.token.User
131 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
137 // ---------------------------------------------------------------------------
140 videoLiveAddValidator,
141 videoLiveUpdateValidator,
142 videoLiveGetValidator
145 // ---------------------------------------------------------------------------
147 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
148 // Check we accept this video
149 const acceptParameters = {
150 liveVideoBody: req.body,
151 user: res.locals.oauth.token.User
153 const acceptedResult = await Hooks.wrapFun(
154 isLocalLiveVideoAccepted,
156 'filter:api.live-video.create.accept.result'
159 if (!acceptedResult || acceptedResult.accepted !== true) {
160 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
163 .json({ error: acceptedResult.errorMessage || 'Refused local live video' })