1 import * as express from 'express'
2 import { body, param } 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, isIdOrUUIDValid, 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'
15 import { areValidationErrors, checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '../shared'
16 import { getCommonVideoEditAttributes } from './videos'
18 const videoLiveGetValidator = [
19 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
24 if (areValidationErrors(req, res)) return
25 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
27 // Check if the user who did the request is able to get the live info
28 const user = res.locals.oauth.token.User
29 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
31 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
34 status: HttpStatusCode.NOT_FOUND_404,
35 message: 'Live video not found'
39 res.locals.videoLive = videoLive
45 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
47 .customSanitizer(toIntOrNull)
48 .custom(isIdValid).withMessage('Should have correct video channel id'),
51 .custom(isVideoNameValid).withMessage(
52 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
57 .customSanitizer(toBooleanOrNull)
58 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
62 .customSanitizer(toBooleanOrNull)
63 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
68 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
70 if (CONFIG.LIVE.ENABLED !== true) {
74 status: HttpStatusCode.FORBIDDEN_403,
75 message: 'Live is not enabled on this instance'
79 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
83 status: HttpStatusCode.FORBIDDEN_403,
84 message: 'Saving live replay is not allowed instance'
88 if (req.body.permanentLive && req.body.saveReplay) {
91 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
94 const user = res.locals.oauth.token.User
95 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
97 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
98 const totalInstanceLives = await VideoModel.countLocalLives()
100 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
104 status: HttpStatusCode.FORBIDDEN_403,
105 message: 'Cannot create this live because the max instance lives limit is reached.',
106 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
111 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
112 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
114 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
118 status: HttpStatusCode.FORBIDDEN_403,
119 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
120 message: 'Cannot create this live because the max user lives limit is reached.'
125 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
131 const videoLiveUpdateValidator = [
134 .customSanitizer(toBooleanOrNull)
135 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
137 (req: express.Request, res: express.Response, next: express.NextFunction) => {
138 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
140 if (areValidationErrors(req, res)) return
142 if (req.body.permanentLive && req.body.saveReplay) {
143 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
146 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
148 status: HttpStatusCode.FORBIDDEN_403,
149 message: 'Saving live replay is not allowed instance'
153 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
154 return res.fail({ message: 'Cannot update a live that has already started' })
157 // Check the user can manage the live
158 const user = res.locals.oauth.token.User
159 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
165 // ---------------------------------------------------------------------------
168 videoLiveAddValidator,
169 videoLiveUpdateValidator,
170 videoLiveGetValidator
173 // ---------------------------------------------------------------------------
175 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
176 // Check we accept this video
177 const acceptParameters = {
178 liveVideoBody: req.body,
179 user: res.locals.oauth.token.User
181 const acceptedResult = await Hooks.wrapFun(
182 isLocalLiveVideoAccepted,
184 'filter:api.live-video.create.accept.result'
187 if (!acceptedResult || acceptedResult.accepted !== true) {
188 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
191 status: HttpStatusCode.FORBIDDEN_403,
192 message: acceptedResult.errorMessage || 'Refused local live video'