1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos'
4 import { VideoLiveModel } from '@server/models/video/video-live'
5 import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
6 import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
7 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
8 import { cleanUpReqFiles } from '../../../helpers/express-utils'
9 import { logger } from '../../../helpers/logger'
10 import { CONFIG } from '../../../initializers/config'
11 import { areValidationErrors } from '../utils'
12 import { getCommonVideoEditAttributes } from './videos'
13 import { VideoModel } from '@server/models/video/video'
14 import { Hooks } from '@server/lib/plugins/hooks'
15 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
16 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
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)
32 if (!videoLive) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
34 res.locals.videoLive = videoLive
40 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
42 .customSanitizer(toIntOrNull)
43 .custom(isIdValid).withMessage('Should have correct video channel id'),
46 .custom(isVideoNameValid).withMessage('Should have a valid name'),
50 .customSanitizer(toBooleanOrNull)
51 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
55 .customSanitizer(toBooleanOrNull)
56 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
58 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
59 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
61 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
63 if (CONFIG.LIVE.ENABLED !== true) {
66 return res.status(HttpStatusCode.FORBIDDEN_403)
67 .json({ error: 'Live is not enabled on this instance' })
70 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
73 return res.status(HttpStatusCode.FORBIDDEN_403)
74 .json({ error: 'Saving live replay is not allowed instance' })
77 if (req.body.permanentLive && req.body.saveReplay) {
80 return res.status(HttpStatusCode.BAD_REQUEST_400)
81 .json({ error: 'Cannot set this live as permanent while saving its replay' })
84 const user = res.locals.oauth.token.User
85 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
87 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
88 const totalInstanceLives = await VideoModel.countLocalLives()
90 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
93 return res.status(HttpStatusCode.FORBIDDEN_403)
95 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
96 error: 'Cannot create this live because the max instance lives limit is reached.'
101 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
102 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
104 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
107 return res.status(HttpStatusCode.FORBIDDEN_403)
109 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
110 error: 'Cannot create this live because the max user lives limit is reached.'
115 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
121 const videoLiveUpdateValidator = [
124 .customSanitizer(toBooleanOrNull)
125 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
127 (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
130 if (areValidationErrors(req, res)) return
132 if (req.body.permanentLive && req.body.saveReplay) {
133 return res.status(HttpStatusCode.BAD_REQUEST_400)
134 .json({ error: 'Cannot set this live as permanent while saving its replay' })
137 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
138 return res.status(HttpStatusCode.FORBIDDEN_403)
139 .json({ error: 'Saving live replay is not allowed instance' })
142 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
143 return res.status(HttpStatusCode.BAD_REQUEST_400)
144 .json({ error: 'Cannot update a live that has already started' })
147 // Check the user can manage the live
148 const user = res.locals.oauth.token.User
149 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
155 // ---------------------------------------------------------------------------
158 videoLiveAddValidator,
159 videoLiveUpdateValidator,
160 videoLiveGetValidator
163 // ---------------------------------------------------------------------------
165 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
166 // Check we accept this video
167 const acceptParameters = {
168 liveVideoBody: req.body,
169 user: res.locals.oauth.token.User
171 const acceptedResult = await Hooks.wrapFun(
172 isLocalLiveVideoAccepted,
174 'filter:api.live-video.create.accept.result'
177 if (!acceptedResult || acceptedResult.accepted !== true) {
178 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
180 res.status(HttpStatusCode.FORBIDDEN_403)
181 .json({ error: acceptedResult.errorMessage || 'Refused local live video' })