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)
35 status: HttpStatusCode.NOT_FOUND_404,
36 message: 'Live video not found'
40 res.locals.videoLive = videoLive
46 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
48 .customSanitizer(toIntOrNull)
49 .custom(isIdValid).withMessage('Should have correct video channel id'),
52 .custom(isVideoNameValid).withMessage(
53 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
58 .customSanitizer(toBooleanOrNull)
59 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
63 .customSanitizer(toBooleanOrNull)
64 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
66 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
69 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
71 if (CONFIG.LIVE.ENABLED !== true) {
75 status: HttpStatusCode.FORBIDDEN_403,
76 message: 'Live is not enabled on this instance'
80 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
84 status: HttpStatusCode.FORBIDDEN_403,
85 message: 'Saving live replay is not allowed instance'
89 if (req.body.permanentLive && req.body.saveReplay) {
92 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
95 const user = res.locals.oauth.token.User
96 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
98 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
99 const totalInstanceLives = await VideoModel.countLocalLives()
101 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
105 status: HttpStatusCode.FORBIDDEN_403,
106 message: 'Cannot create this live because the max instance lives limit is reached.',
107 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
112 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
113 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
115 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
119 status: HttpStatusCode.FORBIDDEN_403,
120 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
121 message: 'Cannot create this live because the max user lives limit is reached.'
126 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
132 const videoLiveUpdateValidator = [
135 .customSanitizer(toBooleanOrNull)
136 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
141 if (areValidationErrors(req, res)) return
143 if (req.body.permanentLive && req.body.saveReplay) {
144 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
147 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
149 status: HttpStatusCode.FORBIDDEN_403,
150 message: 'Saving live replay is not allowed instance'
154 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
155 return res.fail({ message: 'Cannot update a live that has already started' })
158 // Check the user can manage the live
159 const user = res.locals.oauth.token.User
160 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
166 // ---------------------------------------------------------------------------
169 videoLiveAddValidator,
170 videoLiveUpdateValidator,
171 videoLiveGetValidator
174 // ---------------------------------------------------------------------------
176 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
177 // Check we accept this video
178 const acceptParameters = {
179 liveVideoBody: req.body,
180 user: res.locals.oauth.token.User
182 const acceptedResult = await Hooks.wrapFun(
183 isLocalLiveVideoAccepted,
185 'filter:api.live-video.create.accept.result'
188 if (!acceptedResult || acceptedResult.accepted !== true) {
189 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
192 status: HttpStatusCode.FORBIDDEN_403,
193 message: acceptedResult.errorMessage || 'Refused local live video'