1 import * as express from 'express'
2 import { body } from 'express-validator'
3 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
4 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
5 import { Hooks } from '@server/lib/plugins/hooks'
6 import { VideoModel } from '@server/models/video/video'
7 import { VideoLiveModel } from '@server/models/video/video-live'
8 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
9 import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
10 import { isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
11 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
12 import { cleanUpReqFiles } from '../../../helpers/express-utils'
13 import { logger } from '../../../helpers/logger'
14 import { CONFIG } from '../../../initializers/config'
17 checkUserCanManageVideo,
18 doesVideoChannelOfAccountExist,
22 import { getCommonVideoEditAttributes } from './videos'
24 const videoLiveGetValidator = [
25 isValidVideoIdParam('videoId'),
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
30 if (areValidationErrors(req, res)) return
31 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
33 // Check if the user who did the request is able to get the live info
34 const user = res.locals.oauth.token.User
35 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
37 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
40 status: HttpStatusCode.NOT_FOUND_404,
41 message: 'Live video not found'
45 res.locals.videoLive = videoLive
51 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
53 .customSanitizer(toIntOrNull)
54 .custom(isIdValid).withMessage('Should have correct video channel id'),
57 .custom(isVideoNameValid).withMessage(
58 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
63 .customSanitizer(toBooleanOrNull)
64 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
68 .customSanitizer(toBooleanOrNull)
69 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
71 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
74 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
76 if (CONFIG.LIVE.ENABLED !== true) {
80 status: HttpStatusCode.FORBIDDEN_403,
81 message: 'Live is not enabled on this instance',
82 type: ServerErrorCode.LIVE_NOT_ENABLED
86 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
90 status: HttpStatusCode.FORBIDDEN_403,
91 message: 'Saving live replay is not enabled on this instance',
92 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
96 if (req.body.permanentLive && req.body.saveReplay) {
99 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
102 const user = res.locals.oauth.token.User
103 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
105 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
106 const totalInstanceLives = await VideoModel.countLocalLives()
108 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
112 status: HttpStatusCode.FORBIDDEN_403,
113 message: 'Cannot create this live because the max instance lives limit is reached.',
114 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
119 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
120 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
122 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
126 status: HttpStatusCode.FORBIDDEN_403,
127 message: 'Cannot create this live because the max user lives limit is reached.',
128 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
133 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
139 const videoLiveUpdateValidator = [
142 .customSanitizer(toBooleanOrNull)
143 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
145 (req: express.Request, res: express.Response, next: express.NextFunction) => {
146 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
148 if (areValidationErrors(req, res)) return
150 if (req.body.permanentLive && req.body.saveReplay) {
151 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
154 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
156 status: HttpStatusCode.FORBIDDEN_403,
157 message: 'Saving live replay is not allowed instance'
161 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
162 return res.fail({ message: 'Cannot update a live that has already started' })
165 // Check the user can manage the live
166 const user = res.locals.oauth.token.User
167 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
173 // ---------------------------------------------------------------------------
176 videoLiveAddValidator,
177 videoLiveUpdateValidator,
178 videoLiveGetValidator
181 // ---------------------------------------------------------------------------
183 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
184 // Check we accept this video
185 const acceptParameters = {
186 liveVideoBody: req.body,
187 user: res.locals.oauth.token.User
189 const acceptedResult = await Hooks.wrapFun(
190 isLocalLiveVideoAccepted,
192 'filter:api.live-video.create.accept.result'
195 if (!acceptedResult || acceptedResult.accepted !== true) {
196 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
199 status: HttpStatusCode.FORBIDDEN_403,
200 message: acceptedResult.errorMessage || 'Refused local live video'