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