Set sort refractoring
authorChocobozzz <me@florianbigard.com>
Wed, 17 Jan 2018 09:50:33 +0000 (10:50 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 18 Jan 2018 14:42:20 +0000 (15:42 +0100)
server/controllers/api/accounts.ts
server/controllers/api/jobs.ts
server/controllers/api/server/follows.ts
server/controllers/api/users.ts
server/controllers/api/videos/abuse.ts
server/controllers/api/videos/channel.ts
server/controllers/api/videos/comment.ts
server/controllers/api/videos/index.ts
server/middlewares/sort.ts

index aded581a5f9b9a0576917fad323020a778eb4010..3bc929db848b78cb0161ca537e9034e6323dfcb6 100644 (file)
@@ -1,6 +1,6 @@
 import * as express from 'express'
 import { getFormattedObjects } from '../../helpers/utils'
-import { asyncMiddleware, paginationValidator, setAccountsSort, setPagination } from '../../middlewares'
+import { asyncMiddleware, paginationValidator, setDefaultSort, setPagination } from '../../middlewares'
 import { accountsGetValidator, accountsSortValidator } from '../../middlewares/validators'
 import { AccountModel } from '../../models/account/account'
 
@@ -9,7 +9,7 @@ const accountsRouter = express.Router()
 accountsRouter.get('/',
   paginationValidator,
   accountsSortValidator,
-  setAccountsSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listAccounts)
 )
index d9d6030a4851b5872422c9673246f6b700a2b328..180a3fca6bb820d115b74f1712a936716399b690 100644 (file)
@@ -1,7 +1,7 @@
 import * as express from 'express'
 import { UserRight } from '../../../shared/models/users'
 import { getFormattedObjects } from '../../helpers/utils'
-import { asyncMiddleware, authenticate, ensureUserHasRight, jobsSortValidator, setJobsSort, setPagination } from '../../middlewares'
+import { asyncMiddleware, authenticate, ensureUserHasRight, jobsSortValidator, setDefaultSort, setPagination } from '../../middlewares'
 import { paginationValidator } from '../../middlewares/validators'
 import { JobModel } from '../../models/job/job'
 
@@ -12,7 +12,7 @@ jobsRouter.get('/',
   ensureUserHasRight(UserRight.MANAGE_JOBS),
   paginationValidator,
   jobsSortValidator,
-  setJobsSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listJobs)
 )
index b93d8a816189c71ac57503dca20bbd32cabe99f3..0fbcc4b80dc58d176f99b019cadaf80efee63b08 100644 (file)
@@ -9,8 +9,8 @@ import { REMOTE_SCHEME, sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../
 import { getOrCreateActorAndServerAndModel } from '../../../lib/activitypub/actor'
 import { sendFollow, sendUndoFollow } from '../../../lib/activitypub/send'
 import {
-  asyncMiddleware, authenticate, ensureUserHasRight, paginationValidator, removeFollowingValidator, setBodyHostsPort,
-  setFollowersSort, setFollowingSort, setPagination
+  asyncMiddleware, authenticate, ensureUserHasRight, paginationValidator, removeFollowingValidator, setBodyHostsPort, setDefaultSort,
+  setPagination
 } from '../../../middlewares'
 import { followersSortValidator, followingSortValidator, followValidator } from '../../../middlewares/validators'
 import { ActorModel } from '../../../models/activitypub/actor'
@@ -21,7 +21,7 @@ const serverFollowsRouter = express.Router()
 serverFollowsRouter.get('/following',
   paginationValidator,
   followingSortValidator,
-  setFollowingSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listFollowing)
 )
@@ -44,7 +44,7 @@ serverFollowsRouter.delete('/following/:host',
 serverFollowsRouter.get('/followers',
   paginationValidator,
   followersSortValidator,
-  setFollowersSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listFollowers)
 )
index 5374c4b6afdf1e34e8035d1cdd6d69f26f64a18c..0ca9b337a0c83c2bce6be6cedca85e5a5d84c23c 100644 (file)
@@ -12,8 +12,8 @@ import { updateActorAvatarInstance } from '../../lib/activitypub'
 import { sendUpdateUser } from '../../lib/activitypub/send'
 import { createUserAccountAndChannel } from '../../lib/user'
 import {
-  asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort,
-  setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
+  asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setDefaultSort,
+  setPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
   usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
 } from '../../middlewares'
 import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators'
@@ -39,7 +39,7 @@ usersRouter.get('/me/videos',
   authenticate,
   paginationValidator,
   videosSortValidator,
-  setVideosSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(getUserVideos)
 )
@@ -55,7 +55,7 @@ usersRouter.get('/',
   ensureUserHasRight(UserRight.MANAGE_USERS),
   paginationValidator,
   usersSortValidator,
-  setUsersSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listUsers)
 )
index e78f0f6fce4df412776a84e51cb169aa8e4b8a7f..91594490b2bb0892cae5f758709dda90e05b53da 100644 (file)
@@ -6,8 +6,8 @@ import { getFormattedObjects } from '../../../helpers/utils'
 import { sequelizeTypescript } from '../../../initializers'
 import { sendVideoAbuse } from '../../../lib/activitypub/send'
 import {
-  asyncMiddleware, authenticate, ensureUserHasRight, paginationValidator, setPagination, setVideoAbusesSort,
-  videoAbuseReportValidator, videoAbusesSortValidator
+  asyncMiddleware, authenticate, ensureUserHasRight, paginationValidator, setDefaultSort, setPagination, videoAbuseReportValidator,
+  videoAbusesSortValidator
 } from '../../../middlewares'
 import { AccountModel } from '../../../models/account/account'
 import { VideoModel } from '../../../models/video/video'
@@ -20,7 +20,7 @@ abuseVideoRouter.get('/abuse',
   ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
   paginationValidator,
   videoAbusesSortValidator,
-  setVideoAbusesSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listVideoAbuses)
 )
index 7c62b54761178e4edbf6cd34a1251bfadee3ba68..2012575c8676fe0f3d76ba7d55c6082de06c3047 100644 (file)
@@ -7,7 +7,7 @@ import { sequelizeTypescript } from '../../../initializers'
 import { setAsyncActorKeys } from '../../../lib/activitypub'
 import { createVideoChannel } from '../../../lib/video-channel'
 import {
-  asyncMiddleware, authenticate, listVideoAccountChannelsValidator, paginationValidator, setPagination, setVideoChannelsSort,
+  asyncMiddleware, authenticate, listVideoAccountChannelsValidator, paginationValidator, setDefaultSort, setPagination,
   videoChannelsAddValidator, videoChannelsGetValidator, videoChannelsRemoveValidator, videoChannelsSortValidator,
   videoChannelsUpdateValidator
 } from '../../../middlewares'
@@ -19,7 +19,7 @@ const videoChannelRouter = express.Router()
 videoChannelRouter.get('/channels',
   paginationValidator,
   videoChannelsSortValidator,
-  setVideoChannelsSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listVideoChannels)
 )
index 65fcf6b3569e339c1cf4500c1b4565e21e608b17..3c2727530f7b65a7736e1f69ebcf63565a8515a2 100644 (file)
@@ -6,7 +6,7 @@ import { logger } from '../../../helpers/logger'
 import { getFormattedObjects } from '../../../helpers/utils'
 import { sequelizeTypescript } from '../../../initializers'
 import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
-import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares'
+import { asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setPagination } from '../../../middlewares'
 import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
 import {
   addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator, listVideoThreadCommentsValidator,
@@ -20,7 +20,7 @@ const videoCommentRouter = express.Router()
 videoCommentRouter.get('/:videoId/comment-threads',
   paginationValidator,
   videoCommentThreadsSortValidator,
-  setVideoCommentThreadsSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listVideoCommentThreadsValidator),
   asyncMiddleware(listVideoThreads)
index 368327914b4276fcf72188e3b499d76680af6c5e..6a7f1f18425e016a84c15f63e51e770eec3f214c 100644 (file)
@@ -14,7 +14,7 @@ import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServer
 import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
 import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler'
 import {
-  asyncMiddleware, authenticate, paginationValidator, setPagination, setVideosSort, videosAddValidator, videosGetValidator,
+  asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setPagination, videosAddValidator, videosGetValidator,
   videosRemoveValidator, videosSearchValidator, videosSortValidator, videosUpdateValidator
 } from '../../../middlewares'
 import { TagModel } from '../../../models/video/tag'
@@ -44,7 +44,7 @@ videosRouter.get('/privacies', listVideoPrivacies)
 videosRouter.get('/',
   paginationValidator,
   videosSortValidator,
-  setVideosSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(listVideos)
 )
@@ -52,7 +52,7 @@ videosRouter.get('/search',
   videosSearchValidator,
   paginationValidator,
   videosSortValidator,
-  setVideosSort,
+  setDefaultSort,
   setPagination,
   asyncMiddleware(searchVideos)
 )
index 4f524b49a613ec7e74be428bee21365a32260e71..cdb809e75e80084d5f6774e4f21ba4159be89fee 100644 (file)
@@ -2,55 +2,7 @@ import * as express from 'express'
 import 'express-validator'
 import { SortType } from '../helpers/utils'
 
-function setAccountsSort (req: express.Request, res: express.Response, next: express.NextFunction) {
-  if (!req.query.sort) req.query.sort = '-createdAt'
-
-  return next()
-}
-
-function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) {
-  if (!req.query.sort) req.query.sort = '-createdAt'
-
-  return next()
-}
-
-function setJobsSort (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 setVideoCommentThreadsSort (req: express.Request, res: express.Response, next: express.NextFunction) {
-  if (!req.query.sort) req.query.sort = '-createdAt'
-
-  return next()
-}
-
-function setFollowersSort (req: express.Request, res: express.Response, next: express.NextFunction) {
-  if (!req.query.sort) req.query.sort = '-createdAt'
-
-  return next()
-}
-
-function setFollowingSort (req: express.Request, res: express.Response, next: express.NextFunction) {
+function setDefaultSort (req: express.Request, res: express.Response, next: express.NextFunction) {
   if (!req.query.sort) req.query.sort = '-createdAt'
 
   return next()
@@ -80,14 +32,6 @@ function setBlacklistSort (req: express.Request, res: express.Response, next: ex
 // ---------------------------------------------------------------------------
 
 export {
-  setUsersSort,
-  setVideoAbusesSort,
-  setVideoChannelsSort,
-  setVideosSort,
-  setBlacklistSort,
-  setFollowersSort,
-  setFollowingSort,
-  setJobsSort,
-  setVideoCommentThreadsSort,
-  setAccountsSort
+  setDefaultSort,
+  setBlacklistSort
 }