]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/sort.ts
Misc cleanup
[github/Chocobozzz/PeerTube.git] / server / middlewares / sort.ts
index ab9ccf5242bee9af0f6f56eab779534037dd19f1..279b29e654c40d4f069f14de7d3c3024149ab6c7 100644 (file)
@@ -1,25 +1,74 @@
-function setUsersSort (req, res, next) {
+import 'express-validator'
+import * as express from 'express'
+
+import { SortType } from '../helpers'
+import { database } from '../initializers'
+
+function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) {
+  if (!req.query.sort) req.query.sort = '-createdAt'
+
+  return next()
+}
+
+function setVideoAbusesSort (req: express.Request, res: express.Response, next: express.NextFunction) {
+  if (!req.query.sort) req.query.sort = '-createdAt'
+
+  return next()
+}
+
+function setVideoChannelsSort (req: express.Request, res: express.Response, next: express.NextFunction) {
+  if (!req.query.sort) req.query.sort = '-createdAt'
+
+  return next()
+}
+
+function setVideosSort (req: express.Request, res: express.Response, next: express.NextFunction) {
   if (!req.query.sort) req.query.sort = '-createdAt'
 
   return next()
 }
 
-function setVideoAbusesSort (req, res, next) {
+function setFollowersSort (req: express.Request, res: express.Response, next: express.NextFunction) {
   if (!req.query.sort) req.query.sort = '-createdAt'
 
   return next()
 }
 
-function setVideosSort (req, res, next) {
+function setFollowingSort (req: express.Request, res: express.Response, next: express.NextFunction) {
   if (!req.query.sort) req.query.sort = '-createdAt'
 
   return next()
 }
 
+function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) {
+  let newSort: SortType = { sortModel: undefined, sortValue: undefined }
+
+  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 = database.Video
+  }
+
+  newSort.sortValue = req.query.sort
+
+  req.query.sort = newSort
+
+  return next()
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   setUsersSort,
   setVideoAbusesSort,
-  setVideosSort
+  setVideoChannelsSort,
+  setVideosSort,
+  setBlacklistSort,
+  setFollowersSort,
+  setFollowingSort
 }