X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fsort.ts;h=4588958988af88a759c35ebfbc7a6afb58ed0643;hb=adc94cf09c86112051f72055852efcc977e4a04a;hp=8a62c8be65a8d5e5c78c16a72ecaab69097cdfd3;hpb=d525fc399a14a8b16eaad6d4c0bc0a9c4093c3c9;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index 8a62c8be6..458895898 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts @@ -1,28 +1,22 @@ -import * as express from 'express' -import 'express-validator' -import { SortType } from '../helpers/utils' +import express from 'express' +import { SortType } from '../models/utils' -function setDefaultSort (req: express.Request, res: express.Response, next: express.NextFunction) { - if (!req.query.sort) req.query.sort = '-createdAt' - - return next() -} +const setDefaultSort = setDefaultSortFactory('-createdAt') +const setDefaultVideosSort = setDefaultSortFactory('-publishedAt') -function setDefaultSearchSort (req: express.Request, res: express.Response, next: express.NextFunction) { - if (!req.query.sort) req.query.sort = '-match' +const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name') - return next() -} +const setDefaultSearchSort = setDefaultSortFactory('-match') function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) { - let newSort: SortType = { sortModel: undefined, sortValue: undefined } + const newSort: SortType = { sortModel: undefined, sortValue: '' } if (!req.query.sort) req.query.sort = '-createdAt' // Set model we want to sort onto if (req.query.sort === '-createdAt' || req.query.sort === 'createdAt' || req.query.sort === '-id' || req.query.sort === 'id') { - // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter ... + // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter... newSort.sortModel = undefined } else { newSort.sortModel = 'Video' @@ -40,5 +34,17 @@ function setBlacklistSort (req: express.Request, res: express.Response, next: ex export { setDefaultSort, setDefaultSearchSort, + setDefaultVideosSort, + setDefaultVideoRedundanciesSort, setBlacklistSort } + +// --------------------------------------------------------------------------- + +function setDefaultSortFactory (sort: string) { + return (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (!req.query.sort) req.query.sort = sort + + return next() + } +}