]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-live.ts
Merge branch 'release/4.3.0' into develop
[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'
c6c0fa6c
C
20import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
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
bb4ba6d9
C
69 body('permanentLive')
70 .optional()
71 .customSanitizer(toBooleanOrNull)
396f6f01 72 .custom(isBooleanValid).withMessage('Should have a valid permanentLive boolean'),
bb4ba6d9 73
f443a746
C
74 body('latencyMode')
75 .optional()
76 .customSanitizer(toIntOrNull)
396f6f01 77 .custom(isLiveLatencyModeValid),
f443a746 78
c6c0fa6c 79 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
bb4ba6d9
C
80 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
81
c6c0fa6c 82 if (CONFIG.LIVE.ENABLED !== true) {
a056ca48
C
83 cleanUpReqFiles(req)
84
76148b27
RK
85 return res.fail({
86 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
87 message: 'Live is not enabled on this instance',
88 type: ServerErrorCode.LIVE_NOT_ENABLED
76148b27 89 })
c6c0fa6c
C
90 }
91
f443a746
C
92 const body: LiveVideoCreate = req.body
93
94 if (hasValidSaveReplay(body) !== true) {
a056ca48
C
95 cleanUpReqFiles(req)
96
76148b27
RK
97 return res.fail({
98 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
99 message: 'Saving live replay is not enabled on this instance',
100 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
76148b27 101 })
b5b68755
C
102 }
103
f443a746
C
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
c6c0fa6c 113 const user = res.locals.oauth.token.User
f443a746 114 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
c6c0fa6c 115
a056ca48 116 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
adc94cf0 117 const totalInstanceLives = await VideoModel.countLives({ remote: false, mode: 'not-ended' })
a056ca48
C
118
119 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
120 cleanUpReqFiles(req)
121
76148b27
RK
122 return res.fail({
123 status: HttpStatusCode.FORBIDDEN_403,
124 message: 'Cannot create this live because the max instance lives limit is reached.',
3866ea02 125 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
76148b27 126 })
a056ca48
C
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
76148b27
RK
136 return res.fail({
137 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
138 message: 'Cannot create this live because the max user lives limit is reached.',
139 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
76148b27 140 })
a056ca48
C
141 }
142 }
143
3cabf353
C
144 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
145
c6c0fa6c
C
146 return next()
147 }
148])
149
b5b68755
C
150const videoLiveUpdateValidator = [
151 body('saveReplay')
152 .optional()
153 .customSanitizer(toBooleanOrNull)
396f6f01 154 .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
b5b68755 155
f443a746
C
156 body('latencyMode')
157 .optional()
158 .customSanitizer(toIntOrNull)
396f6f01 159 .custom(isLiveLatencyModeValid),
f443a746 160
b5b68755 161 (req: express.Request, res: express.Response, next: express.NextFunction) => {
b5b68755
C
162 if (areValidationErrors(req, res)) return
163
f443a746
C
164 const body: LiveVideoUpdate = req.body
165
f443a746 166 if (hasValidSaveReplay(body) !== true) {
76148b27
RK
167 return res.fail({
168 status: HttpStatusCode.FORBIDDEN_403,
f443a746
C
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'
76148b27 177 })
b5b68755
C
178 }
179
180 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
76148b27 181 return res.fail({ message: 'Cannot update a live that has already started' })
b5b68755
C
182 }
183
af4ae64f
C
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
b5b68755
C
188 return next()
189 }
190]
191
26e3e98f
C
192const videoLiveListSessionsValidator = [
193 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26e3e98f
C
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
202const videoLiveFindReplaySessionValidator = [
203 isValidVideoIdParam('videoId'),
204
205 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26e3e98f
C
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
c6c0fa6c
C
223// ---------------------------------------------------------------------------
224
225export {
226 videoLiveAddValidator,
b5b68755 227 videoLiveUpdateValidator,
26e3e98f
C
228 videoLiveListSessionsValidator,
229 videoLiveFindReplaySessionValidator,
c6c0fa6c
C
230 videoLiveGetValidator
231}
3cabf353
C
232
233// ---------------------------------------------------------------------------
234
235async 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
76148b27
RK
250 res.fail({
251 status: HttpStatusCode.FORBIDDEN_403,
252 message: acceptedResult.errorMessage || 'Refused local live video'
253 })
3cabf353
C
254 return false
255 }
256
257 return true
258}
f443a746
C
259
260function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
261 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
262
263 return true
264}
265
266function 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}