]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/sort.ts
5120804b3a34b27dc88753ff86cffd5906062ff9
[github/Chocobozzz/PeerTube.git] / server / middlewares / sort.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import { SortType } from '../helpers/utils'
4
5 function setDefaultSort (req: express.Request, res: express.Response, next: express.NextFunction) {
6 if (!req.query.sort) req.query.sort = '-createdAt'
7
8 return next()
9 }
10
11 function setDefaultSearchSort (req: express.Request, res: express.Response, next: express.NextFunction) {
12 if (!req.query.sort) req.query.sort = '-match'
13
14 return next()
15 }
16
17 function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) {
18 let newSort: SortType = { sortModel: undefined, sortValue: '' }
19
20 if (!req.query.sort) req.query.sort = '-createdAt'
21
22 // Set model we want to sort onto
23 if (req.query.sort === '-createdAt' || req.query.sort === 'createdAt' ||
24 req.query.sort === '-id' || req.query.sort === 'id') {
25 // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter ...
26 newSort.sortModel = undefined
27 } else {
28 newSort.sortModel = 'Video'
29 }
30
31 newSort.sortValue = req.query.sort
32
33 req.query.sort = newSort
34
35 return next()
36 }
37
38 // ---------------------------------------------------------------------------
39
40 export {
41 setDefaultSort,
42 setDefaultSearchSort,
43 setBlacklistSort
44 }