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