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'
9 import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
18 } from '@shared/models'
19 import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
20 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
21 import { cleanUpReqFiles } from '../../../helpers/express-utils'
22 import { logger } from '../../../helpers/logger'
23 import { CONFIG } from '../../../initializers/config'
26 checkUserCanManageVideo,
27 doesVideoChannelOfAccountExist,
31 import { getCommonVideoEditAttributes } from './videos'
33 const videoLiveGetValidator = [
34 isValidVideoIdParam('videoId'),
36 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 if (areValidationErrors(req, res)) return
38 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
40 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
43 status: HttpStatusCode.NOT_FOUND_404,
44 message: 'Live video not found'
48 res.locals.videoLive = videoLive
54 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
56 .customSanitizer(toIntOrNull)
60 .custom(isVideoNameValid).withMessage(
61 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
66 .customSanitizer(toBooleanOrNull)
67 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
71 .customSanitizer(toBooleanOrNull)
72 .custom(isBooleanValid).withMessage('Should have a valid permanentLive boolean'),
76 .customSanitizer(toIntOrNull)
77 .custom(isLiveLatencyModeValid),
79 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
80 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
82 if (CONFIG.LIVE.ENABLED !== true) {
86 status: HttpStatusCode.FORBIDDEN_403,
87 message: 'Live is not enabled on this instance',
88 type: ServerErrorCode.LIVE_NOT_ENABLED
92 const body: LiveVideoCreate = req.body
94 if (hasValidSaveReplay(body) !== true) {
98 status: HttpStatusCode.FORBIDDEN_403,
99 message: 'Saving live replay is not enabled on this instance',
100 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
104 if (hasValidLatencyMode(body) !== true) {
108 status: HttpStatusCode.FORBIDDEN_403,
109 message: 'Custom latency mode is not allowed by this instance'
113 const user = res.locals.oauth.token.User
114 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
116 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
117 const totalInstanceLives = await VideoModel.countLives({ remote: false, mode: 'not-ended' })
119 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
123 status: HttpStatusCode.FORBIDDEN_403,
124 message: 'Cannot create this live because the max instance lives limit is reached.',
125 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
130 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
131 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
133 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
137 status: HttpStatusCode.FORBIDDEN_403,
138 message: 'Cannot create this live because the max user lives limit is reached.',
139 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
144 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
150 const videoLiveUpdateValidator = [
153 .customSanitizer(toBooleanOrNull)
154 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
158 .customSanitizer(toIntOrNull)
159 .custom(isLiveLatencyModeValid),
161 (req: express.Request, res: express.Response, next: express.NextFunction) => {
162 if (areValidationErrors(req, res)) return
164 const body: LiveVideoUpdate = req.body
166 if (hasValidSaveReplay(body) !== true) {
168 status: HttpStatusCode.FORBIDDEN_403,
169 message: 'Saving live replay is not allowed by this instance'
173 if (hasValidLatencyMode(body) !== true) {
175 status: HttpStatusCode.FORBIDDEN_403,
176 message: 'Custom latency mode is not allowed by this instance'
180 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
181 return res.fail({ message: 'Cannot update a live that has already started' })
184 // Check the user can manage the live
185 const user = res.locals.oauth.token.User
186 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
192 const videoLiveListSessionsValidator = [
193 (req: express.Request, res: express.Response, next: express.NextFunction) => {
194 // Check the user can manage the live
195 const user = res.locals.oauth.token.User
196 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
202 const videoLiveFindReplaySessionValidator = [
203 isValidVideoIdParam('videoId'),
205 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
206 if (areValidationErrors(req, res)) return
207 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
209 const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id)
212 status: HttpStatusCode.NOT_FOUND_404,
213 message: 'No live replay found'
217 res.locals.videoLiveSession = session
223 // ---------------------------------------------------------------------------
226 videoLiveAddValidator,
227 videoLiveUpdateValidator,
228 videoLiveListSessionsValidator,
229 videoLiveFindReplaySessionValidator,
230 videoLiveGetValidator
233 // ---------------------------------------------------------------------------
235 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
236 // Check we accept this video
237 const acceptParameters = {
238 liveVideoBody: req.body,
239 user: res.locals.oauth.token.User
241 const acceptedResult = await Hooks.wrapFun(
242 isLocalLiveVideoAccepted,
244 'filter:api.live-video.create.accept.result'
247 if (!acceptedResult || acceptedResult.accepted !== true) {
248 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
251 status: HttpStatusCode.FORBIDDEN_403,
252 message: acceptedResult.errorMessage || 'Refused local live video'
260 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
261 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
266 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
268 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
269 exists(body.latencyMode) &&
270 body.latencyMode !== LiveVideoLatencyMode.DEFAULT