]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/sort.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / sort.ts
CommitLineData
69818c93 1import * as express from 'express'
06215f15 2import { SortType } from '../models/utils'
792dbaf0 3
b764380a 4const setDefaultSort = setDefaultSortFactory('-createdAt')
7a7724e6 5
b764380a 6const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name')
57c36b27 7
b764380a 8const setDefaultSearchSort = setDefaultSortFactory('-match')
57c36b27 9
792dbaf0 10function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) {
c1e791ba 11 let newSort: SortType = { sortModel: undefined, sortValue: '' }
792dbaf0
GS
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 {
3fd3ab2d 21 newSort.sortModel = 'Video'
792dbaf0
GS
22 }
23
24 newSort.sortValue = req.query.sort
25
26 req.query.sort = newSort
27
28 return next()
29}
30
a877d5ac
C
31// ---------------------------------------------------------------------------
32
65fcc311 33export {
1174a847 34 setDefaultSort,
57c36b27 35 setDefaultSearchSort,
b764380a 36 setDefaultVideoRedundanciesSort,
1174a847 37 setBlacklistSort
65fcc311 38}
b764380a
C
39
40// ---------------------------------------------------------------------------
41
42function 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}