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