diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-20 14:35:18 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-24 14:04:05 +0200 |
commit | d525fc399a14a8b16eaad6d4c0bc0a9c4093c3c9 (patch) | |
tree | 4305044c4a97bdf1275b241c63cb0e85151cfb6a /server/middlewares | |
parent | 57c36b277e68b764dd34cb2e449f6e2ca3d1e9b6 (diff) | |
download | PeerTube-d525fc399a14a8b16eaad6d4c0bc0a9c4093c3c9.tar.gz PeerTube-d525fc399a14a8b16eaad6d4c0bc0a9c4093c3c9.tar.zst PeerTube-d525fc399a14a8b16eaad6d4c0bc0a9c4093c3c9.zip |
Add videos list filters
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/sort.ts | 2 | ||||
-rw-r--r-- | server/middlewares/validators/search.ts | 46 |
2 files changed, 46 insertions, 2 deletions
diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index 6307ee154..8a62c8be6 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts | |||
@@ -9,7 +9,7 @@ function setDefaultSort (req: express.Request, res: express.Response, next: expr | |||
9 | } | 9 | } |
10 | 10 | ||
11 | function setDefaultSearchSort (req: express.Request, res: express.Response, next: express.NextFunction) { | 11 | function setDefaultSearchSort (req: express.Request, res: express.Response, next: express.NextFunction) { |
12 | if (!req.query.sort) req.query.sort = '-bestmatch' | 12 | if (!req.query.sort) req.query.sort = '-match' |
13 | 13 | ||
14 | return next() | 14 | return next() |
15 | } | 15 | } |
diff --git a/server/middlewares/validators/search.ts b/server/middlewares/validators/search.ts index 774845e8a..fb2148eb3 100644 --- a/server/middlewares/validators/search.ts +++ b/server/middlewares/validators/search.ts | |||
@@ -2,12 +2,55 @@ import * as express from 'express' | |||
2 | import { areValidationErrors } from './utils' | 2 | import { areValidationErrors } from './utils' |
3 | import { logger } from '../../helpers/logger' | 3 | import { logger } from '../../helpers/logger' |
4 | import { query } from 'express-validator/check' | 4 | import { query } from 'express-validator/check' |
5 | import { isNumberArray, isStringArray } from '../../helpers/custom-validators/search' | ||
6 | import { isBooleanValid, isDateValid, toArray } from '../../helpers/custom-validators/misc' | ||
5 | 7 | ||
6 | const searchValidator = [ | 8 | const searchValidator = [ |
7 | query('search').not().isEmpty().withMessage('Should have a valid search'), | 9 | query('search').not().isEmpty().withMessage('Should have a valid search'), |
8 | 10 | ||
11 | query('startDate').optional().custom(isDateValid).withMessage('Should have a valid start date'), | ||
12 | query('endDate').optional().custom(isDateValid).withMessage('Should have a valid end date'), | ||
13 | |||
14 | query('durationMin').optional().isInt().withMessage('Should have a valid min duration'), | ||
15 | query('durationMax').optional().isInt().withMessage('Should have a valid max duration'), | ||
16 | |||
17 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
18 | logger.debug('Checking search query', { parameters: req.query }) | ||
19 | |||
20 | if (areValidationErrors(req, res)) return | ||
21 | |||
22 | return next() | ||
23 | } | ||
24 | ] | ||
25 | |||
26 | const commonVideosFiltersValidator = [ | ||
27 | query('categoryOneOf') | ||
28 | .optional() | ||
29 | .customSanitizer(toArray) | ||
30 | .custom(isNumberArray).withMessage('Should have a valid one of category array'), | ||
31 | query('licenceOneOf') | ||
32 | .optional() | ||
33 | .customSanitizer(toArray) | ||
34 | .custom(isNumberArray).withMessage('Should have a valid one of licence array'), | ||
35 | query('languageOneOf') | ||
36 | .optional() | ||
37 | .customSanitizer(toArray) | ||
38 | .custom(isStringArray).withMessage('Should have a valid one of language array'), | ||
39 | query('tagsOneOf') | ||
40 | .optional() | ||
41 | .customSanitizer(toArray) | ||
42 | .custom(isStringArray).withMessage('Should have a valid one of tags array'), | ||
43 | query('tagsAllOf') | ||
44 | .optional() | ||
45 | .customSanitizer(toArray) | ||
46 | .custom(isStringArray).withMessage('Should have a valid all of tags array'), | ||
47 | query('nsfw') | ||
48 | .optional() | ||
49 | .toBoolean() | ||
50 | .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), | ||
51 | |||
9 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | 52 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
10 | logger.debug('Checking search parameters', { parameters: req.params }) | 53 | logger.debug('Checking commons video filters query', { parameters: req.query }) |
11 | 54 | ||
12 | if (areValidationErrors(req, res)) return | 55 | if (areValidationErrors(req, res)) return |
13 | 56 | ||
@@ -18,5 +61,6 @@ const searchValidator = [ | |||
18 | // --------------------------------------------------------------------------- | 61 | // --------------------------------------------------------------------------- |
19 | 62 | ||
20 | export { | 63 | export { |
64 | commonVideosFiltersValidator, | ||
21 | searchValidator | 65 | searchValidator |
22 | } | 66 | } |