]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { body, param, query, ValidationChain } from 'express-validator' | |
3 | import { ExpressPromiseHandler } from '@server/types/express-handler' | |
4 | import { MUserAccountId } from '@server/types/models' | |
5 | import { forceNumber } from '@shared/core-utils' | |
6 | import { | |
7 | HttpStatusCode, | |
8 | UserRight, | |
9 | VideoPlaylistCreate, | |
10 | VideoPlaylistPrivacy, | |
11 | VideoPlaylistType, | |
12 | VideoPlaylistUpdate | |
13 | } from '@shared/models' | |
14 | import { | |
15 | isArrayOf, | |
16 | isIdOrUUIDValid, | |
17 | isIdValid, | |
18 | isUUIDValid, | |
19 | toCompleteUUID, | |
20 | toIntArray, | |
21 | toIntOrNull, | |
22 | toValueOrNull | |
23 | } from '../../../helpers/custom-validators/misc' | |
24 | import { | |
25 | isVideoPlaylistDescriptionValid, | |
26 | isVideoPlaylistNameValid, | |
27 | isVideoPlaylistPrivacyValid, | |
28 | isVideoPlaylistTimestampValid, | |
29 | isVideoPlaylistTypeValid | |
30 | } from '../../../helpers/custom-validators/video-playlists' | |
31 | import { isVideoImageValid } from '../../../helpers/custom-validators/videos' | |
32 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | |
33 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | |
34 | import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element' | |
35 | import { MVideoPlaylist } from '../../../types/models/video/video-playlist' | |
36 | import { authenticatePromise } from '../../auth' | |
37 | import { | |
38 | areValidationErrors, | |
39 | doesVideoChannelIdExist, | |
40 | doesVideoExist, | |
41 | doesVideoPlaylistExist, | |
42 | isValidPlaylistIdParam, | |
43 | VideoPlaylistFetchType | |
44 | } from '../shared' | |
45 | ||
46 | const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([ | |
47 | body('displayName') | |
48 | .custom(isVideoPlaylistNameValid), | |
49 | ||
50 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
51 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | |
52 | ||
53 | const body: VideoPlaylistCreate = req.body | |
54 | if (body.videoChannelId && !await doesVideoChannelIdExist(body.videoChannelId, res)) return cleanUpReqFiles(req) | |
55 | ||
56 | if ( | |
57 | !body.videoChannelId && | |
58 | (body.privacy === VideoPlaylistPrivacy.PUBLIC || body.privacy === VideoPlaylistPrivacy.UNLISTED) | |
59 | ) { | |
60 | cleanUpReqFiles(req) | |
61 | ||
62 | return res.fail({ message: 'Cannot set "public" or "unlisted" a playlist that is not assigned to a channel.' }) | |
63 | } | |
64 | ||
65 | return next() | |
66 | } | |
67 | ]) | |
68 | ||
69 | const videoPlaylistsUpdateValidator = getCommonPlaylistEditAttributes().concat([ | |
70 | isValidPlaylistIdParam('playlistId'), | |
71 | ||
72 | body('displayName') | |
73 | .optional() | |
74 | .custom(isVideoPlaylistNameValid), | |
75 | ||
76 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
77 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | |
78 | ||
79 | if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return cleanUpReqFiles(req) | |
80 | ||
81 | const videoPlaylist = getPlaylist(res) | |
82 | ||
83 | if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) { | |
84 | return cleanUpReqFiles(req) | |
85 | } | |
86 | ||
87 | const body: VideoPlaylistUpdate = req.body | |
88 | ||
89 | const newPrivacy = body.privacy || videoPlaylist.privacy | |
90 | if (newPrivacy === VideoPlaylistPrivacy.PUBLIC && | |
91 | ( | |
92 | (!videoPlaylist.videoChannelId && !body.videoChannelId) || | |
93 | body.videoChannelId === null | |
94 | ) | |
95 | ) { | |
96 | cleanUpReqFiles(req) | |
97 | ||
98 | return res.fail({ message: 'Cannot set "public" a playlist that is not assigned to a channel.' }) | |
99 | } | |
100 | ||
101 | if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) { | |
102 | cleanUpReqFiles(req) | |
103 | ||
104 | return res.fail({ message: 'Cannot update a watch later playlist.' }) | |
105 | } | |
106 | ||
107 | if (body.videoChannelId && !await doesVideoChannelIdExist(body.videoChannelId, res)) return cleanUpReqFiles(req) | |
108 | ||
109 | return next() | |
110 | } | |
111 | ]) | |
112 | ||
113 | const videoPlaylistsDeleteValidator = [ | |
114 | isValidPlaylistIdParam('playlistId'), | |
115 | ||
116 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
117 | if (areValidationErrors(req, res)) return | |
118 | ||
119 | if (!await doesVideoPlaylistExist(req.params.playlistId, res)) return | |
120 | ||
121 | const videoPlaylist = getPlaylist(res) | |
122 | if (videoPlaylist.type === VideoPlaylistType.WATCH_LATER) { | |
123 | return res.fail({ message: 'Cannot delete a watch later playlist.' }) | |
124 | } | |
125 | ||
126 | if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) { | |
127 | return | |
128 | } | |
129 | ||
130 | return next() | |
131 | } | |
132 | ] | |
133 | ||
134 | const videoPlaylistsGetValidator = (fetchType: VideoPlaylistFetchType) => { | |
135 | return [ | |
136 | isValidPlaylistIdParam('playlistId'), | |
137 | ||
138 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
139 | if (areValidationErrors(req, res)) return | |
140 | ||
141 | if (!await doesVideoPlaylistExist(req.params.playlistId, res, fetchType)) return | |
142 | ||
143 | const videoPlaylist = res.locals.videoPlaylistFull || res.locals.videoPlaylistSummary | |
144 | ||
145 | // Video is unlisted, check we used the uuid to fetch it | |
146 | if (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED) { | |
147 | if (isUUIDValid(req.params.playlistId)) return next() | |
148 | ||
149 | return res.fail({ | |
150 | status: HttpStatusCode.NOT_FOUND_404, | |
151 | message: 'Playlist not found' | |
152 | }) | |
153 | } | |
154 | ||
155 | if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
156 | await authenticatePromise(req, res) | |
157 | ||
158 | const user = res.locals.oauth ? res.locals.oauth.token.User : null | |
159 | ||
160 | if ( | |
161 | !user || | |
162 | (videoPlaylist.OwnerAccount.id !== user.Account.id && !user.hasRight(UserRight.UPDATE_ANY_VIDEO_PLAYLIST)) | |
163 | ) { | |
164 | return res.fail({ | |
165 | status: HttpStatusCode.FORBIDDEN_403, | |
166 | message: 'Cannot get this private video playlist.' | |
167 | }) | |
168 | } | |
169 | ||
170 | return next() | |
171 | } | |
172 | ||
173 | return next() | |
174 | } | |
175 | ] | |
176 | } | |
177 | ||
178 | const videoPlaylistsSearchValidator = [ | |
179 | query('search') | |
180 | .optional() | |
181 | .not().isEmpty(), | |
182 | ||
183 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
184 | if (areValidationErrors(req, res)) return | |
185 | ||
186 | return next() | |
187 | } | |
188 | ] | |
189 | ||
190 | const videoPlaylistsAddVideoValidator = [ | |
191 | isValidPlaylistIdParam('playlistId'), | |
192 | ||
193 | body('videoId') | |
194 | .customSanitizer(toCompleteUUID) | |
195 | .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid/short uuid'), | |
196 | body('startTimestamp') | |
197 | .optional() | |
198 | .custom(isVideoPlaylistTimestampValid), | |
199 | body('stopTimestamp') | |
200 | .optional() | |
201 | .custom(isVideoPlaylistTimestampValid), | |
202 | ||
203 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
204 | if (areValidationErrors(req, res)) return | |
205 | ||
206 | if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return | |
207 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return | |
208 | ||
209 | const videoPlaylist = getPlaylist(res) | |
210 | ||
211 | if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) { | |
212 | return | |
213 | } | |
214 | ||
215 | return next() | |
216 | } | |
217 | ] | |
218 | ||
219 | const videoPlaylistsUpdateOrRemoveVideoValidator = [ | |
220 | isValidPlaylistIdParam('playlistId'), | |
221 | param('playlistElementId') | |
222 | .customSanitizer(toCompleteUUID) | |
223 | .custom(isIdValid).withMessage('Should have an element id/uuid/short uuid'), | |
224 | body('startTimestamp') | |
225 | .optional() | |
226 | .custom(isVideoPlaylistTimestampValid), | |
227 | body('stopTimestamp') | |
228 | .optional() | |
229 | .custom(isVideoPlaylistTimestampValid), | |
230 | ||
231 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
232 | if (areValidationErrors(req, res)) return | |
233 | ||
234 | if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return | |
235 | ||
236 | const videoPlaylist = getPlaylist(res) | |
237 | ||
238 | const videoPlaylistElement = await VideoPlaylistElementModel.loadById(req.params.playlistElementId) | |
239 | if (!videoPlaylistElement) { | |
240 | res.fail({ | |
241 | status: HttpStatusCode.NOT_FOUND_404, | |
242 | message: 'Video playlist element not found' | |
243 | }) | |
244 | return | |
245 | } | |
246 | res.locals.videoPlaylistElement = videoPlaylistElement | |
247 | ||
248 | if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return | |
249 | ||
250 | return next() | |
251 | } | |
252 | ] | |
253 | ||
254 | const videoPlaylistElementAPGetValidator = [ | |
255 | isValidPlaylistIdParam('playlistId'), | |
256 | param('playlistElementId') | |
257 | .custom(isIdValid), | |
258 | ||
259 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
260 | if (areValidationErrors(req, res)) return | |
261 | ||
262 | const playlistElementId = forceNumber(req.params.playlistElementId) | |
263 | const playlistId = req.params.playlistId | |
264 | ||
265 | const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndElementIdForAP(playlistId, playlistElementId) | |
266 | if (!videoPlaylistElement) { | |
267 | res.fail({ | |
268 | status: HttpStatusCode.NOT_FOUND_404, | |
269 | message: 'Video playlist element not found' | |
270 | }) | |
271 | return | |
272 | } | |
273 | ||
274 | if (videoPlaylistElement.VideoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
275 | return res.fail({ | |
276 | status: HttpStatusCode.FORBIDDEN_403, | |
277 | message: 'Cannot get this private video playlist.' | |
278 | }) | |
279 | } | |
280 | ||
281 | res.locals.videoPlaylistElementAP = videoPlaylistElement | |
282 | ||
283 | return next() | |
284 | } | |
285 | ] | |
286 | ||
287 | const videoPlaylistsReorderVideosValidator = [ | |
288 | isValidPlaylistIdParam('playlistId'), | |
289 | ||
290 | body('startPosition') | |
291 | .isInt({ min: 1 }), | |
292 | body('insertAfterPosition') | |
293 | .isInt({ min: 0 }), | |
294 | body('reorderLength') | |
295 | .optional() | |
296 | .isInt({ min: 1 }), | |
297 | ||
298 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
299 | if (areValidationErrors(req, res)) return | |
300 | ||
301 | if (!await doesVideoPlaylistExist(req.params.playlistId, res, 'all')) return | |
302 | ||
303 | const videoPlaylist = getPlaylist(res) | |
304 | if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return | |
305 | ||
306 | const nextPosition = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id) | |
307 | const startPosition: number = req.body.startPosition | |
308 | const insertAfterPosition: number = req.body.insertAfterPosition | |
309 | const reorderLength: number = req.body.reorderLength | |
310 | ||
311 | if (startPosition >= nextPosition || insertAfterPosition >= nextPosition) { | |
312 | res.fail({ message: `Start position or insert after position exceed the playlist limits (max: ${nextPosition - 1})` }) | |
313 | return | |
314 | } | |
315 | ||
316 | if (reorderLength && reorderLength + startPosition > nextPosition) { | |
317 | res.fail({ message: `Reorder length with this start position exceeds the playlist limits (max: ${nextPosition - startPosition})` }) | |
318 | return | |
319 | } | |
320 | ||
321 | return next() | |
322 | } | |
323 | ] | |
324 | ||
325 | const commonVideoPlaylistFiltersValidator = [ | |
326 | query('playlistType') | |
327 | .optional() | |
328 | .custom(isVideoPlaylistTypeValid), | |
329 | ||
330 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
331 | if (areValidationErrors(req, res)) return | |
332 | ||
333 | return next() | |
334 | } | |
335 | ] | |
336 | ||
337 | const doVideosInPlaylistExistValidator = [ | |
338 | query('videoIds') | |
339 | .customSanitizer(toIntArray) | |
340 | .custom(v => isArrayOf(v, isIdValid)).withMessage('Should have a valid video ids array'), | |
341 | ||
342 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
343 | if (areValidationErrors(req, res)) return | |
344 | ||
345 | return next() | |
346 | } | |
347 | ] | |
348 | ||
349 | // --------------------------------------------------------------------------- | |
350 | ||
351 | export { | |
352 | videoPlaylistsAddValidator, | |
353 | videoPlaylistsUpdateValidator, | |
354 | videoPlaylistsDeleteValidator, | |
355 | videoPlaylistsGetValidator, | |
356 | videoPlaylistsSearchValidator, | |
357 | ||
358 | videoPlaylistsAddVideoValidator, | |
359 | videoPlaylistsUpdateOrRemoveVideoValidator, | |
360 | videoPlaylistsReorderVideosValidator, | |
361 | ||
362 | videoPlaylistElementAPGetValidator, | |
363 | ||
364 | commonVideoPlaylistFiltersValidator, | |
365 | ||
366 | doVideosInPlaylistExistValidator | |
367 | } | |
368 | ||
369 | // --------------------------------------------------------------------------- | |
370 | ||
371 | function getCommonPlaylistEditAttributes () { | |
372 | return [ | |
373 | body('thumbnailfile') | |
374 | .custom((value, { req }) => isVideoImageValid(req.files, 'thumbnailfile')) | |
375 | .withMessage( | |
376 | 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: ' + | |
377 | CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.IMAGE.EXTNAME.join(', ') | |
378 | ), | |
379 | ||
380 | body('description') | |
381 | .optional() | |
382 | .customSanitizer(toValueOrNull) | |
383 | .custom(isVideoPlaylistDescriptionValid), | |
384 | body('privacy') | |
385 | .optional() | |
386 | .customSanitizer(toIntOrNull) | |
387 | .custom(isVideoPlaylistPrivacyValid), | |
388 | body('videoChannelId') | |
389 | .optional() | |
390 | .customSanitizer(toIntOrNull) | |
391 | ] as (ValidationChain | ExpressPromiseHandler)[] | |
392 | } | |
393 | ||
394 | function checkUserCanManageVideoPlaylist (user: MUserAccountId, videoPlaylist: MVideoPlaylist, right: UserRight, res: express.Response) { | |
395 | if (videoPlaylist.isOwned() === false) { | |
396 | res.fail({ | |
397 | status: HttpStatusCode.FORBIDDEN_403, | |
398 | message: 'Cannot manage video playlist of another server.' | |
399 | }) | |
400 | return false | |
401 | } | |
402 | ||
403 | // Check if the user can manage the video playlist | |
404 | // The user can delete it if s/he is an admin | |
405 | // Or if s/he is the video playlist's owner | |
406 | if (user.hasRight(right) === false && videoPlaylist.ownerAccountId !== user.Account.id) { | |
407 | res.fail({ | |
408 | status: HttpStatusCode.FORBIDDEN_403, | |
409 | message: 'Cannot manage video playlist of another user' | |
410 | }) | |
411 | return false | |
412 | } | |
413 | ||
414 | return true | |
415 | } | |
416 | ||
417 | function getPlaylist (res: express.Response) { | |
418 | return res.locals.videoPlaylistFull || res.locals.videoPlaylistSummary | |
419 | } |