aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-10-19 09:10:01 +0200
committerChocobozzz <me@florianbigard.com>2021-10-20 09:25:44 +0200
commit9593a78ae1368a9ad8bb11044fce6fde2892701a (patch)
tree6a048d32cdcfdfc3c1ea6c02c4a86d8861bac1e7 /server
parent9c58375c082dc044f683487e81c0a80a10b3a087 (diff)
downloadPeerTube-9593a78ae1368a9ad8bb11044fce6fde2892701a.tar.gz
PeerTube-9593a78ae1368a9ad8bb11044fce6fde2892701a.tar.zst
PeerTube-9593a78ae1368a9ad8bb11044fce6fde2892701a.zip
Refactor sort middlewares
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/server/follows.ts8
-rw-r--r--server/initializers/constants.ts4
-rw-r--r--server/middlewares/validators/shared/utils.ts22
-rw-r--r--server/middlewares/validators/sort.ts105
4 files changed, 59 insertions, 80 deletions
diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts
index 2e0fd25eb..76ed75186 100644
--- a/server/controllers/api/server/follows.ts
+++ b/server/controllers/api/server/follows.ts
@@ -21,8 +21,8 @@ import {
21} from '../../../middlewares' 21} from '../../../middlewares'
22import { 22import {
23 acceptOrRejectFollowerValidator, 23 acceptOrRejectFollowerValidator,
24 followersSortValidator, 24 instanceFollowersSortValidator,
25 followingSortValidator, 25 instanceFollowingSortValidator,
26 followValidator, 26 followValidator,
27 getFollowerValidator, 27 getFollowerValidator,
28 listFollowsValidator, 28 listFollowsValidator,
@@ -35,7 +35,7 @@ const serverFollowsRouter = express.Router()
35serverFollowsRouter.get('/following', 35serverFollowsRouter.get('/following',
36 listFollowsValidator, 36 listFollowsValidator,
37 paginationValidator, 37 paginationValidator,
38 followingSortValidator, 38 instanceFollowingSortValidator,
39 setDefaultSort, 39 setDefaultSort,
40 setDefaultPagination, 40 setDefaultPagination,
41 asyncMiddleware(listFollowing) 41 asyncMiddleware(listFollowing)
@@ -59,7 +59,7 @@ serverFollowsRouter.delete('/following/:hostOrHandle',
59serverFollowsRouter.get('/followers', 59serverFollowsRouter.get('/followers',
60 listFollowsValidator, 60 listFollowsValidator,
61 paginationValidator, 61 paginationValidator,
62 followersSortValidator, 62 instanceFollowersSortValidator,
63 setDefaultSort, 63 setDefaultSort,
64 setDefaultPagination, 64 setDefaultPagination,
65 asyncMiddleware(listFollowers) 65 asyncMiddleware(listFollowers)
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 9896e1efb..029984559 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -69,8 +69,8 @@ const SORTABLE_COLUMNS = {
69 69
70 VIDEO_RATES: [ 'createdAt' ], 70 VIDEO_RATES: [ 'createdAt' ],
71 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ], 71 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
72 FOLLOWERS: [ 'createdAt', 'state', 'score' ], 72 INSTANCE_FOLLOWERS: [ 'createdAt', 'state', 'score' ],
73 FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ], 73 INSTANCE_FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ],
74 74
75 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ], 75 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ],
76 76
diff --git a/server/middlewares/validators/shared/utils.ts b/server/middlewares/validators/shared/utils.ts
index 5c66ad500..8e451a24c 100644
--- a/server/middlewares/validators/shared/utils.ts
+++ b/server/middlewares/validators/shared/utils.ts
@@ -22,26 +22,6 @@ function areValidationErrors (req: express.Request, res: express.Response) {
22 return false 22 return false
23} 23}
24 24
25function checkSort (sortableColumns: string[], tags: string[] = []) {
26 return [
27 query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
28
29 (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 logger.debug('Checking sort parameters', { parameters: req.query, tags })
31
32 if (areValidationErrors(req, res)) return
33
34 return next()
35 }
36 ]
37}
38
39function createSortableColumns (sortableColumns: string[]) {
40 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
41
42 return sortableColumns.concat(sortableColumnDesc)
43}
44
45function isValidVideoIdParam (paramName: string) { 25function isValidVideoIdParam (paramName: string) {
46 return param(paramName) 26 return param(paramName)
47 .customSanitizer(toCompleteUUID) 27 .customSanitizer(toCompleteUUID)
@@ -58,8 +38,6 @@ function isValidPlaylistIdParam (paramName: string) {
58 38
59export { 39export {
60 areValidationErrors, 40 areValidationErrors,
61 checkSort,
62 createSortableColumns,
63 isValidVideoIdParam, 41 isValidVideoIdParam,
64 isValidPlaylistIdParam 42 isValidPlaylistIdParam
65} 43}
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts
index 473010460..ce8df8fee 100644
--- a/server/middlewares/validators/sort.ts
+++ b/server/middlewares/validators/sort.ts
@@ -1,56 +1,57 @@
1import express from 'express'
2import { query } from 'express-validator'
3import { logger } from '@server/helpers/logger'
1import { SORTABLE_COLUMNS } from '../../initializers/constants' 4import { SORTABLE_COLUMNS } from '../../initializers/constants'
2import { checkSort, createSortableColumns } from './shared' 5import { areValidationErrors } from './shared'
3 6
4// Initialize constants here for better performances 7function checkSortFactory (columns: string[], tags: string[] = []) {
5const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) 8 return checkSort(createSortableColumns(columns), tags)
6const SORTABLE_ACCOUNTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS) 9}
7const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS) 10
8const SORTABLE_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ABUSES) 11function checkSort (sortableColumns: string[], tags: string[] = []) {
9const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) 12 return [
10const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH) 13 query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
11const SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH) 14
12const SORTABLE_VIDEO_PLAYLISTS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_PLAYLISTS_SEARCH) 15 (req: express.Request, res: express.Response, next: express.NextFunction) => {
13const SORTABLE_VIDEO_IMPORTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_IMPORTS) 16 logger.debug('Checking sort parameters', { parameters: req.query, tags })
14const SORTABLE_VIDEO_COMMENTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) 17
15const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) 18 if (areValidationErrors(req, res)) return
16const SORTABLE_VIDEO_RATES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_RATES) 19
17const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) 20 return next()
18const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) 21 }
19const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS) 22 ]
20const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWING) 23}
21const SORTABLE_USER_SUBSCRIPTIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS) 24
22const SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST) 25function createSortableColumns (sortableColumns: string[]) {
23const SORTABLE_SERVERS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.SERVERS_BLOCKLIST) 26 const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
24const SORTABLE_USER_NOTIFICATIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_NOTIFICATIONS) 27
25const SORTABLE_VIDEO_PLAYLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_PLAYLISTS) 28 return sortableColumns.concat(sortableColumnDesc)
26const SORTABLE_PLUGINS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PLUGINS) 29}
27const SORTABLE_AVAILABLE_PLUGINS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.AVAILABLE_PLUGINS)
28const SORTABLE_VIDEO_REDUNDANCIES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_REDUNDANCIES)
29 30
30const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) 31const usersSortValidator = checkSortFactory(SORTABLE_COLUMNS.USERS)
31const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) 32const accountsSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS)
32const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS, [ 'jobs' ]) 33const jobsSortValidator = checkSortFactory(SORTABLE_COLUMNS.JOBS, [ 'jobs' ])
33const abusesSortValidator = checkSort(SORTABLE_ABUSES_COLUMNS) 34const abusesSortValidator = checkSortFactory(SORTABLE_COLUMNS.ABUSES)
34const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) 35const videosSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS)
35const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS) 36const videoImportsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_IMPORTS)
36const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS) 37const videosSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS_SEARCH)
37const videoChannelsSearchSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS) 38const videoChannelsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH)
38const videoPlaylistsSearchSortValidator = checkSort(SORTABLE_VIDEO_PLAYLISTS_SEARCH_COLUMNS) 39const videoPlaylistsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS_SEARCH)
39const videoCommentsValidator = checkSort(SORTABLE_VIDEO_COMMENTS_COLUMNS) 40const videoCommentsValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENTS)
40const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS) 41const videoCommentThreadsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
41const videoRatesSortValidator = checkSort(SORTABLE_VIDEO_RATES_COLUMNS) 42const videoRatesSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_RATES)
42const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) 43const blacklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.BLACKLISTS)
43const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS) 44const videoChannelsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS)
44const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS) 45const instanceFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWERS)
45const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS) 46const instanceFollowingSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWING)
46const userSubscriptionsSortValidator = checkSort(SORTABLE_USER_SUBSCRIPTIONS_COLUMNS) 47const userSubscriptionsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS)
47const accountsBlocklistSortValidator = checkSort(SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS) 48const accountsBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST)
48const serversBlocklistSortValidator = checkSort(SORTABLE_SERVERS_BLOCKLIST_COLUMNS) 49const serversBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.SERVERS_BLOCKLIST)
49const userNotificationsSortValidator = checkSort(SORTABLE_USER_NOTIFICATIONS_COLUMNS) 50const userNotificationsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_NOTIFICATIONS)
50const videoPlaylistsSortValidator = checkSort(SORTABLE_VIDEO_PLAYLISTS_COLUMNS) 51const videoPlaylistsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS)
51const pluginsSortValidator = checkSort(SORTABLE_PLUGINS_COLUMNS) 52const pluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.PLUGINS)
52const availablePluginsSortValidator = checkSort(SORTABLE_AVAILABLE_PLUGINS_COLUMNS) 53const availablePluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.AVAILABLE_PLUGINS)
53const videoRedundanciesSortValidator = checkSort(SORTABLE_VIDEO_REDUNDANCIES_COLUMNS) 54const videoRedundanciesSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_REDUNDANCIES)
54 55
55// --------------------------------------------------------------------------- 56// ---------------------------------------------------------------------------
56 57
@@ -64,8 +65,8 @@ export {
64 videosSortValidator, 65 videosSortValidator,
65 blacklistSortValidator, 66 blacklistSortValidator,
66 accountsSortValidator, 67 accountsSortValidator,
67 followersSortValidator, 68 instanceFollowersSortValidator,
68 followingSortValidator, 69 instanceFollowingSortValidator,
69 jobsSortValidator, 70 jobsSortValidator,
70 videoCommentThreadsSortValidator, 71 videoCommentThreadsSortValidator,
71 videoRatesSortValidator, 72 videoRatesSortValidator,