]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
d4a8e7a6 | 2 | import { body } from 'express-validator' |
f443a746 | 3 | import { isLiveLatencyModeValid } from '@server/helpers/custom-validators/video-lives' |
10363c74 C |
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' | |
b5b68755 | 8 | import { VideoLiveModel } from '@server/models/video/video-live' |
f443a746 C |
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' | |
c6c0fa6c C |
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' | |
d4a8e7a6 C |
23 | import { |
24 | areValidationErrors, | |
25 | checkUserCanManageVideo, | |
26 | doesVideoChannelOfAccountExist, | |
27 | doesVideoExist, | |
28 | isValidVideoIdParam | |
29 | } from '../shared' | |
c6c0fa6c | 30 | import { getCommonVideoEditAttributes } from './videos' |
26e3e98f | 31 | import { VideoLiveSessionModel } from '@server/models/video/video-live-session' |
c6c0fa6c C |
32 | |
33 | const videoLiveGetValidator = [ | |
d4a8e7a6 | 34 | isValidVideoIdParam('videoId'), |
c6c0fa6c C |
35 | |
36 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
961cbe42 | 37 | logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params }) |
c6c0fa6c C |
38 | |
39 | if (areValidationErrors(req, res)) return | |
40 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return | |
41 | ||
c6c0fa6c | 42 | const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id) |
76148b27 RK |
43 | if (!videoLive) { |
44 | return res.fail({ | |
45 | status: HttpStatusCode.NOT_FOUND_404, | |
46 | message: 'Live video not found' | |
47 | }) | |
48 | } | |
c6c0fa6c C |
49 | |
50 | res.locals.videoLive = videoLive | |
51 | ||
52 | return next() | |
53 | } | |
54 | ] | |
55 | ||
56 | const videoLiveAddValidator = getCommonVideoEditAttributes().concat([ | |
57 | body('channelId') | |
58 | .customSanitizer(toIntOrNull) | |
396f6f01 | 59 | .custom(isIdValid), |
c6c0fa6c C |
60 | |
61 | body('name') | |
7dab0bd6 RK |
62 | .custom(isVideoNameValid).withMessage( |
63 | `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long` | |
64 | ), | |
c6c0fa6c | 65 | |
b5b68755 C |
66 | body('saveReplay') |
67 | .optional() | |
68 | .customSanitizer(toBooleanOrNull) | |
396f6f01 | 69 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'), |
b5b68755 | 70 | |
bb4ba6d9 C |
71 | body('permanentLive') |
72 | .optional() | |
73 | .customSanitizer(toBooleanOrNull) | |
396f6f01 | 74 | .custom(isBooleanValid).withMessage('Should have a valid permanentLive boolean'), |
bb4ba6d9 | 75 | |
f443a746 C |
76 | body('latencyMode') |
77 | .optional() | |
78 | .customSanitizer(toIntOrNull) | |
396f6f01 | 79 | .custom(isLiveLatencyModeValid), |
f443a746 | 80 | |
c6c0fa6c C |
81 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
82 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | |
83 | ||
bb4ba6d9 C |
84 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) |
85 | ||
c6c0fa6c | 86 | if (CONFIG.LIVE.ENABLED !== true) { |
a056ca48 C |
87 | cleanUpReqFiles(req) |
88 | ||
76148b27 RK |
89 | return res.fail({ |
90 | status: HttpStatusCode.FORBIDDEN_403, | |
c756bae0 RK |
91 | message: 'Live is not enabled on this instance', |
92 | type: ServerErrorCode.LIVE_NOT_ENABLED | |
76148b27 | 93 | }) |
c6c0fa6c C |
94 | } |
95 | ||
f443a746 C |
96 | const body: LiveVideoCreate = req.body |
97 | ||
98 | if (hasValidSaveReplay(body) !== true) { | |
a056ca48 C |
99 | cleanUpReqFiles(req) |
100 | ||
76148b27 RK |
101 | return res.fail({ |
102 | status: HttpStatusCode.FORBIDDEN_403, | |
c756bae0 RK |
103 | message: 'Saving live replay is not enabled on this instance', |
104 | type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY | |
76148b27 | 105 | }) |
b5b68755 C |
106 | } |
107 | ||
f443a746 C |
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 | ||
c6c0fa6c | 117 | const user = res.locals.oauth.token.User |
f443a746 | 118 | if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req) |
c6c0fa6c | 119 | |
a056ca48 | 120 | if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) { |
adc94cf0 | 121 | const totalInstanceLives = await VideoModel.countLives({ remote: false, mode: 'not-ended' }) |
a056ca48 C |
122 | |
123 | if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) { | |
124 | cleanUpReqFiles(req) | |
125 | ||
76148b27 RK |
126 | return res.fail({ |
127 | status: HttpStatusCode.FORBIDDEN_403, | |
128 | message: 'Cannot create this live because the max instance lives limit is reached.', | |
3866ea02 | 129 | type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED |
76148b27 | 130 | }) |
a056ca48 C |
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 | ||
76148b27 RK |
140 | return res.fail({ |
141 | status: HttpStatusCode.FORBIDDEN_403, | |
c756bae0 RK |
142 | message: 'Cannot create this live because the max user lives limit is reached.', |
143 | type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED | |
76148b27 | 144 | }) |
a056ca48 C |
145 | } |
146 | } | |
147 | ||
3cabf353 C |
148 | if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req) |
149 | ||
c6c0fa6c C |
150 | return next() |
151 | } | |
152 | ]) | |
153 | ||
b5b68755 C |
154 | const videoLiveUpdateValidator = [ |
155 | body('saveReplay') | |
156 | .optional() | |
157 | .customSanitizer(toBooleanOrNull) | |
396f6f01 | 158 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay boolean'), |
b5b68755 | 159 | |
f443a746 C |
160 | body('latencyMode') |
161 | .optional() | |
162 | .customSanitizer(toIntOrNull) | |
396f6f01 | 163 | .custom(isLiveLatencyModeValid), |
f443a746 | 164 | |
b5b68755 C |
165 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
166 | logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body }) | |
167 | ||
168 | if (areValidationErrors(req, res)) return | |
169 | ||
f443a746 C |
170 | const body: LiveVideoUpdate = req.body |
171 | ||
f443a746 | 172 | if (hasValidSaveReplay(body) !== true) { |
76148b27 RK |
173 | return res.fail({ |
174 | status: HttpStatusCode.FORBIDDEN_403, | |
f443a746 C |
175 | message: 'Saving live replay is not allowed by this instance' |
176 | }) | |
177 | } | |
178 | ||
179 | if (hasValidLatencyMode(body) !== true) { | |
180 | return res.fail({ | |
181 | status: HttpStatusCode.FORBIDDEN_403, | |
182 | message: 'Custom latency mode is not allowed by this instance' | |
76148b27 | 183 | }) |
b5b68755 C |
184 | } |
185 | ||
186 | if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) { | |
76148b27 | 187 | return res.fail({ message: 'Cannot update a live that has already started' }) |
b5b68755 C |
188 | } |
189 | ||
af4ae64f C |
190 | // Check the user can manage the live |
191 | const user = res.locals.oauth.token.User | |
192 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return | |
193 | ||
b5b68755 C |
194 | return next() |
195 | } | |
196 | ] | |
197 | ||
26e3e98f C |
198 | const videoLiveListSessionsValidator = [ |
199 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
200 | logger.debug('Checking videoLiveListSessionsValidator parameters', { parameters: req.params }) | |
201 | ||
202 | // Check the user can manage the live | |
203 | const user = res.locals.oauth.token.User | |
204 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return | |
205 | ||
206 | return next() | |
207 | } | |
208 | ] | |
209 | ||
210 | const videoLiveFindReplaySessionValidator = [ | |
211 | isValidVideoIdParam('videoId'), | |
212 | ||
213 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
214 | logger.debug('Checking videoLiveFindReplaySessionValidator parameters', { parameters: req.params }) | |
215 | ||
216 | if (areValidationErrors(req, res)) return | |
217 | if (!await doesVideoExist(req.params.videoId, res, 'id')) return | |
218 | ||
219 | const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id) | |
220 | if (!session) { | |
221 | return res.fail({ | |
222 | status: HttpStatusCode.NOT_FOUND_404, | |
223 | message: 'No live replay found' | |
224 | }) | |
225 | } | |
226 | ||
227 | res.locals.videoLiveSession = session | |
228 | ||
229 | return next() | |
230 | } | |
231 | ] | |
232 | ||
c6c0fa6c C |
233 | // --------------------------------------------------------------------------- |
234 | ||
235 | export { | |
236 | videoLiveAddValidator, | |
b5b68755 | 237 | videoLiveUpdateValidator, |
26e3e98f C |
238 | videoLiveListSessionsValidator, |
239 | videoLiveFindReplaySessionValidator, | |
c6c0fa6c C |
240 | videoLiveGetValidator |
241 | } | |
3cabf353 C |
242 | |
243 | // --------------------------------------------------------------------------- | |
244 | ||
245 | async function isLiveVideoAccepted (req: express.Request, res: express.Response) { | |
246 | // Check we accept this video | |
247 | const acceptParameters = { | |
248 | liveVideoBody: req.body, | |
249 | user: res.locals.oauth.token.User | |
250 | } | |
251 | const acceptedResult = await Hooks.wrapFun( | |
252 | isLocalLiveVideoAccepted, | |
253 | acceptParameters, | |
254 | 'filter:api.live-video.create.accept.result' | |
255 | ) | |
256 | ||
257 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
258 | logger.info('Refused local live video.', { acceptedResult, acceptParameters }) | |
259 | ||
76148b27 RK |
260 | res.fail({ |
261 | status: HttpStatusCode.FORBIDDEN_403, | |
262 | message: acceptedResult.errorMessage || 'Refused local live video' | |
263 | }) | |
3cabf353 C |
264 | return false |
265 | } | |
266 | ||
267 | return true | |
268 | } | |
f443a746 C |
269 | |
270 | function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) { | |
271 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false | |
272 | ||
273 | return true | |
274 | } | |
275 | ||
276 | function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) { | |
277 | if ( | |
278 | CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true && | |
279 | exists(body.latencyMode) && | |
280 | body.latencyMode !== LiveVideoLatencyMode.DEFAULT | |
281 | ) return false | |
282 | ||
283 | return true | |
284 | } |