PeerTube/server/middlewares/validators/sort.ts

48 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-06-10 22:15:25 +02:00
import 'express-validator'
import * as express from 'express'
2017-05-15 22:22:03 +02:00
import { checkErrors } from './utils'
import { logger } from '../../helpers'
import { SORTABLE_COLUMNS } from '../../initializers'
2016-05-17 21:03:00 +02:00
2017-02-26 19:26:57 +01:00
// Initialize constants here for better performances
2017-05-15 22:22:03 +02:00
const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
2016-08-16 22:31:45 +02:00
2017-06-10 22:15:25 +02:00
function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-02-26 19:26:57 +01:00
checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
2017-01-04 20:59:23 +01:00
}
2016-08-16 22:31:45 +02:00
2017-06-10 22:15:25 +02:00
function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-02-26 19:26:57 +01:00
checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS)
2016-08-16 22:31:45 +02:00
}
2017-06-10 22:15:25 +02:00
function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-02-26 19:26:57 +01:00
checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS)
2017-01-04 20:59:23 +01:00
}
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
usersSortValidator,
videoAbusesSortValidator,
videosSortValidator
}
2017-01-04 20:59:23 +01:00
// ---------------------------------------------------------------------------
2017-06-10 22:15:25 +02:00
function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) {
2016-05-17 21:03:00 +02:00
req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns)
logger.debug('Checking sort parameters', { parameters: req.query })
checkErrors(req, res, next)
}
2017-02-26 19:26:57 +01:00
2017-06-10 22:15:25 +02:00
function createSortableColumns (sortableColumns: string[]) {
2017-02-26 19:26:57 +01:00
const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
return sortableColumns.concat(sortableColumnDesc)
}