]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-live.ts
Improve views/viewers documentation
[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'
f443a746
C
9import {
10 HttpStatusCode,
11 LiveVideoCreate,
12 LiveVideoLatencyMode,
13 LiveVideoUpdate,
14 ServerErrorCode,
15 UserRight,
16 VideoState
17} from '@shared/models'
18import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
c6c0fa6c
C
19import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
20import { cleanUpReqFiles } from '../../../helpers/express-utils'
21import { logger } from '../../../helpers/logger'
22import { CONFIG } from '../../../initializers/config'
d4a8e7a6
C
23import {
24 areValidationErrors,
25 checkUserCanManageVideo,
26 doesVideoChannelOfAccountExist,
27 doesVideoExist,
28 isValidVideoIdParam
29} from '../shared'
c6c0fa6c 30import { getCommonVideoEditAttributes } from './videos'
c6c0fa6c
C
31
32const videoLiveGetValidator = [
d4a8e7a6 33 isValidVideoIdParam('videoId'),
c6c0fa6c
C
34
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
af4ae64f 36 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
c6c0fa6c
C
37
38 if (areValidationErrors(req, res)) return
39 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
40
af4ae64f 41 // Check if the user who did the request is able to get the live info
c6c0fa6c 42 const user = res.locals.oauth.token.User
af4ae64f 43 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
c6c0fa6c
C
44
45 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
76148b27
RK
46 if (!videoLive) {
47 return res.fail({
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Live video not found'
50 })
51 }
c6c0fa6c
C
52
53 res.locals.videoLive = videoLive
54
55 return next()
56 }
57]
58
59const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
60 body('channelId')
61 .customSanitizer(toIntOrNull)
62 .custom(isIdValid).withMessage('Should have correct video channel id'),
63
64 body('name')
7dab0bd6
RK
65 .custom(isVideoNameValid).withMessage(
66 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
67 ),
c6c0fa6c 68
b5b68755
C
69 body('saveReplay')
70 .optional()
71 .customSanitizer(toBooleanOrNull)
72 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
73
bb4ba6d9
C
74 body('permanentLive')
75 .optional()
76 .customSanitizer(toBooleanOrNull)
77 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
78
f443a746
C
79 body('latencyMode')
80 .optional()
81 .customSanitizer(toIntOrNull)
82 .custom(isLiveLatencyModeValid)
83 .withMessage('Should have a valid latency mode attribute'),
84
c6c0fa6c
C
85 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
86 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
87
bb4ba6d9
C
88 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
89
c6c0fa6c 90 if (CONFIG.LIVE.ENABLED !== true) {
a056ca48
C
91 cleanUpReqFiles(req)
92
76148b27
RK
93 return res.fail({
94 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
95 message: 'Live is not enabled on this instance',
96 type: ServerErrorCode.LIVE_NOT_ENABLED
76148b27 97 })
c6c0fa6c
C
98 }
99
f443a746
C
100 const body: LiveVideoCreate = req.body
101
102 if (hasValidSaveReplay(body) !== true) {
a056ca48
C
103 cleanUpReqFiles(req)
104
76148b27
RK
105 return res.fail({
106 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
107 message: 'Saving live replay is not enabled on this instance',
108 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
76148b27 109 })
b5b68755
C
110 }
111
f443a746
C
112 if (hasValidLatencyMode(body) !== true) {
113 cleanUpReqFiles(req)
114
115 return res.fail({
116 status: HttpStatusCode.FORBIDDEN_403,
117 message: 'Custom latency mode is not allowed by this instance'
118 })
119 }
120
121 if (body.permanentLive && body.saveReplay) {
bb4ba6d9
C
122 cleanUpReqFiles(req)
123
76148b27 124 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
bb4ba6d9 125 }
c6c0fa6c
C
126
127 const user = res.locals.oauth.token.User
f443a746 128 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
c6c0fa6c 129
a056ca48
C
130 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
131 const totalInstanceLives = await VideoModel.countLocalLives()
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)
168 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
169
f443a746
C
170 body('latencyMode')
171 .optional()
172 .customSanitizer(toIntOrNull)
173 .custom(isLiveLatencyModeValid)
174 .withMessage('Should have a valid latency mode attribute'),
175
b5b68755
C
176 (req: express.Request, res: express.Response, next: express.NextFunction) => {
177 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
178
179 if (areValidationErrors(req, res)) return
180
f443a746
C
181 const body: LiveVideoUpdate = req.body
182
183 if (body.permanentLive && body.saveReplay) {
76148b27 184 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
bb4ba6d9
C
185 }
186
f443a746 187 if (hasValidSaveReplay(body) !== true) {
76148b27
RK
188 return res.fail({
189 status: HttpStatusCode.FORBIDDEN_403,
f443a746
C
190 message: 'Saving live replay is not allowed by this instance'
191 })
192 }
193
194 if (hasValidLatencyMode(body) !== true) {
195 return res.fail({
196 status: HttpStatusCode.FORBIDDEN_403,
197 message: 'Custom latency mode is not allowed by this instance'
76148b27 198 })
b5b68755
C
199 }
200
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
c6c0fa6c
C
213// ---------------------------------------------------------------------------
214
215export {
216 videoLiveAddValidator,
b5b68755 217 videoLiveUpdateValidator,
c6c0fa6c
C
218 videoLiveGetValidator
219}
3cabf353
C
220
221// ---------------------------------------------------------------------------
222
223async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
224 // Check we accept this video
225 const acceptParameters = {
226 liveVideoBody: req.body,
227 user: res.locals.oauth.token.User
228 }
229 const acceptedResult = await Hooks.wrapFun(
230 isLocalLiveVideoAccepted,
231 acceptParameters,
232 'filter:api.live-video.create.accept.result'
233 )
234
235 if (!acceptedResult || acceptedResult.accepted !== true) {
236 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
237
76148b27
RK
238 res.fail({
239 status: HttpStatusCode.FORBIDDEN_403,
240 message: acceptedResult.errorMessage || 'Refused local live video'
241 })
3cabf353
C
242 return false
243 }
244
245 return true
246}
f443a746
C
247
248function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
249 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
250
251 return true
252}
253
254function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
255 if (
256 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
257 exists(body.latencyMode) &&
258 body.latencyMode !== LiveVideoLatencyMode.DEFAULT
259 ) return false
260
261 return true
262}