PeerTube/server/middlewares/validators/sort.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-05-17 14:03:00 -05:00
'use strict'
const checkErrors = require('./utils').checkErrors
const constants = require('../../initializers/constants')
const logger = require('../../helpers/logger')
2016-07-01 09:16:40 -05:00
const validatorsSort = {
usersSort,
2017-01-04 13:59:23 -06:00
videoAbusesSort,
videosSort
2016-05-17 14:03:00 -05:00
}
2017-02-26 12:26:57 -06:00
// Initialize constants here for better performances
const SORTABLE_USERS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.USERS)
const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEO_ABUSES)
const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEOS)
2016-08-16 15:31:45 -05:00
2017-02-26 12:26:57 -06:00
function usersSort (req, res, next) {
checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
2017-01-04 13:59:23 -06:00
}
2016-08-16 15:31:45 -05:00
2017-01-04 13:59:23 -06:00
function videoAbusesSort (req, res, next) {
2017-02-26 12:26:57 -06:00
checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS)
2016-08-16 15:31:45 -05:00
}
2016-05-17 14:03:00 -05:00
function videosSort (req, res, next) {
2017-02-26 12:26:57 -06:00
checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS)
2017-01-04 13:59:23 -06:00
}
// ---------------------------------------------------------------------------
module.exports = validatorsSort
// ---------------------------------------------------------------------------
function checkSort (req, res, next, sortableColumns) {
2016-05-17 14:03:00 -05: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 12:26:57 -06:00
function createSortableColumns (sortableColumns) {
const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
return sortableColumns.concat(sortableColumnDesc)
}