]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-playlists.ts
Playlist server API
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-playlists.ts
CommitLineData
418d092a
C
1import * as express from 'express'
2import { body, param, ValidationChain } from 'express-validator/check'
3import { UserRight, VideoPrivacy } from '../../../../shared'
4import { logger } from '../../../helpers/logger'
5import { UserModel } from '../../../models/account/user'
6import { areValidationErrors } from '../utils'
7import { isVideoExist, isVideoImage } from '../../../helpers/custom-validators/videos'
8import { CONSTRAINTS_FIELDS } from '../../../initializers'
9import { isIdOrUUIDValid, toValueOrNull } from '../../../helpers/custom-validators/misc'
10import {
11 isVideoPlaylistDescriptionValid,
12 isVideoPlaylistExist,
13 isVideoPlaylistNameValid,
14 isVideoPlaylistPrivacyValid
15} from '../../../helpers/custom-validators/video-playlists'
16import { VideoPlaylistModel } from '../../../models/video/video-playlist'
17import { cleanUpReqFiles } from '../../../helpers/express-utils'
18import { isVideoChannelIdExist } from '../../../helpers/custom-validators/video-channels'
19import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element'
20import { VideoModel } from '../../../models/video/video'
21import { authenticatePromiseIfNeeded } from '../../oauth'
22import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
23
24const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoPlaylistsAddValidator parameters', { parameters: req.body })
27
28 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
29
30 if (req.body.videoChannelId && !await isVideoChannelIdExist(req.body.videoChannelId, res)) return cleanUpReqFiles(req)
31
32 return next()
33 }
34])
35
36const videoPlaylistsUpdateValidator = getCommonPlaylistEditAttributes().concat([
37 param('playlistId')
38 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
39
40 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 logger.debug('Checking videoPlaylistsUpdateValidator parameters', { parameters: req.body })
42
43 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
44
45 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return cleanUpReqFiles(req)
46 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
47 return cleanUpReqFiles(req)
48 }
49
50 if (req.body.videoChannelId && !await isVideoChannelIdExist(req.body.videoChannelId, res)) return cleanUpReqFiles(req)
51
52 return next()
53 }
54])
55
56const videoPlaylistsDeleteValidator = [
57 param('playlistId')
58 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
59
60 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
61 logger.debug('Checking videoPlaylistsDeleteValidator parameters', { parameters: req.params })
62
63 if (areValidationErrors(req, res)) return
64
65 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
66 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.REMOVE_ANY_VIDEO_PLAYLIST, res)) {
67 return
68 }
69
70 return next()
71 }
72]
73
74const videoPlaylistsGetValidator = [
75 param('playlistId')
76 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
77
78 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking videoPlaylistsGetValidator parameters', { parameters: req.params })
80
81 if (areValidationErrors(req, res)) return
82
83 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
84
85 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
86 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
87 await authenticatePromiseIfNeeded(req, res)
88
89 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : null
90
91 if (
92 !user ||
93 (videoPlaylist.OwnerAccount.userId !== user.id && !user.hasRight(UserRight.UPDATE_ANY_VIDEO_PLAYLIST))
94 ) {
95 return res.status(403)
96 .json({ error: 'Cannot get this private video playlist.' })
97 }
98
99 return next()
100 }
101
102 return next()
103 }
104]
105
106const videoPlaylistsAddVideoValidator = [
107 param('playlistId')
108 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
109 body('videoId')
110 .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid'),
111 body('startTimestamp')
112 .optional()
113 .isInt({ min: 0 }).withMessage('Should have a valid start timestamp'),
114 body('stopTimestamp')
115 .optional()
116 .isInt({ min: 0 }).withMessage('Should have a valid stop timestamp'),
117
118 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
119 logger.debug('Checking videoPlaylistsAddVideoValidator parameters', { parameters: req.params })
120
121 if (areValidationErrors(req, res)) return
122
123 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
124 if (!await isVideoExist(req.body.videoId, res, 'id')) return
125
126 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
127 const video: VideoModel = res.locals.video
128
129 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
130 if (videoPlaylistElement) {
131 res.status(409)
132 .json({ error: 'This video in this playlist already exists' })
133 .end()
134
135 return
136 }
137
138 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, res.locals.videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) {
139 return
140 }
141
142 return next()
143 }
144]
145
146const videoPlaylistsUpdateOrRemoveVideoValidator = [
147 param('playlistId')
148 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
149 param('videoId')
150 .custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
151 body('startTimestamp')
152 .optional()
153 .isInt({ min: 0 }).withMessage('Should have a valid start timestamp'),
154 body('stopTimestamp')
155 .optional()
156 .isInt({ min: 0 }).withMessage('Should have a valid stop timestamp'),
157
158 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
159 logger.debug('Checking videoPlaylistsRemoveVideoValidator parameters', { parameters: req.params })
160
161 if (areValidationErrors(req, res)) return
162
163 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
164 if (!await isVideoExist(req.params.playlistId, res, 'id')) return
165
166 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
167 const video: VideoModel = res.locals.video
168
169 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideo(videoPlaylist.id, video.id)
170 if (!videoPlaylistElement) {
171 res.status(404)
172 .json({ error: 'Video playlist element not found' })
173 .end()
174
175 return
176 }
177 res.locals.videoPlaylistElement = videoPlaylistElement
178
179 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
180
181 return next()
182 }
183]
184
185const videoPlaylistElementAPGetValidator = [
186 param('playlistId')
187 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
188 param('videoId')
189 .custom(isIdOrUUIDValid).withMessage('Should have an video id/uuid'),
190
191 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
192 logger.debug('Checking videoPlaylistElementAPGetValidator parameters', { parameters: req.params })
193
194 if (areValidationErrors(req, res)) return
195
196 const videoPlaylistElement = await VideoPlaylistElementModel.loadByPlaylistAndVideoForAP(req.params.playlistId, req.params.videoId)
197 if (!videoPlaylistElement) {
198 res.status(404)
199 .json({ error: 'Video playlist element not found' })
200 .end()
201
202 return
203 }
204
205 if (videoPlaylistElement.VideoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
206 return res.status(403).end()
207 }
208
209 res.locals.videoPlaylistElement = videoPlaylistElement
210
211 return next()
212 }
213]
214
215const videoPlaylistsReorderVideosValidator = [
216 param('playlistId')
217 .custom(isIdOrUUIDValid).withMessage('Should have a valid playlist id/uuid'),
218 body('startPosition')
219 .isInt({ min: 1 }).withMessage('Should have a valid start position'),
220 body('insertAfterPosition')
221 .isInt({ min: 0 }).withMessage('Should have a valid insert after position'),
222 body('reorderLength')
223 .optional()
224 .isInt({ min: 1 }).withMessage('Should have a valid range length'),
225
226 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
227 logger.debug('Checking videoPlaylistsReorderVideosValidator parameters', { parameters: req.params })
228
229 if (areValidationErrors(req, res)) return
230
231 if (!await isVideoPlaylistExist(req.params.playlistId, res)) return
232
233 const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
234 if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return
235
236 return next()
237 }
238]
239
240// ---------------------------------------------------------------------------
241
242export {
243 videoPlaylistsAddValidator,
244 videoPlaylistsUpdateValidator,
245 videoPlaylistsDeleteValidator,
246 videoPlaylistsGetValidator,
247
248 videoPlaylistsAddVideoValidator,
249 videoPlaylistsUpdateOrRemoveVideoValidator,
250 videoPlaylistsReorderVideosValidator,
251
252 videoPlaylistElementAPGetValidator
253}
254
255// ---------------------------------------------------------------------------
256
257function getCommonPlaylistEditAttributes () {
258 return [
259 body('thumbnailfile')
260 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
261 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
262 + CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.IMAGE.EXTNAME.join(', ')
263 ),
264
265 body('displayName')
266 .custom(isVideoPlaylistNameValid).withMessage('Should have a valid display name'),
267 body('description')
268 .optional()
269 .customSanitizer(toValueOrNull)
270 .custom(isVideoPlaylistDescriptionValid).withMessage('Should have a valid description'),
271 body('privacy')
272 .optional()
273 .toInt()
274 .custom(isVideoPlaylistPrivacyValid).withMessage('Should have correct playlist privacy'),
275 body('videoChannelId')
276 .optional()
277 .toInt()
278 ] as (ValidationChain | express.Handler)[]
279}
280
281function checkUserCanManageVideoPlaylist (user: UserModel, videoPlaylist: VideoPlaylistModel, right: UserRight, res: express.Response) {
282 if (videoPlaylist.isOwned() === false) {
283 res.status(403)
284 .json({ error: 'Cannot manage video playlist of another server.' })
285 .end()
286
287 return false
288 }
289
290 // Check if the user can manage the video playlist
291 // The user can delete it if s/he is an admin
292 // Or if s/he is the video playlist's owner
293 if (user.hasRight(right) === false && videoPlaylist.ownerAccountId !== user.Account.id) {
294 res.status(403)
295 .json({ error: 'Cannot manage video playlist of another user' })
296 .end()
297
298 return false
299 }
300
301 return true
302}