]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-live.ts
Begin live tests
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-live.ts
CommitLineData
c6c0fa6c
C
1import * as express from 'express'
2import { body, param } from 'express-validator'
3import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos'
b5b68755 4import { VideoLiveModel } from '@server/models/video/video-live'
a056ca48 5import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
b5b68755 6import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
c6c0fa6c
C
7import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
8import { cleanUpReqFiles } from '../../../helpers/express-utils'
9import { logger } from '../../../helpers/logger'
10import { CONFIG } from '../../../initializers/config'
11import { areValidationErrors } from '../utils'
12import { getCommonVideoEditAttributes } from './videos'
a056ca48 13import { VideoModel } from '@server/models/video/video'
c6c0fa6c
C
14
15const videoLiveGetValidator = [
16 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
17
18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
af4ae64f 19 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
c6c0fa6c
C
20
21 if (areValidationErrors(req, res)) return
22 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
23
af4ae64f 24 // Check if the user who did the request is able to get the live info
c6c0fa6c 25 const user = res.locals.oauth.token.User
af4ae64f 26 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
c6c0fa6c
C
27
28 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
29 if (!videoLive) return res.sendStatus(404)
30
31 res.locals.videoLive = videoLive
32
33 return next()
34 }
35]
36
37const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
38 body('channelId')
39 .customSanitizer(toIntOrNull)
40 .custom(isIdValid).withMessage('Should have correct video channel id'),
41
42 body('name')
43 .custom(isVideoNameValid).withMessage('Should have a valid name'),
44
b5b68755
C
45 body('saveReplay')
46 .optional()
47 .customSanitizer(toBooleanOrNull)
48 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
49
c6c0fa6c
C
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
52
53 if (CONFIG.LIVE.ENABLED !== true) {
a056ca48
C
54 cleanUpReqFiles(req)
55
c6c0fa6c
C
56 return res.status(403)
57 .json({ error: 'Live is not enabled on this instance' })
58 }
59
b5b68755 60 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
a056ca48
C
61 cleanUpReqFiles(req)
62
b5b68755
C
63 return res.status(403)
64 .json({ error: 'Saving live replay is not allowed instance' })
65 }
66
c6c0fa6c
C
67 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
68
69 const user = res.locals.oauth.token.User
70 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
71
a056ca48
C
72 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
73 const totalInstanceLives = await VideoModel.countLocalLives()
74
75 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
76 cleanUpReqFiles(req)
77
78 return res.status(403)
79 .json({
80 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
81 error: 'Cannot create this live because the max instance lives limit is reached.'
82 })
83 }
84 }
85
86 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
87 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
88
89 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
90 cleanUpReqFiles(req)
91
92 return res.status(403)
93 .json({
94 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
95 error: 'Cannot create this live because the max user lives limit is reached.'
96 })
97 }
98 }
99
c6c0fa6c
C
100 return next()
101 }
102])
103
b5b68755
C
104const videoLiveUpdateValidator = [
105 body('saveReplay')
106 .optional()
107 .customSanitizer(toBooleanOrNull)
108 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
109
110 (req: express.Request, res: express.Response, next: express.NextFunction) => {
111 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
112
113 if (areValidationErrors(req, res)) return
114
115 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
116 return res.status(403)
117 .json({ error: 'Saving live replay is not allowed instance' })
118 }
119
120 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
121 return res.status(400)
122 .json({ error: 'Cannot update a live that has already started' })
123 }
124
af4ae64f
C
125 // Check the user can manage the live
126 const user = res.locals.oauth.token.User
127 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
128
b5b68755
C
129 return next()
130 }
131]
132
c6c0fa6c
C
133// ---------------------------------------------------------------------------
134
135export {
136 videoLiveAddValidator,
b5b68755 137 videoLiveUpdateValidator,
c6c0fa6c
C
138 videoLiveGetValidator
139}