X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fsort.ts;h=7d063910799f419d607724e7420a14040b8716b5;hb=9972ace3a3bc65865fb3aaaa865a400386e49252;hp=3baee9fb3df437236e675b0d4223f2fe61c26721;hpb=69818c9394366b954b6ba3bd697bd9d2b09f2a16;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index 3baee9fb3..7d0639107 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -1,47 +1,90 @@ -import 'express-validator' -import * as express from 'express' +import express from 'express' +import { query } from 'express-validator' -import { checkErrors } from './utils' -import { logger } from '../../helpers' -import { SORTABLE_COLUMNS } from '../../initializers' +import { SORTABLE_COLUMNS } from '../../initializers/constants' +import { areValidationErrors } from './shared' -// Initialize constants here for better performances -const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) -const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) -const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) - -function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { - checkSort(req, res, next, SORTABLE_USERS_COLUMNS) +function checkSortFactory (columns: string[], tags: string[] = []) { + return checkSort(createSortableColumns(columns), tags) } -function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { - checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) -} +function checkSort (sortableColumns: string[], tags: string[] = []) { + return [ + query('sort') + .optional() + .isIn(sortableColumns), -function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { - checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) -} - -// --------------------------------------------------------------------------- + (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (areValidationErrors(req, res, { tags })) return -export { - usersSortValidator, - videoAbusesSortValidator, - videosSortValidator + return next() + } + ] } -// --------------------------------------------------------------------------- +function createSortableColumns (sortableColumns: string[]) { + const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) -function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) { - req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) + return sortableColumns.concat(sortableColumnDesc) +} - logger.debug('Checking sort parameters', { parameters: req.query }) +const adminUsersSortValidator = checkSortFactory(SORTABLE_COLUMNS.ADMIN_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) +const videoChannelSyncsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNEL_SYNCS) - checkErrors(req, res, next) -} +const accountsFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNT_FOLLOWERS) +const videoChannelsFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.CHANNEL_FOLLOWERS) -function createSortableColumns (sortableColumns: string[]) { - const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) +// --------------------------------------------------------------------------- - return sortableColumns.concat(sortableColumnDesc) +export { + adminUsersSortValidator, + abusesSortValidator, + videoChannelsSortValidator, + videoImportsSortValidator, + videoCommentsValidator, + videosSearchSortValidator, + videosSortValidator, + blacklistSortValidator, + accountsSortValidator, + instanceFollowersSortValidator, + instanceFollowingSortValidator, + jobsSortValidator, + videoCommentThreadsSortValidator, + videoRatesSortValidator, + userSubscriptionsSortValidator, + availablePluginsSortValidator, + videoChannelsSearchSortValidator, + accountsBlocklistSortValidator, + serversBlocklistSortValidator, + userNotificationsSortValidator, + videoPlaylistsSortValidator, + videoRedundanciesSortValidator, + videoPlaylistsSearchSortValidator, + accountsFollowersSortValidator, + videoChannelsFollowersSortValidator, + videoChannelSyncsSortValidator, + pluginsSortValidator }