]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Refactor sort middlewares
authorChocobozzz <me@florianbigard.com>
Tue, 19 Oct 2021 07:10:01 +0000 (09:10 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 20 Oct 2021 07:25:44 +0000 (09:25 +0200)
server/controllers/api/server/follows.ts
server/initializers/constants.ts
server/middlewares/validators/shared/utils.ts
server/middlewares/validators/sort.ts

index 2e0fd25ebb2d4dbf0fae0ced9a5c765fbccbeae8..76ed7518685395a46627435a5e6419fc1c203735 100644 (file)
@@ -21,8 +21,8 @@ import {
 } from '../../../middlewares'
 import {
   acceptOrRejectFollowerValidator,
-  followersSortValidator,
-  followingSortValidator,
+  instanceFollowersSortValidator,
+  instanceFollowingSortValidator,
   followValidator,
   getFollowerValidator,
   listFollowsValidator,
@@ -35,7 +35,7 @@ const serverFollowsRouter = express.Router()
 serverFollowsRouter.get('/following',
   listFollowsValidator,
   paginationValidator,
-  followingSortValidator,
+  instanceFollowingSortValidator,
   setDefaultSort,
   setDefaultPagination,
   asyncMiddleware(listFollowing)
@@ -59,7 +59,7 @@ serverFollowsRouter.delete('/following/:hostOrHandle',
 serverFollowsRouter.get('/followers',
   listFollowsValidator,
   paginationValidator,
-  followersSortValidator,
+  instanceFollowersSortValidator,
   setDefaultSort,
   setDefaultPagination,
   asyncMiddleware(listFollowers)
index 9896e1efba793c952303119b2041dc082e77d1fc..02998455982ec72433097b4435c0348a5a3bd093 100644 (file)
@@ -69,8 +69,8 @@ const SORTABLE_COLUMNS = {
 
   VIDEO_RATES: [ 'createdAt' ],
   BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
-  FOLLOWERS: [ 'createdAt', 'state', 'score' ],
-  FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ],
+  INSTANCE_FOLLOWERS: [ 'createdAt', 'state', 'score' ],
+  INSTANCE_FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ],
 
   VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ],
 
index 5c66ad500f39854acafb64df2e151c736daab5bb..8e451a24c133ad6d9e7951b63852491e9dcc8c41 100644 (file)
@@ -22,26 +22,6 @@ function areValidationErrors (req: express.Request, res: express.Response) {
   return false
 }
 
-function checkSort (sortableColumns: string[], tags: string[] = []) {
-  return [
-    query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
-
-    (req: express.Request, res: express.Response, next: express.NextFunction) => {
-      logger.debug('Checking sort parameters', { parameters: req.query, tags })
-
-      if (areValidationErrors(req, res)) return
-
-      return next()
-    }
-  ]
-}
-
-function createSortableColumns (sortableColumns: string[]) {
-  const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
-
-  return sortableColumns.concat(sortableColumnDesc)
-}
-
 function isValidVideoIdParam (paramName: string) {
   return param(paramName)
     .customSanitizer(toCompleteUUID)
@@ -58,8 +38,6 @@ function isValidPlaylistIdParam (paramName: string) {
 
 export {
   areValidationErrors,
-  checkSort,
-  createSortableColumns,
   isValidVideoIdParam,
   isValidPlaylistIdParam
 }
index 473010460b1e5161ab4b16cfbe783e5208dc83be..ce8df8fee12ad3b94c952a0ab3b0d50e4e949f1a 100644 (file)
@@ -1,56 +1,57 @@
+import express from 'express'
+import { query } from 'express-validator'
+import { logger } from '@server/helpers/logger'
 import { SORTABLE_COLUMNS } from '../../initializers/constants'
-import { checkSort, createSortableColumns } from './shared'
+import { areValidationErrors } from './shared'
 
-// Initialize constants here for better performances
-const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
-const SORTABLE_ACCOUNTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS)
-const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS)
-const SORTABLE_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ABUSES)
-const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
-const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH)
-const SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH)
-const SORTABLE_VIDEO_PLAYLISTS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_PLAYLISTS_SEARCH)
-const SORTABLE_VIDEO_IMPORTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_IMPORTS)
-const SORTABLE_VIDEO_COMMENTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
-const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
-const SORTABLE_VIDEO_RATES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_RATES)
-const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS)
-const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS)
-const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS)
-const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWING)
-const SORTABLE_USER_SUBSCRIPTIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS)
-const SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST)
-const SORTABLE_SERVERS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.SERVERS_BLOCKLIST)
-const SORTABLE_USER_NOTIFICATIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_NOTIFICATIONS)
-const SORTABLE_VIDEO_PLAYLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_PLAYLISTS)
-const SORTABLE_PLUGINS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PLUGINS)
-const SORTABLE_AVAILABLE_PLUGINS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.AVAILABLE_PLUGINS)
-const SORTABLE_VIDEO_REDUNDANCIES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_REDUNDANCIES)
+function checkSortFactory (columns: string[], tags: string[] = []) {
+  return checkSort(createSortableColumns(columns), tags)
+}
+
+function checkSort (sortableColumns: string[], tags: string[] = []) {
+  return [
+    query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
+
+    (req: express.Request, res: express.Response, next: express.NextFunction) => {
+      logger.debug('Checking sort parameters', { parameters: req.query, tags })
+
+      if (areValidationErrors(req, res)) return
+
+      return next()
+    }
+  ]
+}
+
+function createSortableColumns (sortableColumns: string[]) {
+  const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
+
+  return sortableColumns.concat(sortableColumnDesc)
+}
 
-const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS)
-const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS)
-const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS, [ 'jobs' ])
-const abusesSortValidator = checkSort(SORTABLE_ABUSES_COLUMNS)
-const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS)
-const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS)
-const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS)
-const videoChannelsSearchSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS)
-const videoPlaylistsSearchSortValidator = checkSort(SORTABLE_VIDEO_PLAYLISTS_SEARCH_COLUMNS)
-const videoCommentsValidator = checkSort(SORTABLE_VIDEO_COMMENTS_COLUMNS)
-const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS)
-const videoRatesSortValidator = checkSort(SORTABLE_VIDEO_RATES_COLUMNS)
-const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
-const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS)
-const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS)
-const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS)
-const userSubscriptionsSortValidator = checkSort(SORTABLE_USER_SUBSCRIPTIONS_COLUMNS)
-const accountsBlocklistSortValidator = checkSort(SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS)
-const serversBlocklistSortValidator = checkSort(SORTABLE_SERVERS_BLOCKLIST_COLUMNS)
-const userNotificationsSortValidator = checkSort(SORTABLE_USER_NOTIFICATIONS_COLUMNS)
-const videoPlaylistsSortValidator = checkSort(SORTABLE_VIDEO_PLAYLISTS_COLUMNS)
-const pluginsSortValidator = checkSort(SORTABLE_PLUGINS_COLUMNS)
-const availablePluginsSortValidator = checkSort(SORTABLE_AVAILABLE_PLUGINS_COLUMNS)
-const videoRedundanciesSortValidator = checkSort(SORTABLE_VIDEO_REDUNDANCIES_COLUMNS)
+const usersSortValidator = checkSortFactory(SORTABLE_COLUMNS.USERS)
+const accountsSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS)
+const jobsSortValidator = checkSortFactory(SORTABLE_COLUMNS.JOBS, [ 'jobs' ])
+const abusesSortValidator = checkSortFactory(SORTABLE_COLUMNS.ABUSES)
+const videosSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS)
+const videoImportsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_IMPORTS)
+const videosSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS_SEARCH)
+const videoChannelsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH)
+const videoPlaylistsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS_SEARCH)
+const videoCommentsValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENTS)
+const videoCommentThreadsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
+const videoRatesSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_RATES)
+const blacklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.BLACKLISTS)
+const videoChannelsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS)
+const instanceFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWERS)
+const instanceFollowingSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWING)
+const userSubscriptionsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS)
+const accountsBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST)
+const serversBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.SERVERS_BLOCKLIST)
+const userNotificationsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_NOTIFICATIONS)
+const videoPlaylistsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS)
+const pluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.PLUGINS)
+const availablePluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.AVAILABLE_PLUGINS)
+const videoRedundanciesSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_REDUNDANCIES)
 
 // ---------------------------------------------------------------------------
 
@@ -64,8 +65,8 @@ export {
   videosSortValidator,
   blacklistSortValidator,
   accountsSortValidator,
-  followersSortValidator,
-  followingSortValidator,
+  instanceFollowersSortValidator,
+  instanceFollowingSortValidator,
   jobsSortValidator,
   videoCommentThreadsSortValidator,
   videoRatesSortValidator,