]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-live.ts
Add Podcast RSS feeds (#5487)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-live.ts
CommitLineData
41fb13c3 1import express from 'express'
d4a8e7a6 2import { body } from 'express-validator'
f443a746 3import { isLiveLatencyModeValid } from '@server/helpers/custom-validators/video-lives'
10363c74
C
4import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
5import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
6import { Hooks } from '@server/lib/plugins/hooks'
7import { VideoModel } from '@server/models/video/video'
b5b68755 8import { VideoLiveModel } from '@server/models/video/video-live'
a85d5303 9import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
f443a746
C
10import {
11 HttpStatusCode,
12 LiveVideoCreate,
13 LiveVideoLatencyMode,
14 LiveVideoUpdate,
15 ServerErrorCode,
16 UserRight,
17 VideoState
18} from '@shared/models'
19import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
05a60d85 20import { isVideoNameValid, isVideoPrivacyValid } from '../../../helpers/custom-validators/videos'
c6c0fa6c
C
21import { cleanUpReqFiles } from '../../../helpers/express-utils'
22import { logger } from '../../../helpers/logger'
23import { CONFIG } from '../../../initializers/config'
d4a8e7a6
C
24import {
25 areValidationErrors,
26 checkUserCanManageVideo,
27 doesVideoChannelOfAccountExist,
28 doesVideoExist,
29 isValidVideoIdParam
30} from '../shared'
c6c0fa6c 31import { getCommonVideoEditAttributes } from './videos'
c6c0fa6c
C
32
33const videoLiveGetValidator = [
d4a8e7a6 34 isValidVideoIdParam('videoId'),
c6c0fa6c
C
35
36 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
c6c0fa6c
C
37 if (areValidationErrors(req, res)) return
38 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
39
c6c0fa6c 40 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
76148b27
RK
41 if (!videoLive) {
42 return res.fail({
43 status: HttpStatusCode.NOT_FOUND_404,
44 message: 'Live video not found'
45 })
46 }
c6c0fa6c
C
47
48 res.locals.videoLive = videoLive
49
50 return next()
51 }
52]
53
54const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
55 body('channelId')
56 .customSanitizer(toIntOrNull)
396f6f01 57 .custom(isIdValid),
c6c0fa6c
C
58
59 body('name')
7dab0bd6
RK
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 ),
c6c0fa6c 63
b5b68755
C
64 body('saveReplay')
65 .optional()
66 .customSanitizer(toBooleanOrNull)
396f6f01 67 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
b5b68755 68
05a60d85
W
69 body('replaySettings.privacy')
70 .optional()
71 .customSanitizer(toIntOrNull)
72 .custom(isVideoPrivacyValid),
73
bb4ba6d9
C
74 body('permanentLive')
75 .optional()
76 .customSanitizer(toBooleanOrNull)
396f6f01 77 .custom(isBooleanValid).withMessage('Should have a valid permanentLive boolean'),
bb4ba6d9 78
f443a746
C
79 body('latencyMode')
80 .optional()
81 .customSanitizer(toIntOrNull)
396f6f01 82 .custom(isLiveLatencyModeValid),
f443a746 83
c6c0fa6c 84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
bb4ba6d9
C
85 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
86
c6c0fa6c 87 if (CONFIG.LIVE.ENABLED !== true) {
a056ca48
C
88 cleanUpReqFiles(req)
89
76148b27
RK
90 return res.fail({
91 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
92 message: 'Live is not enabled on this instance',
93 type: ServerErrorCode.LIVE_NOT_ENABLED
76148b27 94 })
c6c0fa6c
C
95 }
96
f443a746
C
97 const body: LiveVideoCreate = req.body
98
99 if (hasValidSaveReplay(body) !== true) {
a056ca48
C
100 cleanUpReqFiles(req)
101
76148b27
RK
102 return res.fail({
103 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
104 message: 'Saving live replay is not enabled on this instance',
105 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
76148b27 106 })
b5b68755
C
107 }
108
f443a746
C
109 if (hasValidLatencyMode(body) !== true) {
110 cleanUpReqFiles(req)
111
112 return res.fail({
113 status: HttpStatusCode.FORBIDDEN_403,
114 message: 'Custom latency mode is not allowed by this instance'
115 })
116 }
117
0c9668f7
C
118 if (body.saveReplay && !body.replaySettings?.privacy) {
119 cleanUpReqFiles(req)
120
121 return res.fail({
122 status: HttpStatusCode.BAD_REQUEST_400,
123 message: 'Live replay is enabled but privacy replay setting is missing'
124 })
125 }
126
c6c0fa6c 127 const user = res.locals.oauth.token.User
f443a746 128 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
c6c0fa6c 129
a056ca48 130 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
adc94cf0 131 const totalInstanceLives = await VideoModel.countLives({ remote: false, mode: 'not-ended' })
a056ca48
C
132
133 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
134 cleanUpReqFiles(req)
135
76148b27
RK
136 return res.fail({
137 status: HttpStatusCode.FORBIDDEN_403,
138 message: 'Cannot create this live because the max instance lives limit is reached.',
3866ea02 139 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
76148b27 140 })
a056ca48
C
141 }
142 }
143
144 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
145 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
146
147 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
148 cleanUpReqFiles(req)
149
76148b27
RK
150 return res.fail({
151 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
152 message: 'Cannot create this live because the max user lives limit is reached.',
153 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
76148b27 154 })
a056ca48
C
155 }
156 }
157
3cabf353
C
158 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
159
c6c0fa6c
C
160 return next()
161 }
162])
163
b5b68755
C
164const videoLiveUpdateValidator = [
165 body('saveReplay')
166 .optional()
167 .customSanitizer(toBooleanOrNull)
396f6f01 168 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
b5b68755 169
05a60d85
W
170 body('replaySettings.privacy')
171 .optional()
172 .customSanitizer(toIntOrNull)
173 .custom(isVideoPrivacyValid),
174
f443a746
C
175 body('latencyMode')
176 .optional()
177 .customSanitizer(toIntOrNull)
396f6f01 178 .custom(isLiveLatencyModeValid),
f443a746 179
b5b68755 180 (req: express.Request, res: express.Response, next: express.NextFunction) => {
b5b68755
C
181 if (areValidationErrors(req, res)) return
182
f443a746
C
183 const body: LiveVideoUpdate = req.body
184
f443a746 185 if (hasValidSaveReplay(body) !== true) {
76148b27
RK
186 return res.fail({
187 status: HttpStatusCode.FORBIDDEN_403,
f443a746
C
188 message: 'Saving live replay is not allowed by this instance'
189 })
190 }
191
192 if (hasValidLatencyMode(body) !== true) {
193 return res.fail({
194 status: HttpStatusCode.FORBIDDEN_403,
195 message: 'Custom latency mode is not allowed by this instance'
76148b27 196 })
b5b68755
C
197 }
198
05a60d85
W
199 if (!checkLiveSettingsReplayConsistency({ res, body })) return
200
b5b68755 201 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
76148b27 202 return res.fail({ message: 'Cannot update a live that has already started' })
b5b68755
C
203 }
204
af4ae64f
C
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
208
b5b68755
C
209 return next()
210 }
211]
212
26e3e98f
C
213const videoLiveListSessionsValidator = [
214 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26e3e98f
C
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
218
219 return next()
220 }
221]
222
223const videoLiveFindReplaySessionValidator = [
224 isValidVideoIdParam('videoId'),
225
226 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26e3e98f
C
227 if (areValidationErrors(req, res)) return
228 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
229
230 const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id)
231 if (!session) {
232 return res.fail({
233 status: HttpStatusCode.NOT_FOUND_404,
234 message: 'No live replay found'
235 })
236 }
237
238 res.locals.videoLiveSession = session
239
240 return next()
241 }
242]
243
c6c0fa6c
C
244// ---------------------------------------------------------------------------
245
246export {
247 videoLiveAddValidator,
b5b68755 248 videoLiveUpdateValidator,
26e3e98f
C
249 videoLiveListSessionsValidator,
250 videoLiveFindReplaySessionValidator,
c6c0fa6c
C
251 videoLiveGetValidator
252}
3cabf353
C
253
254// ---------------------------------------------------------------------------
255
256async 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
261 }
262 const acceptedResult = await Hooks.wrapFun(
263 isLocalLiveVideoAccepted,
264 acceptParameters,
265 'filter:api.live-video.create.accept.result'
266 )
267
268 if (!acceptedResult || acceptedResult.accepted !== true) {
269 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
270
76148b27
RK
271 res.fail({
272 status: HttpStatusCode.FORBIDDEN_403,
273 message: acceptedResult.errorMessage || 'Refused local live video'
274 })
3cabf353
C
275 return false
276 }
277
278 return true
279}
f443a746
C
280
281function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
282 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
283
284 return true
285}
286
287function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
288 if (
289 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
290 exists(body.latencyMode) &&
291 body.latencyMode !== LiveVideoLatencyMode.DEFAULT
292 ) return false
293
294 return true
295}
05a60d85
W
296
297function checkLiveSettingsReplayConsistency (options: {
298 res: express.Response
299 body: LiveVideoUpdate
300}) {
301 const { res, body } = options
302
303 // We now save replays of this live, so replay settings are mandatory
304 if (res.locals.videoLive.saveReplay !== true && body.saveReplay === true) {
305
306 if (!exists(body.replaySettings)) {
307 res.fail({
308 status: HttpStatusCode.BAD_REQUEST_400,
309 message: 'Replay settings are missing now the live replay is saved'
310 })
311 return false
312 }
313
314 if (!exists(body.replaySettings.privacy)) {
315 res.fail({
316 status: HttpStatusCode.BAD_REQUEST_400,
317 message: 'Privacy replay setting is missing now the live replay is saved'
318 })
319 return false
320 }
321 }
322
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)) {
326 res.fail({
327 status: HttpStatusCode.BAD_REQUEST_400,
328 message: 'Cannot save replay settings since live replay is not enabled'
329 })
330 return false
331 }
332 }
333
334 return true
335}