]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/sort.ts
Fix video removing when it is corrupted
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / sort.ts
CommitLineData
b60e5f38 1import { query } from 'express-validator/check'
69818c93
C
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)
792dbaf0 12const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS)
5c39adb7 13
b60e5f38
C
14const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS)
15const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS)
16const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS)
792dbaf0 17const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
55fa55a9
C
18
19// ---------------------------------------------------------------------------
20
65fcc311
C
21export {
22 usersSortValidator,
23 videoAbusesSortValidator,
792dbaf0
GS
24 videosSortValidator,
25 blacklistSortValidator
65fcc311 26}
55fa55a9
C
27
28// ---------------------------------------------------------------------------
29
b60e5f38
C
30function checkSort (sortableColumns: string[]) {
31 return [
32 query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
a877d5ac 33
b60e5f38
C
34 (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 logger.debug('Checking sort parameters', { parameters: req.query })
a877d5ac 36
b60e5f38
C
37 checkErrors(req, res, next)
38 }
39 ]
a877d5ac 40}
9c2c18f3 41
69818c93 42function createSortableColumns (sortableColumns: string[]) {
9c2c18f3
C
43 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
44
45 return sortableColumns.concat(sortableColumnDesc)
46}