]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-live.ts
cbc48fe93f0c5bd79dc137572e9ed05670b3aaf7
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-live.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos'
4 import { VideoLiveModel } from '@server/models/video/video-live'
5 import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
6 import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
7 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
8 import { cleanUpReqFiles } from '../../../helpers/express-utils'
9 import { logger } from '../../../helpers/logger'
10 import { CONFIG } from '../../../initializers/config'
11 import { areValidationErrors } from '../utils'
12 import { getCommonVideoEditAttributes } from './videos'
13 import { VideoModel } from '@server/models/video/video'
14
15 const 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) => {
19 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
20
21 if (areValidationErrors(req, res)) return
22 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
23
24 // Check if the user who did the request is able to get the live info
25 const user = res.locals.oauth.token.User
26 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
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
37 const 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
45 body('saveReplay')
46 .optional()
47 .customSanitizer(toBooleanOrNull)
48 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
49
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) {
54 cleanUpReqFiles(req)
55
56 return res.status(403)
57 .json({ error: 'Live is not enabled on this instance' })
58 }
59
60 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
61 cleanUpReqFiles(req)
62
63 return res.status(403)
64 .json({ error: 'Saving live replay is not allowed instance' })
65 }
66
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
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
100 return next()
101 }
102 ])
103
104 const 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
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
129 return next()
130 }
131 ]
132
133 // ---------------------------------------------------------------------------
134
135 export {
136 videoLiveAddValidator,
137 videoLiveUpdateValidator,
138 videoLiveGetValidator
139 }