1 import express from 'express'
2 import { body } from 'express-validator'
3 import { isLiveLatencyModeValid } from '@server/helpers/custom-validators/video-lives'
4 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
5 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
6 import { Hooks } from '@server/lib/plugins/hooks'
7 import { VideoModel } from '@server/models/video/video'
8 import { VideoLiveModel } from '@server/models/video/video-live'
17 } from '@shared/models'
18 import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
19 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
20 import { cleanUpReqFiles } from '../../../helpers/express-utils'
21 import { logger } from '../../../helpers/logger'
22 import { CONFIG } from '../../../initializers/config'
25 checkUserCanManageVideo,
26 doesVideoChannelOfAccountExist,
30 import { getCommonVideoEditAttributes } from './videos'
31 import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
33 const videoLiveGetValidator = [
34 isValidVideoIdParam('videoId'),
36 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params })
39 if (areValidationErrors(req, res)) return
40 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
42 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
45 status: HttpStatusCode.NOT_FOUND_404,
46 message: 'Live video not found'
50 res.locals.videoLive = videoLive
56 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
58 .customSanitizer(toIntOrNull)
62 .custom(isVideoNameValid).withMessage(
63 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
68 .customSanitizer(toBooleanOrNull)
69 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
73 .customSanitizer(toBooleanOrNull)
74 .custom(isBooleanValid).withMessage('Should have a valid permanentLive boolean'),
78 .customSanitizer(toIntOrNull)
79 .custom(isLiveLatencyModeValid),
81 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
82 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
84 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
86 if (CONFIG.LIVE.ENABLED !== true) {
90 status: HttpStatusCode.FORBIDDEN_403,
91 message: 'Live is not enabled on this instance',
92 type: ServerErrorCode.LIVE_NOT_ENABLED
96 const body: LiveVideoCreate = req.body
98 if (hasValidSaveReplay(body) !== true) {
102 status: HttpStatusCode.FORBIDDEN_403,
103 message: 'Saving live replay is not enabled on this instance',
104 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
108 if (hasValidLatencyMode(body) !== true) {
112 status: HttpStatusCode.FORBIDDEN_403,
113 message: 'Custom latency mode is not allowed by this instance'
117 const user = res.locals.oauth.token.User
118 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
120 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
121 const totalInstanceLives = await VideoModel.countLives({ remote: false, mode: 'not-ended' })
123 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
127 status: HttpStatusCode.FORBIDDEN_403,
128 message: 'Cannot create this live because the max instance lives limit is reached.',
129 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
134 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
135 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
137 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
141 status: HttpStatusCode.FORBIDDEN_403,
142 message: 'Cannot create this live because the max user lives limit is reached.',
143 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
148 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
154 const videoLiveUpdateValidator = [
157 .customSanitizer(toBooleanOrNull)
158 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
162 .customSanitizer(toIntOrNull)
163 .custom(isLiveLatencyModeValid),
165 (req: express.Request, res: express.Response, next: express.NextFunction) => {
166 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
168 if (areValidationErrors(req, res)) return
170 const body: LiveVideoUpdate = req.body
172 if (hasValidSaveReplay(body) !== true) {
174 status: HttpStatusCode.FORBIDDEN_403,
175 message: 'Saving live replay is not allowed by this instance'
179 if (hasValidLatencyMode(body) !== true) {
181 status: HttpStatusCode.FORBIDDEN_403,
182 message: 'Custom latency mode is not allowed by this instance'
186 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
187 return res.fail({ message: 'Cannot update a live that has already started' })
190 // Check the user can manage the live
191 const user = res.locals.oauth.token.User
192 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
198 const videoLiveListSessionsValidator = [
199 (req: express.Request, res: express.Response, next: express.NextFunction) => {
200 logger.debug('Checking videoLiveListSessionsValidator parameters', { parameters: req.params })
202 // Check the user can manage the live
203 const user = res.locals.oauth.token.User
204 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
210 const videoLiveFindReplaySessionValidator = [
211 isValidVideoIdParam('videoId'),
213 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
214 logger.debug('Checking videoLiveFindReplaySessionValidator parameters', { parameters: req.params })
216 if (areValidationErrors(req, res)) return
217 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
219 const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id)
222 status: HttpStatusCode.NOT_FOUND_404,
223 message: 'No live replay found'
227 res.locals.videoLiveSession = session
233 // ---------------------------------------------------------------------------
236 videoLiveAddValidator,
237 videoLiveUpdateValidator,
238 videoLiveListSessionsValidator,
239 videoLiveFindReplaySessionValidator,
240 videoLiveGetValidator
243 // ---------------------------------------------------------------------------
245 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
246 // Check we accept this video
247 const acceptParameters = {
248 liveVideoBody: req.body,
249 user: res.locals.oauth.token.User
251 const acceptedResult = await Hooks.wrapFun(
252 isLocalLiveVideoAccepted,
254 'filter:api.live-video.create.accept.result'
257 if (!acceptedResult || acceptedResult.accepted !== true) {
258 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
261 status: HttpStatusCode.FORBIDDEN_403,
262 message: acceptedResult.errorMessage || 'Refused local live video'
270 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
271 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
276 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
278 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
279 exists(body.latencyMode) &&
280 body.latencyMode !== LiveVideoLatencyMode.DEFAULT