]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/sort.ts
91aa3e5b6b4c2ac38d7f1a330dd8ca9f36aac8f3
[github/Chocobozzz/PeerTube.git] / server / middlewares / sort.ts
1 import 'express-validator'
2 import * as express from 'express'
3
4 import { SortType } from '../helpers'
5 import { database } from '../initializers'
6
7 function setPodsSort (req: express.Request, res: express.Response, next: express.NextFunction) {
8 if (!req.query.sort) req.query.sort = '-createdAt'
9
10 return next()
11 }
12
13 function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) {
14 if (!req.query.sort) req.query.sort = '-createdAt'
15
16 return next()
17 }
18
19 function setVideoAbusesSort (req: express.Request, res: express.Response, next: express.NextFunction) {
20 if (!req.query.sort) req.query.sort = '-createdAt'
21
22 return next()
23 }
24
25 function setVideoChannelsSort (req: express.Request, res: express.Response, next: express.NextFunction) {
26 if (!req.query.sort) req.query.sort = '-createdAt'
27
28 return next()
29 }
30
31 function setVideosSort (req: express.Request, res: express.Response, next: express.NextFunction) {
32 if (!req.query.sort) req.query.sort = '-createdAt'
33
34 return next()
35 }
36
37 function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) {
38 let newSort: SortType = { sortModel: undefined, sortValue: undefined }
39
40 if (!req.query.sort) req.query.sort = '-createdAt'
41
42 // Set model we want to sort onto
43 if (req.query.sort === '-createdAt' || req.query.sort === 'createdAt' ||
44 req.query.sort === '-id' || req.query.sort === 'id') {
45 // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter ...
46 newSort.sortModel = undefined
47 } else {
48 newSort.sortModel = database.Video
49 }
50
51 newSort.sortValue = req.query.sort
52
53 req.query.sort = newSort
54
55 return next()
56 }
57
58 // ---------------------------------------------------------------------------
59
60 export {
61 setPodsSort,
62 setUsersSort,
63 setVideoAbusesSort,
64 setVideoChannelsSort,
65 setVideosSort,
66 setBlacklistSort
67 }