1 import express from 'express'
2 import { body } from 'express-validator'
3 import { isLiveLatencyModeValid } from '@server/helpers/custom-validators/video-lives'
4 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
5 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
6 import { Hooks } from '@server/lib/plugins/hooks'
7 import { VideoModel } from '@server/models/video/video'
8 import { VideoLiveModel } from '@server/models/video/video-live'
17 } from '@shared/models'
18 import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
19 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
20 import { cleanUpReqFiles } from '../../../helpers/express-utils'
21 import { logger } from '../../../helpers/logger'
22 import { CONFIG } from '../../../initializers/config'
25 checkUserCanManageVideo,
26 doesVideoChannelOfAccountExist,
30 import { getCommonVideoEditAttributes } from './videos'
32 const videoLiveGetValidator = [
33 isValidVideoIdParam('videoId'),
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
38 if (areValidationErrors(req, res)) return
39 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
41 // Check if the user who did the request is able to get the live info
42 const user = res.locals.oauth.token.User
43 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
45 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Live video not found'
53 res.locals.videoLive = videoLive
59 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
61 .customSanitizer(toIntOrNull)
62 .custom(isIdValid).withMessage('Should have correct video channel id'),
65 .custom(isVideoNameValid).withMessage(
66 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
71 .customSanitizer(toBooleanOrNull)
72 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
76 .customSanitizer(toBooleanOrNull)
77 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
81 .customSanitizer(toIntOrNull)
82 .custom(isLiveLatencyModeValid)
83 .withMessage('Should have a valid latency mode attribute'),
85 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
86 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
88 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
90 if (CONFIG.LIVE.ENABLED !== true) {
94 status: HttpStatusCode.FORBIDDEN_403,
95 message: 'Live is not enabled on this instance',
96 type: ServerErrorCode.LIVE_NOT_ENABLED
100 const body: LiveVideoCreate = req.body
102 if (hasValidSaveReplay(body) !== true) {
106 status: HttpStatusCode.FORBIDDEN_403,
107 message: 'Saving live replay is not enabled on this instance',
108 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
112 if (hasValidLatencyMode(body) !== true) {
116 status: HttpStatusCode.FORBIDDEN_403,
117 message: 'Custom latency mode is not allowed by this instance'
121 if (body.permanentLive && body.saveReplay) {
124 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
127 const user = res.locals.oauth.token.User
128 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
130 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
131 const totalInstanceLives = await VideoModel.countLocalLives()
133 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
137 status: HttpStatusCode.FORBIDDEN_403,
138 message: 'Cannot create this live because the max instance lives limit is reached.',
139 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
144 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
145 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
147 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
151 status: HttpStatusCode.FORBIDDEN_403,
152 message: 'Cannot create this live because the max user lives limit is reached.',
153 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
158 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
164 const videoLiveUpdateValidator = [
167 .customSanitizer(toBooleanOrNull)
168 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
172 .customSanitizer(toIntOrNull)
173 .custom(isLiveLatencyModeValid)
174 .withMessage('Should have a valid latency mode attribute'),
176 (req: express.Request, res: express.Response, next: express.NextFunction) => {
177 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
179 if (areValidationErrors(req, res)) return
181 const body: LiveVideoUpdate = req.body
183 if (body.permanentLive && body.saveReplay) {
184 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
187 if (hasValidSaveReplay(body) !== true) {
189 status: HttpStatusCode.FORBIDDEN_403,
190 message: 'Saving live replay is not allowed by this instance'
194 if (hasValidLatencyMode(body) !== true) {
196 status: HttpStatusCode.FORBIDDEN_403,
197 message: 'Custom latency mode is not allowed by this instance'
201 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
202 return res.fail({ message: 'Cannot update a live that has already started' })
205 // Check the user can manage the live
206 const user = res.locals.oauth.token.User
207 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
213 // ---------------------------------------------------------------------------
216 videoLiveAddValidator,
217 videoLiveUpdateValidator,
218 videoLiveGetValidator
221 // ---------------------------------------------------------------------------
223 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
224 // Check we accept this video
225 const acceptParameters = {
226 liveVideoBody: req.body,
227 user: res.locals.oauth.token.User
229 const acceptedResult = await Hooks.wrapFun(
230 isLocalLiveVideoAccepted,
232 'filter:api.live-video.create.accept.result'
235 if (!acceptedResult || acceptedResult.accepted !== true) {
236 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
239 status: HttpStatusCode.FORBIDDEN_403,
240 message: acceptedResult.errorMessage || 'Refused local live video'
248 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
249 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
254 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
256 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
257 exists(body.latencyMode) &&
258 body.latencyMode !== LiveVideoLatencyMode.DEFAULT