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)
59 .custom(isIdValid).withMessage('Should have correct video channel id'),
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 attribute'),
73 .customSanitizer(toBooleanOrNull)
74 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
78 .customSanitizer(toIntOrNull)
79 .custom(isLiveLatencyModeValid)
80 .withMessage('Should have a valid latency mode attribute'),
82 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
83 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
85 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
87 if (CONFIG.LIVE.ENABLED !== true) {
91 status: HttpStatusCode.FORBIDDEN_403,
92 message: 'Live is not enabled on this instance',
93 type: ServerErrorCode.LIVE_NOT_ENABLED
97 const body: LiveVideoCreate = req.body
99 if (hasValidSaveReplay(body) !== true) {
103 status: HttpStatusCode.FORBIDDEN_403,
104 message: 'Saving live replay is not enabled on this instance',
105 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
109 if (hasValidLatencyMode(body) !== true) {
113 status: HttpStatusCode.FORBIDDEN_403,
114 message: 'Custom latency mode is not allowed by this instance'
118 const user = res.locals.oauth.token.User
119 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
121 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
122 const totalInstanceLives = await VideoModel.countLives({ remote: false, mode: 'not-ended' })
124 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
128 status: HttpStatusCode.FORBIDDEN_403,
129 message: 'Cannot create this live because the max instance lives limit is reached.',
130 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
135 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
136 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
138 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
142 status: HttpStatusCode.FORBIDDEN_403,
143 message: 'Cannot create this live because the max user lives limit is reached.',
144 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
149 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
155 const videoLiveUpdateValidator = [
158 .customSanitizer(toBooleanOrNull)
159 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
163 .customSanitizer(toIntOrNull)
164 .custom(isLiveLatencyModeValid)
165 .withMessage('Should have a valid latency mode attribute'),
167 (req: express.Request, res: express.Response, next: express.NextFunction) => {
168 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
170 if (areValidationErrors(req, res)) return
172 const body: LiveVideoUpdate = req.body
174 if (hasValidSaveReplay(body) !== true) {
176 status: HttpStatusCode.FORBIDDEN_403,
177 message: 'Saving live replay is not allowed by this instance'
181 if (hasValidLatencyMode(body) !== true) {
183 status: HttpStatusCode.FORBIDDEN_403,
184 message: 'Custom latency mode is not allowed by this instance'
188 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
189 return res.fail({ message: 'Cannot update a live that has already started' })
192 // Check the user can manage the live
193 const user = res.locals.oauth.token.User
194 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
200 const videoLiveListSessionsValidator = [
201 (req: express.Request, res: express.Response, next: express.NextFunction) => {
202 logger.debug('Checking videoLiveListSessionsValidator parameters', { parameters: req.params })
204 // Check the user can manage the live
205 const user = res.locals.oauth.token.User
206 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
212 const videoLiveFindReplaySessionValidator = [
213 isValidVideoIdParam('videoId'),
215 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
216 logger.debug('Checking videoLiveFindReplaySessionValidator parameters', { parameters: req.params })
218 if (areValidationErrors(req, res)) return
219 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
221 const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id)
224 status: HttpStatusCode.NOT_FOUND_404,
225 message: 'No live replay found'
229 res.locals.videoLiveSession = session
235 // ---------------------------------------------------------------------------
238 videoLiveAddValidator,
239 videoLiveUpdateValidator,
240 videoLiveListSessionsValidator,
241 videoLiveFindReplaySessionValidator,
242 videoLiveGetValidator
245 // ---------------------------------------------------------------------------
247 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
248 // Check we accept this video
249 const acceptParameters = {
250 liveVideoBody: req.body,
251 user: res.locals.oauth.token.User
253 const acceptedResult = await Hooks.wrapFun(
254 isLocalLiveVideoAccepted,
256 'filter:api.live-video.create.accept.result'
259 if (!acceptedResult || acceptedResult.accepted !== true) {
260 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
263 status: HttpStatusCode.FORBIDDEN_403,
264 message: acceptedResult.errorMessage || 'Refused local live video'
272 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
273 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
278 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
280 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
281 exists(body.latencyMode) &&
282 body.latencyMode !== LiveVideoLatencyMode.DEFAULT