X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fsort.ts;h=4588958988af88a759c35ebfbc7a6afb58ed0643;hb=3455c2656e257ae3d9b4169af58b6889d9904148;hp=ab9ccf5242bee9af0f6f56eab779534037dd19f1;hpb=65fcc3119c334b75dd13bcfdebf186afdc580a8f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index ab9ccf524..458895898 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts @@ -1,25 +1,50 @@ -function setUsersSort (req, res, next) { - if (!req.query.sort) req.query.sort = '-createdAt' +import express from 'express' +import { SortType } from '../models/utils' - return next() -} +const setDefaultSort = setDefaultSortFactory('-createdAt') +const setDefaultVideosSort = setDefaultSortFactory('-publishedAt') -function setVideoAbusesSort (req, res, next) { - if (!req.query.sort) req.query.sort = '-createdAt' +const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name') - return next() -} +const setDefaultSearchSort = setDefaultSortFactory('-match') + +function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) { + const newSort: SortType = { sortModel: undefined, sortValue: '' } -function setVideosSort (req, res, next) { 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... + newSort.sortModel = undefined + } else { + newSort.sortModel = 'Video' + } + + newSort.sortValue = req.query.sort + + req.query.sort = newSort + return next() } // --------------------------------------------------------------------------- export { - setUsersSort, - setVideoAbusesSort, - setVideosSort + 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() + } }