]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-live.ts
69a14ccb16a4acb1627a84ebc777fe71f833fee1
[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 import { Hooks } from '@server/lib/plugins/hooks'
15 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
16
17 const videoLiveGetValidator = [
18 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
19
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
22
23 if (areValidationErrors(req, res)) return
24 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
25
26 // Check if the user who did the request is able to get the live info
27 const user = res.locals.oauth.token.User
28 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
29
30 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
31 if (!videoLive) return res.sendStatus(404)
32
33 res.locals.videoLive = videoLive
34
35 return next()
36 }
37 ]
38
39 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
40 body('channelId')
41 .customSanitizer(toIntOrNull)
42 .custom(isIdValid).withMessage('Should have correct video channel id'),
43
44 body('name')
45 .custom(isVideoNameValid).withMessage('Should have a valid name'),
46
47 body('saveReplay')
48 .optional()
49 .customSanitizer(toBooleanOrNull)
50 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
51
52 body('permanentLive')
53 .optional()
54 .customSanitizer(toBooleanOrNull)
55 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
56
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
59
60 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
61
62 if (CONFIG.LIVE.ENABLED !== true) {
63 cleanUpReqFiles(req)
64
65 return res.status(403)
66 .json({ error: 'Live is not enabled on this instance' })
67 }
68
69 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
70 cleanUpReqFiles(req)
71
72 return res.status(403)
73 .json({ error: 'Saving live replay is not allowed instance' })
74 }
75
76 if (req.body.permanentLive && req.body.saveReplay) {
77 cleanUpReqFiles(req)
78
79 return res.status(400)
80 .json({ error: 'Cannot set this live as permanent while saving its replay' })
81 }
82
83 const user = res.locals.oauth.token.User
84 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
85
86 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
87 const totalInstanceLives = await VideoModel.countLocalLives()
88
89 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
90 cleanUpReqFiles(req)
91
92 return res.status(403)
93 .json({
94 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
95 error: 'Cannot create this live because the max instance lives limit is reached.'
96 })
97 }
98 }
99
100 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
101 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
102
103 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
104 cleanUpReqFiles(req)
105
106 return res.status(403)
107 .json({
108 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
109 error: 'Cannot create this live because the max user lives limit is reached.'
110 })
111 }
112 }
113
114 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
115
116 return next()
117 }
118 ])
119
120 const videoLiveUpdateValidator = [
121 body('saveReplay')
122 .optional()
123 .customSanitizer(toBooleanOrNull)
124 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
125
126 (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
128
129 if (areValidationErrors(req, res)) return
130
131 if (req.body.permanentLive && req.body.saveReplay) {
132 return res.status(400)
133 .json({ error: 'Cannot set this live as permanent while saving its replay' })
134 }
135
136 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
137 return res.status(403)
138 .json({ error: 'Saving live replay is not allowed instance' })
139 }
140
141 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
142 return res.status(400)
143 .json({ error: 'Cannot update a live that has already started' })
144 }
145
146 // Check the user can manage the live
147 const user = res.locals.oauth.token.User
148 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
149
150 return next()
151 }
152 ]
153
154 // ---------------------------------------------------------------------------
155
156 export {
157 videoLiveAddValidator,
158 videoLiveUpdateValidator,
159 videoLiveGetValidator
160 }
161
162 // ---------------------------------------------------------------------------
163
164 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
165 // Check we accept this video
166 const acceptParameters = {
167 liveVideoBody: req.body,
168 user: res.locals.oauth.token.User
169 }
170 const acceptedResult = await Hooks.wrapFun(
171 isLocalLiveVideoAccepted,
172 acceptParameters,
173 'filter:api.live-video.create.accept.result'
174 )
175
176 if (!acceptedResult || acceptedResult.accepted !== true) {
177 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
178
179 res.status(403)
180 .json({ error: acceptedResult.errorMessage || 'Refused local live video' })
181
182 return false
183 }
184
185 return true
186 }