]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/sort.ts
Type functions
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / sort.ts
CommitLineData
69818c93
C
1import 'express-validator'
2import * as express from 'express'
3
65fcc311
C
4import { checkErrors } from './utils'
5import { logger } from '../../helpers'
6import { SORTABLE_COLUMNS } from '../../initializers'
a877d5ac 7
9c2c18f3 8// Initialize constants here for better performances
65fcc311
C
9const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
10const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
11const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
5c39adb7 12
69818c93 13function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
9c2c18f3 14 checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
55fa55a9 15}
5c39adb7 16
69818c93 17function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
9c2c18f3 18 checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS)
5c39adb7
C
19}
20
69818c93 21function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
9c2c18f3 22 checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS)
55fa55a9
C
23}
24
25// ---------------------------------------------------------------------------
26
65fcc311
C
27export {
28 usersSortValidator,
29 videoAbusesSortValidator,
30 videosSortValidator
31}
55fa55a9
C
32
33// ---------------------------------------------------------------------------
34
69818c93 35function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) {
a877d5ac
C
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}
9c2c18f3 42
69818c93 43function createSortableColumns (sortableColumns: string[]) {
9c2c18f3
C
44 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
45
46 return sortableColumns.concat(sortableColumnDesc)
47}