]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/sort.ts
Optimize default sort when listing videos
[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')
8054669f 5const setDefaultVideosSort = setDefaultSortFactory('-publishedAt')
7a7724e6 6
b764380a 7const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name')
57c36b27 8
b764380a 9const setDefaultSearchSort = setDefaultSortFactory('-match')
57c36b27 10
792dbaf0 11function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) {
a1587156 12 const newSort: SortType = { sortModel: undefined, sortValue: '' }
792dbaf0
GS
13
14 if (!req.query.sort) req.query.sort = '-createdAt'
15
16 // Set model we want to sort onto
17 if (req.query.sort === '-createdAt' || req.query.sort === 'createdAt' ||
18 req.query.sort === '-id' || req.query.sort === 'id') {
19 // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter ...
20 newSort.sortModel = undefined
21 } else {
3fd3ab2d 22 newSort.sortModel = 'Video'
792dbaf0
GS
23 }
24
25 newSort.sortValue = req.query.sort
26
27 req.query.sort = newSort
28
29 return next()
30}
31
a877d5ac
C
32// ---------------------------------------------------------------------------
33
65fcc311 34export {
1174a847 35 setDefaultSort,
57c36b27 36 setDefaultSearchSort,
8054669f 37 setDefaultVideosSort,
b764380a 38 setDefaultVideoRedundanciesSort,
1174a847 39 setBlacklistSort
65fcc311 40}
b764380a
C
41
42// ---------------------------------------------------------------------------
43
44function setDefaultSortFactory (sort: string) {
45 return (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 if (!req.query.sort) req.query.sort = sort
47
48 return next()
49 }
50}