]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/sort.ts
Type functions
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / sort.ts
1 import 'express-validator'
2 import * as express from 'express'
3
4 import { checkErrors } from './utils'
5 import { logger } from '../../helpers'
6 import { SORTABLE_COLUMNS } from '../../initializers'
7
8 // Initialize constants here for better performances
9 const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
10 const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
11 const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
12
13 function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
14 checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
15 }
16
17 function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
18 checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS)
19 }
20
21 function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
22 checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS)
23 }
24
25 // ---------------------------------------------------------------------------
26
27 export {
28 usersSortValidator,
29 videoAbusesSortValidator,
30 videosSortValidator
31 }
32
33 // ---------------------------------------------------------------------------
34
35 function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) {
36 req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns)
37
38 logger.debug('Checking sort parameters', { parameters: req.query })
39
40 checkErrors(req, res, next)
41 }
42
43 function createSortableColumns (sortableColumns: string[]) {
44 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
45
46 return sortableColumns.concat(sortableColumnDesc)
47 }