1 import 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, ServerErrorCode, UserRight, VideoState } from '@shared/models'
9 import { isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
10 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
11 import { cleanUpReqFiles } from '../../../helpers/express-utils'
12 import { logger } from '../../../helpers/logger'
13 import { CONFIG } from '../../../initializers/config'
16 checkUserCanManageVideo,
17 doesVideoChannelOfAccountExist,
21 import { getCommonVideoEditAttributes } from './videos'
23 const videoLiveGetValidator = [
24 isValidVideoIdParam('videoId'),
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
29 if (areValidationErrors(req, res)) return
30 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
32 // Check if the user who did the request is able to get the live info
33 const user = res.locals.oauth.token.User
34 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
36 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
39 status: HttpStatusCode.NOT_FOUND_404,
40 message: 'Live video not found'
44 res.locals.videoLive = videoLive
50 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
52 .customSanitizer(toIntOrNull)
53 .custom(isIdValid).withMessage('Should have correct video channel id'),
56 .custom(isVideoNameValid).withMessage(
57 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
62 .customSanitizer(toBooleanOrNull)
63 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
67 .customSanitizer(toBooleanOrNull)
68 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
70 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
71 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
73 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
75 if (CONFIG.LIVE.ENABLED !== true) {
79 status: HttpStatusCode.FORBIDDEN_403,
80 message: 'Live is not enabled on this instance',
81 type: ServerErrorCode.LIVE_NOT_ENABLED
85 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
89 status: HttpStatusCode.FORBIDDEN_403,
90 message: 'Saving live replay is not enabled on this instance',
91 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
95 if (req.body.permanentLive && req.body.saveReplay) {
98 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
101 const user = res.locals.oauth.token.User
102 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
104 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
105 const totalInstanceLives = await VideoModel.countLocalLives()
107 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
111 status: HttpStatusCode.FORBIDDEN_403,
112 message: 'Cannot create this live because the max instance lives limit is reached.',
113 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
118 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
119 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
121 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
125 status: HttpStatusCode.FORBIDDEN_403,
126 message: 'Cannot create this live because the max user lives limit is reached.',
127 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
132 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
138 const videoLiveUpdateValidator = [
141 .customSanitizer(toBooleanOrNull)
142 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
144 (req: express.Request, res: express.Response, next: express.NextFunction) => {
145 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
147 if (areValidationErrors(req, res)) return
149 if (req.body.permanentLive && req.body.saveReplay) {
150 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
153 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
155 status: HttpStatusCode.FORBIDDEN_403,
156 message: 'Saving live replay is not allowed instance'
160 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
161 return res.fail({ message: 'Cannot update a live that has already started' })
164 // Check the user can manage the live
165 const user = res.locals.oauth.token.User
166 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
172 // ---------------------------------------------------------------------------
175 videoLiveAddValidator,
176 videoLiveUpdateValidator,
177 videoLiveGetValidator
180 // ---------------------------------------------------------------------------
182 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
183 // Check we accept this video
184 const acceptParameters = {
185 liveVideoBody: req.body,
186 user: res.locals.oauth.token.User
188 const acceptedResult = await Hooks.wrapFun(
189 isLocalLiveVideoAccepted,
191 'filter:api.live-video.create.accept.result'
194 if (!acceptedResult || acceptedResult.accepted !== true) {
195 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
198 status: HttpStatusCode.FORBIDDEN_403,
199 message: acceptedResult.errorMessage || 'Refused local live video'