]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-live.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-live.ts
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'
10 import {
11 HttpStatusCode,
12 LiveVideoCreate,
13 LiveVideoLatencyMode,
14 LiveVideoUpdate,
15 ServerErrorCode,
16 UserRight,
17 VideoState
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'
24 import {
25 areValidationErrors,
26 checkUserCanManageVideo,
27 doesVideoChannelOfAccountExist,
28 doesVideoExist,
29 isValidVideoIdParam
30 } from '../shared'
31 import { getCommonVideoEditAttributes } from './videos'
32
33 const videoLiveGetValidator = [
34 isValidVideoIdParam('videoId'),
35
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
39
40 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
41 if (!videoLive) {
42 return res.fail({
43 status: HttpStatusCode.NOT_FOUND_404,
44 message: 'Live video not found'
45 })
46 }
47
48 res.locals.videoLive = videoLive
49
50 return next()
51 }
52 ]
53
54 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
55 body('channelId')
56 .customSanitizer(toIntOrNull)
57 .custom(isIdValid),
58
59 body('name')
60 .custom(isVideoNameValid).withMessage(
61 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
62 ),
63
64 body('saveReplay')
65 .optional()
66 .customSanitizer(toBooleanOrNull)
67 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
68
69 body('permanentLive')
70 .optional()
71 .customSanitizer(toBooleanOrNull)
72 .custom(isBooleanValid).withMessage('Should have a valid permanentLive boolean'),
73
74 body('latencyMode')
75 .optional()
76 .customSanitizer(toIntOrNull)
77 .custom(isLiveLatencyModeValid),
78
79 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
80 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
81
82 if (CONFIG.LIVE.ENABLED !== true) {
83 cleanUpReqFiles(req)
84
85 return res.fail({
86 status: HttpStatusCode.FORBIDDEN_403,
87 message: 'Live is not enabled on this instance',
88 type: ServerErrorCode.LIVE_NOT_ENABLED
89 })
90 }
91
92 const body: LiveVideoCreate = req.body
93
94 if (hasValidSaveReplay(body) !== true) {
95 cleanUpReqFiles(req)
96
97 return res.fail({
98 status: HttpStatusCode.FORBIDDEN_403,
99 message: 'Saving live replay is not enabled on this instance',
100 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
101 })
102 }
103
104 if (hasValidLatencyMode(body) !== true) {
105 cleanUpReqFiles(req)
106
107 return res.fail({
108 status: HttpStatusCode.FORBIDDEN_403,
109 message: 'Custom latency mode is not allowed by this instance'
110 })
111 }
112
113 const user = res.locals.oauth.token.User
114 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
115
116 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
117 const totalInstanceLives = await VideoModel.countLives({ remote: false, mode: 'not-ended' })
118
119 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
120 cleanUpReqFiles(req)
121
122 return res.fail({
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
126 })
127 }
128 }
129
130 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
131 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
132
133 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
134 cleanUpReqFiles(req)
135
136 return res.fail({
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
140 })
141 }
142 }
143
144 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
145
146 return next()
147 }
148 ])
149
150 const videoLiveUpdateValidator = [
151 body('saveReplay')
152 .optional()
153 .customSanitizer(toBooleanOrNull)
154 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
155
156 body('latencyMode')
157 .optional()
158 .customSanitizer(toIntOrNull)
159 .custom(isLiveLatencyModeValid),
160
161 (req: express.Request, res: express.Response, next: express.NextFunction) => {
162 if (areValidationErrors(req, res)) return
163
164 const body: LiveVideoUpdate = req.body
165
166 if (hasValidSaveReplay(body) !== true) {
167 return res.fail({
168 status: HttpStatusCode.FORBIDDEN_403,
169 message: 'Saving live replay is not allowed by this instance'
170 })
171 }
172
173 if (hasValidLatencyMode(body) !== true) {
174 return res.fail({
175 status: HttpStatusCode.FORBIDDEN_403,
176 message: 'Custom latency mode is not allowed by this instance'
177 })
178 }
179
180 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
181 return res.fail({ message: 'Cannot update a live that has already started' })
182 }
183
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
187
188 return next()
189 }
190 ]
191
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
197
198 return next()
199 }
200 ]
201
202 const videoLiveFindReplaySessionValidator = [
203 isValidVideoIdParam('videoId'),
204
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
208
209 const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id)
210 if (!session) {
211 return res.fail({
212 status: HttpStatusCode.NOT_FOUND_404,
213 message: 'No live replay found'
214 })
215 }
216
217 res.locals.videoLiveSession = session
218
219 return next()
220 }
221 ]
222
223 // ---------------------------------------------------------------------------
224
225 export {
226 videoLiveAddValidator,
227 videoLiveUpdateValidator,
228 videoLiveListSessionsValidator,
229 videoLiveFindReplaySessionValidator,
230 videoLiveGetValidator
231 }
232
233 // ---------------------------------------------------------------------------
234
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
240 }
241 const acceptedResult = await Hooks.wrapFun(
242 isLocalLiveVideoAccepted,
243 acceptParameters,
244 'filter:api.live-video.create.accept.result'
245 )
246
247 if (!acceptedResult || acceptedResult.accepted !== true) {
248 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
249
250 res.fail({
251 status: HttpStatusCode.FORBIDDEN_403,
252 message: acceptedResult.errorMessage || 'Refused local live video'
253 })
254 return false
255 }
256
257 return true
258 }
259
260 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
261 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
262
263 return true
264 }
265
266 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
267 if (
268 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
269 exists(body.latencyMode) &&
270 body.latencyMode !== LiveVideoLatencyMode.DEFAULT
271 ) return false
272
273 return true
274 }