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, isVideoPrivacyValid } 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'),
69 body('replaySettings.privacy')
71 .customSanitizer(toIntOrNull)
72 .custom(isVideoPrivacyValid),
76 .customSanitizer(toBooleanOrNull)
77 .custom(isBooleanValid).withMessage('Should have a valid permanentLive boolean'),
81 .customSanitizer(toIntOrNull)
82 .custom(isLiveLatencyModeValid),
84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
87 if (CONFIG.LIVE.ENABLED !== true) {
91 status: HttpStatusCode.FORBIDDEN_403,
92 message: 'Live is not enabled on this instance',
93 type: ServerErrorCode.LIVE_NOT_ENABLED
97 const body: LiveVideoCreate = req.body
99 if (hasValidSaveReplay(body) !== true) {
103 status: HttpStatusCode.FORBIDDEN_403,
104 message: 'Saving live replay is not enabled on this instance',
105 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
109 if (hasValidLatencyMode(body) !== true) {
113 status: HttpStatusCode.FORBIDDEN_403,
114 message: 'Custom latency mode is not allowed by this instance'
118 if (body.saveReplay && !body.replaySettings?.privacy) {
122 status: HttpStatusCode.BAD_REQUEST_400,
123 message: 'Live replay is enabled but privacy replay setting is missing'
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.countLives({ remote: false, mode: 'not-ended' })
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 boolean'),
170 body('replaySettings.privacy')
172 .customSanitizer(toIntOrNull)
173 .custom(isVideoPrivacyValid),
177 .customSanitizer(toIntOrNull)
178 .custom(isLiveLatencyModeValid),
180 (req: express.Request, res: express.Response, next: express.NextFunction) => {
181 if (areValidationErrors(req, res)) return
183 const body: LiveVideoUpdate = req.body
185 if (hasValidSaveReplay(body) !== true) {
187 status: HttpStatusCode.FORBIDDEN_403,
188 message: 'Saving live replay is not allowed by this instance'
192 if (hasValidLatencyMode(body) !== true) {
194 status: HttpStatusCode.FORBIDDEN_403,
195 message: 'Custom latency mode is not allowed by this instance'
199 if (!checkLiveSettingsReplayConsistency({ res, body })) return
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 const videoLiveListSessionsValidator = [
214 (req: express.Request, res: express.Response, next: express.NextFunction) => {
215 // Check the user can manage the live
216 const user = res.locals.oauth.token.User
217 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
223 const videoLiveFindReplaySessionValidator = [
224 isValidVideoIdParam('videoId'),
226 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
227 if (areValidationErrors(req, res)) return
228 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
230 const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id)
233 status: HttpStatusCode.NOT_FOUND_404,
234 message: 'No live replay found'
238 res.locals.videoLiveSession = session
244 // ---------------------------------------------------------------------------
247 videoLiveAddValidator,
248 videoLiveUpdateValidator,
249 videoLiveListSessionsValidator,
250 videoLiveFindReplaySessionValidator,
251 videoLiveGetValidator
254 // ---------------------------------------------------------------------------
256 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
257 // Check we accept this video
258 const acceptParameters = {
259 liveVideoBody: req.body,
260 user: res.locals.oauth.token.User
262 const acceptedResult = await Hooks.wrapFun(
263 isLocalLiveVideoAccepted,
265 'filter:api.live-video.create.accept.result'
268 if (!acceptedResult || acceptedResult.accepted !== true) {
269 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
272 status: HttpStatusCode.FORBIDDEN_403,
273 message: acceptedResult.errorMessage || 'Refused local live video'
281 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
282 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
287 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
289 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
290 exists(body.latencyMode) &&
291 body.latencyMode !== LiveVideoLatencyMode.DEFAULT
297 function checkLiveSettingsReplayConsistency (options: {
298 res: express.Response
299 body: LiveVideoUpdate
301 const { res, body } = options
303 // We now save replays of this live, so replay settings are mandatory
304 if (res.locals.videoLive.saveReplay !== true && body.saveReplay === true) {
306 if (!exists(body.replaySettings)) {
308 status: HttpStatusCode.BAD_REQUEST_400,
309 message: 'Replay settings are missing now the live replay is saved'
314 if (!exists(body.replaySettings.privacy)) {
316 status: HttpStatusCode.BAD_REQUEST_400,
317 message: 'Privacy replay setting is missing now the live replay is saved'
323 // Save replay was and is not enabled, so send an error the user if it specified replay settings
324 if ((!exists(body.saveReplay) && res.locals.videoLive.saveReplay === false) || body.saveReplay === false) {
325 if (exists(body.replaySettings)) {
327 status: HttpStatusCode.BAD_REQUEST_400,
328 message: 'Cannot save replay settings since live replay is not enabled'