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'),
54 .customSanitizer(toBooleanOrNull)
55 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
60 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
62 if (CONFIG.LIVE.ENABLED !== true) {
65 return res.status(403)
66 .json({ error: 'Live is not enabled on this instance' })
69 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
72 return res.status(403)
73 .json({ error: 'Saving live replay is not allowed instance' })
76 if (req.body.permanentLive && req.body.saveReplay) {
79 return res.status(400)
80 .json({ error: 'Cannot set this live as permanent while saving its replay' })
83 const user = res.locals.oauth.token.User
84 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
86 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
87 const totalInstanceLives = await VideoModel.countLocalLives()
89 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
92 return res.status(403)
94 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
95 error: 'Cannot create this live because the max instance lives limit is reached.'
100 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
101 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
103 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
106 return res.status(403)
108 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
109 error: 'Cannot create this live because the max user lives limit is reached.'
114 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
120 const videoLiveUpdateValidator = [
123 .customSanitizer(toBooleanOrNull)
124 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
126 (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
129 if (areValidationErrors(req, res)) return
131 if (req.body.permanentLive && req.body.saveReplay) {
132 return res.status(400)
133 .json({ error: 'Cannot set this live as permanent while saving its replay' })
136 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
137 return res.status(403)
138 .json({ error: 'Saving live replay is not allowed instance' })
141 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
142 return res.status(400)
143 .json({ error: 'Cannot update a live that has already started' })
146 // Check the user can manage the live
147 const user = res.locals.oauth.token.User
148 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
154 // ---------------------------------------------------------------------------
157 videoLiveAddValidator,
158 videoLiveUpdateValidator,
159 videoLiveGetValidator
162 // ---------------------------------------------------------------------------
164 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
165 // Check we accept this video
166 const acceptParameters = {
167 liveVideoBody: req.body,
168 user: res.locals.oauth.token.User
170 const acceptedResult = await Hooks.wrapFun(
171 isLocalLiveVideoAccepted,
173 'filter:api.live-video.create.accept.result'
176 if (!acceptedResult || acceptedResult.accepted !== true) {
177 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
180 .json({ error: acceptedResult.errorMessage || 'Refused local live video' })