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',
76 type: ServerErrorCode.LIVE_NOT_ENABLED
80 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
84 status: HttpStatusCode.FORBIDDEN_403,
85 message: 'Saving live replay is not enabled on this instance',
86 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
90 if (req.body.permanentLive && req.body.saveReplay) {
93 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
96 const user = res.locals.oauth.token.User
97 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
99 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
100 const totalInstanceLives = await VideoModel.countLocalLives()
102 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
106 status: HttpStatusCode.FORBIDDEN_403,
107 message: 'Cannot create this live because the max instance lives limit is reached.',
108 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
113 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
114 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
116 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
120 status: HttpStatusCode.FORBIDDEN_403,
121 message: 'Cannot create this live because the max user lives limit is reached.',
122 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
127 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
133 const videoLiveUpdateValidator = [
136 .customSanitizer(toBooleanOrNull)
137 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
139 (req: express.Request, res: express.Response, next: express.NextFunction) => {
140 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
142 if (areValidationErrors(req, res)) return
144 if (req.body.permanentLive && req.body.saveReplay) {
145 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
148 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
150 status: HttpStatusCode.FORBIDDEN_403,
151 message: 'Saving live replay is not allowed instance'
155 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
156 return res.fail({ message: 'Cannot update a live that has already started' })
159 // Check the user can manage the live
160 const user = res.locals.oauth.token.User
161 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
167 // ---------------------------------------------------------------------------
170 videoLiveAddValidator,
171 videoLiveUpdateValidator,
172 videoLiveGetValidator
175 // ---------------------------------------------------------------------------
177 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
178 // Check we accept this video
179 const acceptParameters = {
180 liveVideoBody: req.body,
181 user: res.locals.oauth.token.User
183 const acceptedResult = await Hooks.wrapFun(
184 isLocalLiveVideoAccepted,
186 'filter:api.live-video.create.accept.result'
189 if (!acceptedResult || acceptedResult.accepted !== true) {
190 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
193 status: HttpStatusCode.FORBIDDEN_403,
194 message: acceptedResult.errorMessage || 'Refused local live video'