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'
16 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
17 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
19 const videoLiveGetValidator = [
20 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
25 if (areValidationErrors(req, res)) return
26 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
28 // Check if the user who did the request is able to get the live info
29 const user = res.locals.oauth.token.User
30 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
32 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
33 if (!videoLive) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
35 res.locals.videoLive = videoLive
41 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
43 .customSanitizer(toIntOrNull)
44 .custom(isIdValid).withMessage('Should have correct video channel id'),
47 .custom(isVideoNameValid).withMessage(
48 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
53 .customSanitizer(toBooleanOrNull)
54 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
58 .customSanitizer(toBooleanOrNull)
59 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
61 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
62 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
64 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
66 if (CONFIG.LIVE.ENABLED !== true) {
69 return res.status(HttpStatusCode.FORBIDDEN_403)
70 .json({ error: 'Live is not enabled on this instance' })
73 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
76 return res.status(HttpStatusCode.FORBIDDEN_403)
77 .json({ error: 'Saving live replay is not allowed instance' })
80 if (req.body.permanentLive && req.body.saveReplay) {
83 return res.status(HttpStatusCode.BAD_REQUEST_400)
84 .json({ error: 'Cannot set this live as permanent while saving its replay' })
87 const user = res.locals.oauth.token.User
88 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
90 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
91 const totalInstanceLives = await VideoModel.countLocalLives()
93 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
96 return res.status(HttpStatusCode.FORBIDDEN_403)
98 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
99 error: 'Cannot create this live because the max instance lives limit is reached.'
104 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
105 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
107 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
110 return res.status(HttpStatusCode.FORBIDDEN_403)
112 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
113 error: 'Cannot create this live because the max user lives limit is reached.'
118 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
124 const videoLiveUpdateValidator = [
127 .customSanitizer(toBooleanOrNull)
128 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
130 (req: express.Request, res: express.Response, next: express.NextFunction) => {
131 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
133 if (areValidationErrors(req, res)) return
135 if (req.body.permanentLive && req.body.saveReplay) {
136 return res.status(HttpStatusCode.BAD_REQUEST_400)
137 .json({ error: 'Cannot set this live as permanent while saving its replay' })
140 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
141 return res.status(HttpStatusCode.FORBIDDEN_403)
142 .json({ error: 'Saving live replay is not allowed instance' })
145 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
146 return res.status(HttpStatusCode.BAD_REQUEST_400)
147 .json({ error: 'Cannot update a live that has already started' })
150 // Check the user can manage the live
151 const user = res.locals.oauth.token.User
152 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
158 // ---------------------------------------------------------------------------
161 videoLiveAddValidator,
162 videoLiveUpdateValidator,
163 videoLiveGetValidator
166 // ---------------------------------------------------------------------------
168 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
169 // Check we accept this video
170 const acceptParameters = {
171 liveVideoBody: req.body,
172 user: res.locals.oauth.token.User
174 const acceptedResult = await Hooks.wrapFun(
175 isLocalLiveVideoAccepted,
177 'filter:api.live-video.create.accept.result'
180 if (!acceptedResult || acceptedResult.accepted !== true) {
181 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
183 res.status(HttpStatusCode.FORBIDDEN_403)
184 .json({ error: acceptedResult.errorMessage || 'Refused local live video' })