diff options
-rw-r--r-- | server/controllers/api/server/follows.ts | 8 | ||||
-rw-r--r-- | server/initializers/constants.ts | 4 | ||||
-rw-r--r-- | server/middlewares/validators/shared/utils.ts | 22 | ||||
-rw-r--r-- | server/middlewares/validators/sort.ts | 105 |
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' |
22 | import { | 22 | import { |
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() | |||
35 | serverFollowsRouter.get('/following', | 35 | serverFollowsRouter.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', | |||
59 | serverFollowsRouter.get('/followers', | 59 | serverFollowsRouter.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 | ||
25 | function 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 | |||
39 | function createSortableColumns (sortableColumns: string[]) { | ||
40 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) | ||
41 | |||
42 | return sortableColumns.concat(sortableColumnDesc) | ||
43 | } | ||
44 | |||
45 | function isValidVideoIdParam (paramName: string) { | 25 | function 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 | ||
59 | export { | 39 | export { |
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 @@ | |||
1 | import express from 'express' | ||
2 | import { query } from 'express-validator' | ||
3 | import { logger } from '@server/helpers/logger' | ||
1 | import { SORTABLE_COLUMNS } from '../../initializers/constants' | 4 | import { SORTABLE_COLUMNS } from '../../initializers/constants' |
2 | import { checkSort, createSortableColumns } from './shared' | 5 | import { areValidationErrors } from './shared' |
3 | 6 | ||
4 | // Initialize constants here for better performances | 7 | function checkSortFactory (columns: string[], tags: string[] = []) { |
5 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) | 8 | return checkSort(createSortableColumns(columns), tags) |
6 | const SORTABLE_ACCOUNTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS) | 9 | } |
7 | const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS) | 10 | |
8 | const SORTABLE_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ABUSES) | 11 | function checkSort (sortableColumns: string[], tags: string[] = []) { |
9 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) | 12 | return [ |
10 | const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH) | 13 | query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'), |
11 | const SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH) | 14 | |
12 | const SORTABLE_VIDEO_PLAYLISTS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_PLAYLISTS_SEARCH) | 15 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
13 | const SORTABLE_VIDEO_IMPORTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_IMPORTS) | 16 | logger.debug('Checking sort parameters', { parameters: req.query, tags }) |
14 | const SORTABLE_VIDEO_COMMENTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) | 17 | |
15 | const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) | 18 | if (areValidationErrors(req, res)) return |
16 | const SORTABLE_VIDEO_RATES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_RATES) | 19 | |
17 | const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) | 20 | return next() |
18 | const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) | 21 | } |
19 | const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS) | 22 | ] |
20 | const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWING) | 23 | } |
21 | const SORTABLE_USER_SUBSCRIPTIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS) | 24 | |
22 | const SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST) | 25 | function createSortableColumns (sortableColumns: string[]) { |
23 | const SORTABLE_SERVERS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.SERVERS_BLOCKLIST) | 26 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) |
24 | const SORTABLE_USER_NOTIFICATIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_NOTIFICATIONS) | 27 | |
25 | const SORTABLE_VIDEO_PLAYLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_PLAYLISTS) | 28 | return sortableColumns.concat(sortableColumnDesc) |
26 | const SORTABLE_PLUGINS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PLUGINS) | 29 | } |
27 | const SORTABLE_AVAILABLE_PLUGINS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.AVAILABLE_PLUGINS) | ||
28 | const SORTABLE_VIDEO_REDUNDANCIES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_REDUNDANCIES) | ||
29 | 30 | ||
30 | const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) | 31 | const usersSortValidator = checkSortFactory(SORTABLE_COLUMNS.USERS) |
31 | const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) | 32 | const accountsSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS) |
32 | const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS, [ 'jobs' ]) | 33 | const jobsSortValidator = checkSortFactory(SORTABLE_COLUMNS.JOBS, [ 'jobs' ]) |
33 | const abusesSortValidator = checkSort(SORTABLE_ABUSES_COLUMNS) | 34 | const abusesSortValidator = checkSortFactory(SORTABLE_COLUMNS.ABUSES) |
34 | const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) | 35 | const videosSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS) |
35 | const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS) | 36 | const videoImportsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_IMPORTS) |
36 | const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS) | 37 | const videosSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS_SEARCH) |
37 | const videoChannelsSearchSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS) | 38 | const videoChannelsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH) |
38 | const videoPlaylistsSearchSortValidator = checkSort(SORTABLE_VIDEO_PLAYLISTS_SEARCH_COLUMNS) | 39 | const videoPlaylistsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS_SEARCH) |
39 | const videoCommentsValidator = checkSort(SORTABLE_VIDEO_COMMENTS_COLUMNS) | 40 | const videoCommentsValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENTS) |
40 | const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS) | 41 | const videoCommentThreadsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) |
41 | const videoRatesSortValidator = checkSort(SORTABLE_VIDEO_RATES_COLUMNS) | 42 | const videoRatesSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_RATES) |
42 | const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) | 43 | const blacklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.BLACKLISTS) |
43 | const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS) | 44 | const videoChannelsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS) |
44 | const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS) | 45 | const instanceFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWERS) |
45 | const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS) | 46 | const instanceFollowingSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWING) |
46 | const userSubscriptionsSortValidator = checkSort(SORTABLE_USER_SUBSCRIPTIONS_COLUMNS) | 47 | const userSubscriptionsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS) |
47 | const accountsBlocklistSortValidator = checkSort(SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS) | 48 | const accountsBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST) |
48 | const serversBlocklistSortValidator = checkSort(SORTABLE_SERVERS_BLOCKLIST_COLUMNS) | 49 | const serversBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.SERVERS_BLOCKLIST) |
49 | const userNotificationsSortValidator = checkSort(SORTABLE_USER_NOTIFICATIONS_COLUMNS) | 50 | const userNotificationsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_NOTIFICATIONS) |
50 | const videoPlaylistsSortValidator = checkSort(SORTABLE_VIDEO_PLAYLISTS_COLUMNS) | 51 | const videoPlaylistsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS) |
51 | const pluginsSortValidator = checkSort(SORTABLE_PLUGINS_COLUMNS) | 52 | const pluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.PLUGINS) |
52 | const availablePluginsSortValidator = checkSort(SORTABLE_AVAILABLE_PLUGINS_COLUMNS) | 53 | const availablePluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.AVAILABLE_PLUGINS) |
53 | const videoRedundanciesSortValidator = checkSort(SORTABLE_VIDEO_REDUNDANCIES_COLUMNS) | 54 | const 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, |