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'
32 const videoLiveGetValidator = [
33 isValidVideoIdParam('videoId'),
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params })
38 if (areValidationErrors(req, res)) return
39 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
41 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
44 status: HttpStatusCode.NOT_FOUND_404,
45 message: 'Live video not found'
49 res.locals.videoLive = videoLive
55 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
57 .customSanitizer(toIntOrNull)
58 .custom(isIdValid).withMessage('Should have correct video channel id'),
61 .custom(isVideoNameValid).withMessage(
62 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
67 .customSanitizer(toBooleanOrNull)
68 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
72 .customSanitizer(toBooleanOrNull)
73 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
77 .customSanitizer(toIntOrNull)
78 .custom(isLiveLatencyModeValid)
79 .withMessage('Should have a valid latency mode attribute'),
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.countLocalLives()
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 attribute'),
162 .customSanitizer(toIntOrNull)
163 .custom(isLiveLatencyModeValid)
164 .withMessage('Should have a valid latency mode attribute'),
166 (req: express.Request, res: express.Response, next: express.NextFunction) => {
167 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
169 if (areValidationErrors(req, res)) return
171 const body: LiveVideoUpdate = req.body
173 if (body.permanentLive && body.saveReplay) {
174 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
177 if (hasValidSaveReplay(body) !== true) {
179 status: HttpStatusCode.FORBIDDEN_403,
180 message: 'Saving live replay is not allowed by this instance'
184 if (hasValidLatencyMode(body) !== true) {
186 status: HttpStatusCode.FORBIDDEN_403,
187 message: 'Custom latency mode is not allowed by this instance'
191 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
192 return res.fail({ message: 'Cannot update a live that has already started' })
195 // Check the user can manage the live
196 const user = res.locals.oauth.token.User
197 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
203 // ---------------------------------------------------------------------------
206 videoLiveAddValidator,
207 videoLiveUpdateValidator,
208 videoLiveGetValidator
211 // ---------------------------------------------------------------------------
213 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
214 // Check we accept this video
215 const acceptParameters = {
216 liveVideoBody: req.body,
217 user: res.locals.oauth.token.User
219 const acceptedResult = await Hooks.wrapFun(
220 isLocalLiveVideoAccepted,
222 'filter:api.live-video.create.accept.result'
225 if (!acceptedResult || acceptedResult.accepted !== true) {
226 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
229 status: HttpStatusCode.FORBIDDEN_403,
230 message: acceptedResult.errorMessage || 'Refused local live video'
238 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
239 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
244 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
246 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
247 exists(body.latencyMode) &&
248 body.latencyMode !== LiveVideoLatencyMode.DEFAULT