]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-live.ts
Fix getting live by anonymous user
[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 {
10 HttpStatusCode,
11 LiveVideoCreate,
12 LiveVideoLatencyMode,
13 LiveVideoUpdate,
14 ServerErrorCode,
15 UserRight,
16 VideoState
17 } from '@shared/models'
18 import { exists, isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
19 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
20 import { cleanUpReqFiles } from '../../../helpers/express-utils'
21 import { logger } from '../../../helpers/logger'
22 import { CONFIG } from '../../../initializers/config'
23 import {
24 areValidationErrors,
25 checkUserCanManageVideo,
26 doesVideoChannelOfAccountExist,
27 doesVideoExist,
28 isValidVideoIdParam
29 } from '../shared'
30 import { getCommonVideoEditAttributes } from './videos'
31
32 const videoLiveGetValidator = [
33 isValidVideoIdParam('videoId'),
34
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params })
37
38 if (areValidationErrors(req, res)) return
39 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
40
41 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
42 if (!videoLive) {
43 return res.fail({
44 status: HttpStatusCode.NOT_FOUND_404,
45 message: 'Live video not found'
46 })
47 }
48
49 res.locals.videoLive = videoLive
50
51 return next()
52 }
53 ]
54
55 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
56 body('channelId')
57 .customSanitizer(toIntOrNull)
58 .custom(isIdValid).withMessage('Should have correct video channel id'),
59
60 body('name')
61 .custom(isVideoNameValid).withMessage(
62 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
63 ),
64
65 body('saveReplay')
66 .optional()
67 .customSanitizer(toBooleanOrNull)
68 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
69
70 body('permanentLive')
71 .optional()
72 .customSanitizer(toBooleanOrNull)
73 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
74
75 body('latencyMode')
76 .optional()
77 .customSanitizer(toIntOrNull)
78 .custom(isLiveLatencyModeValid)
79 .withMessage('Should have a valid latency mode attribute'),
80
81 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
82 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
83
84 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
85
86 if (CONFIG.LIVE.ENABLED !== true) {
87 cleanUpReqFiles(req)
88
89 return res.fail({
90 status: HttpStatusCode.FORBIDDEN_403,
91 message: 'Live is not enabled on this instance',
92 type: ServerErrorCode.LIVE_NOT_ENABLED
93 })
94 }
95
96 const body: LiveVideoCreate = req.body
97
98 if (hasValidSaveReplay(body) !== true) {
99 cleanUpReqFiles(req)
100
101 return res.fail({
102 status: HttpStatusCode.FORBIDDEN_403,
103 message: 'Saving live replay is not enabled on this instance',
104 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
105 })
106 }
107
108 if (hasValidLatencyMode(body) !== true) {
109 cleanUpReqFiles(req)
110
111 return res.fail({
112 status: HttpStatusCode.FORBIDDEN_403,
113 message: 'Custom latency mode is not allowed by this instance'
114 })
115 }
116
117 const user = res.locals.oauth.token.User
118 if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req)
119
120 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
121 const totalInstanceLives = await VideoModel.countLocalLives()
122
123 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
124 cleanUpReqFiles(req)
125
126 return res.fail({
127 status: HttpStatusCode.FORBIDDEN_403,
128 message: 'Cannot create this live because the max instance lives limit is reached.',
129 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
130 })
131 }
132 }
133
134 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
135 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
136
137 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
138 cleanUpReqFiles(req)
139
140 return res.fail({
141 status: HttpStatusCode.FORBIDDEN_403,
142 message: 'Cannot create this live because the max user lives limit is reached.',
143 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
144 })
145 }
146 }
147
148 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
149
150 return next()
151 }
152 ])
153
154 const videoLiveUpdateValidator = [
155 body('saveReplay')
156 .optional()
157 .customSanitizer(toBooleanOrNull)
158 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
159
160 body('latencyMode')
161 .optional()
162 .customSanitizer(toIntOrNull)
163 .custom(isLiveLatencyModeValid)
164 .withMessage('Should have a valid latency mode attribute'),
165
166 (req: express.Request, res: express.Response, next: express.NextFunction) => {
167 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
168
169 if (areValidationErrors(req, res)) return
170
171 const body: LiveVideoUpdate = req.body
172
173 if (body.permanentLive && body.saveReplay) {
174 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
175 }
176
177 if (hasValidSaveReplay(body) !== true) {
178 return res.fail({
179 status: HttpStatusCode.FORBIDDEN_403,
180 message: 'Saving live replay is not allowed by this instance'
181 })
182 }
183
184 if (hasValidLatencyMode(body) !== true) {
185 return res.fail({
186 status: HttpStatusCode.FORBIDDEN_403,
187 message: 'Custom latency mode is not allowed by this instance'
188 })
189 }
190
191 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
192 return res.fail({ message: 'Cannot update a live that has already started' })
193 }
194
195 // Check the user can manage the live
196 const user = res.locals.oauth.token.User
197 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
198
199 return next()
200 }
201 ]
202
203 // ---------------------------------------------------------------------------
204
205 export {
206 videoLiveAddValidator,
207 videoLiveUpdateValidator,
208 videoLiveGetValidator
209 }
210
211 // ---------------------------------------------------------------------------
212
213 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
214 // Check we accept this video
215 const acceptParameters = {
216 liveVideoBody: req.body,
217 user: res.locals.oauth.token.User
218 }
219 const acceptedResult = await Hooks.wrapFun(
220 isLocalLiveVideoAccepted,
221 acceptParameters,
222 'filter:api.live-video.create.accept.result'
223 )
224
225 if (!acceptedResult || acceptedResult.accepted !== true) {
226 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
227
228 res.fail({
229 status: HttpStatusCode.FORBIDDEN_403,
230 message: acceptedResult.errorMessage || 'Refused local live video'
231 })
232 return false
233 }
234
235 return true
236 }
237
238 function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) {
239 if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false
240
241 return true
242 }
243
244 function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) {
245 if (
246 CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
247 exists(body.latencyMode) &&
248 body.latencyMode !== LiveVideoLatencyMode.DEFAULT
249 ) return false
250
251 return true
252 }