]>
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) | |
59 | .custom(isIdValid).withMessage('Should have correct video channel id'), | |
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) | |
69 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
70 | ||
bb4ba6d9 C |
71 | body('permanentLive') |
72 | .optional() | |
73 | .customSanitizer(toBooleanOrNull) | |
74 | .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'), | |
75 | ||
f443a746 C |
76 | body('latencyMode') |
77 | .optional() | |
78 | .customSanitizer(toIntOrNull) | |
79 | .custom(isLiveLatencyModeValid) | |
80 | .withMessage('Should have a valid latency mode attribute'), | |
81 | ||
c6c0fa6c C |
82 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
83 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | |
84 | ||
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 | ||
c6c0fa6c | 118 | const user = res.locals.oauth.token.User |
f443a746 | 119 | if (!await doesVideoChannelOfAccountExist(body.channelId, user, res)) return cleanUpReqFiles(req) |
c6c0fa6c | 120 | |
a056ca48 C |
121 | if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) { |
122 | const totalInstanceLives = await VideoModel.countLocalLives() | |
123 | ||
124 | if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) { | |
125 | cleanUpReqFiles(req) | |
126 | ||
76148b27 RK |
127 | return res.fail({ |
128 | status: HttpStatusCode.FORBIDDEN_403, | |
129 | message: 'Cannot create this live because the max instance lives limit is reached.', | |
3866ea02 | 130 | type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED |
76148b27 | 131 | }) |
a056ca48 C |
132 | } |
133 | } | |
134 | ||
135 | if (CONFIG.LIVE.MAX_USER_LIVES !== -1) { | |
136 | const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id) | |
137 | ||
138 | if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) { | |
139 | cleanUpReqFiles(req) | |
140 | ||
76148b27 RK |
141 | return res.fail({ |
142 | status: HttpStatusCode.FORBIDDEN_403, | |
c756bae0 RK |
143 | message: 'Cannot create this live because the max user lives limit is reached.', |
144 | type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED | |
76148b27 | 145 | }) |
a056ca48 C |
146 | } |
147 | } | |
148 | ||
3cabf353 C |
149 | if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req) |
150 | ||
c6c0fa6c C |
151 | return next() |
152 | } | |
153 | ]) | |
154 | ||
b5b68755 C |
155 | const videoLiveUpdateValidator = [ |
156 | body('saveReplay') | |
157 | .optional() | |
158 | .customSanitizer(toBooleanOrNull) | |
159 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
160 | ||
f443a746 C |
161 | body('latencyMode') |
162 | .optional() | |
163 | .customSanitizer(toIntOrNull) | |
164 | .custom(isLiveLatencyModeValid) | |
165 | .withMessage('Should have a valid latency mode attribute'), | |
166 | ||
b5b68755 C |
167 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
168 | logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body }) | |
169 | ||
170 | if (areValidationErrors(req, res)) return | |
171 | ||
f443a746 C |
172 | const body: LiveVideoUpdate = req.body |
173 | ||
f443a746 | 174 | if (hasValidSaveReplay(body) !== true) { |
76148b27 RK |
175 | return res.fail({ |
176 | status: HttpStatusCode.FORBIDDEN_403, | |
f443a746 C |
177 | message: 'Saving live replay is not allowed by this instance' |
178 | }) | |
179 | } | |
180 | ||
181 | if (hasValidLatencyMode(body) !== true) { | |
182 | return res.fail({ | |
183 | status: HttpStatusCode.FORBIDDEN_403, | |
184 | message: 'Custom latency mode is not allowed by this instance' | |
76148b27 | 185 | }) |
b5b68755 C |
186 | } |
187 | ||
188 | if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) { | |
76148b27 | 189 | return res.fail({ message: 'Cannot update a live that has already started' }) |
b5b68755 C |
190 | } |
191 | ||
af4ae64f C |
192 | // Check the user can manage the live |
193 | const user = res.locals.oauth.token.User | |
194 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return | |
195 | ||
b5b68755 C |
196 | return next() |
197 | } | |
198 | ] | |
199 | ||
26e3e98f C |
200 | const videoLiveListSessionsValidator = [ |
201 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
202 | logger.debug('Checking videoLiveListSessionsValidator parameters', { parameters: req.params }) | |
203 | ||
204 | // Check the user can manage the live | |
205 | const user = res.locals.oauth.token.User | |
206 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return | |
207 | ||
208 | return next() | |
209 | } | |
210 | ] | |
211 | ||
212 | const videoLiveFindReplaySessionValidator = [ | |
213 | isValidVideoIdParam('videoId'), | |
214 | ||
215 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
216 | logger.debug('Checking videoLiveFindReplaySessionValidator parameters', { parameters: req.params }) | |
217 | ||
218 | if (areValidationErrors(req, res)) return | |
219 | if (!await doesVideoExist(req.params.videoId, res, 'id')) return | |
220 | ||
221 | const session = await VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id) | |
222 | if (!session) { | |
223 | return res.fail({ | |
224 | status: HttpStatusCode.NOT_FOUND_404, | |
225 | message: 'No live replay found' | |
226 | }) | |
227 | } | |
228 | ||
229 | res.locals.videoLiveSession = session | |
230 | ||
231 | return next() | |
232 | } | |
233 | ] | |
234 | ||
c6c0fa6c C |
235 | // --------------------------------------------------------------------------- |
236 | ||
237 | export { | |
238 | videoLiveAddValidator, | |
b5b68755 | 239 | videoLiveUpdateValidator, |
26e3e98f C |
240 | videoLiveListSessionsValidator, |
241 | videoLiveFindReplaySessionValidator, | |
c6c0fa6c C |
242 | videoLiveGetValidator |
243 | } | |
3cabf353 C |
244 | |
245 | // --------------------------------------------------------------------------- | |
246 | ||
247 | async function isLiveVideoAccepted (req: express.Request, res: express.Response) { | |
248 | // Check we accept this video | |
249 | const acceptParameters = { | |
250 | liveVideoBody: req.body, | |
251 | user: res.locals.oauth.token.User | |
252 | } | |
253 | const acceptedResult = await Hooks.wrapFun( | |
254 | isLocalLiveVideoAccepted, | |
255 | acceptParameters, | |
256 | 'filter:api.live-video.create.accept.result' | |
257 | ) | |
258 | ||
259 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
260 | logger.info('Refused local live video.', { acceptedResult, acceptParameters }) | |
261 | ||
76148b27 RK |
262 | res.fail({ |
263 | status: HttpStatusCode.FORBIDDEN_403, | |
264 | message: acceptedResult.errorMessage || 'Refused local live video' | |
265 | }) | |
3cabf353 C |
266 | return false |
267 | } | |
268 | ||
269 | return true | |
270 | } | |
f443a746 C |
271 | |
272 | function hasValidSaveReplay (body: LiveVideoUpdate | LiveVideoCreate) { | |
273 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true) return false | |
274 | ||
275 | return true | |
276 | } | |
277 | ||
278 | function hasValidLatencyMode (body: LiveVideoUpdate | LiveVideoCreate) { | |
279 | if ( | |
280 | CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true && | |
281 | exists(body.latencyMode) && | |
282 | body.latencyMode !== LiveVideoLatencyMode.DEFAULT | |
283 | ) return false | |
284 | ||
285 | return true | |
286 | } |