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