aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
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/middlewares/validators
parent9c58375c082dc044f683487e81c0a80a10b3a087 (diff)
downloadPeerTube-9593a78ae1368a9ad8bb11044fce6fde2892701a.tar.gz
PeerTube-9593a78ae1368a9ad8bb11044fce6fde2892701a.tar.zst
PeerTube-9593a78ae1368a9ad8bb11044fce6fde2892701a.zip
Refactor sort middlewares
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/shared/utils.ts22
-rw-r--r--server/middlewares/validators/sort.ts105
2 files changed, 53 insertions, 74 deletions
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,