sort.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import express from 'express'
  2. import { query } from 'express-validator'
  3. import { logger } from '@server/helpers/logger'
  4. import { SORTABLE_COLUMNS } from '../../initializers/constants'
  5. import { areValidationErrors } from './shared'
  6. function checkSortFactory (columns: string[], tags: string[] = []) {
  7. return checkSort(createSortableColumns(columns), tags)
  8. }
  9. function checkSort (sortableColumns: string[], tags: string[] = []) {
  10. return [
  11. query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
  12. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  13. logger.debug('Checking sort parameters', { parameters: req.query, tags })
  14. if (areValidationErrors(req, res)) return
  15. return next()
  16. }
  17. ]
  18. }
  19. function createSortableColumns (sortableColumns: string[]) {
  20. const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
  21. return sortableColumns.concat(sortableColumnDesc)
  22. }
  23. const usersSortValidator = checkSortFactory(SORTABLE_COLUMNS.USERS)
  24. const accountsSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS)
  25. const jobsSortValidator = checkSortFactory(SORTABLE_COLUMNS.JOBS, [ 'jobs' ])
  26. const abusesSortValidator = checkSortFactory(SORTABLE_COLUMNS.ABUSES)
  27. const videosSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS)
  28. const videoImportsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_IMPORTS)
  29. const videosSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEOS_SEARCH)
  30. const videoChannelsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH)
  31. const videoPlaylistsSearchSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS_SEARCH)
  32. const videoCommentsValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENTS)
  33. const videoCommentThreadsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
  34. const videoRatesSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_RATES)
  35. const blacklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.BLACKLISTS)
  36. const videoChannelsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_CHANNELS)
  37. const instanceFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWERS)
  38. const instanceFollowingSortValidator = checkSortFactory(SORTABLE_COLUMNS.INSTANCE_FOLLOWING)
  39. const userSubscriptionsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS)
  40. const accountsBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST)
  41. const serversBlocklistSortValidator = checkSortFactory(SORTABLE_COLUMNS.SERVERS_BLOCKLIST)
  42. const userNotificationsSortValidator = checkSortFactory(SORTABLE_COLUMNS.USER_NOTIFICATIONS)
  43. const videoPlaylistsSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_PLAYLISTS)
  44. const pluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.PLUGINS)
  45. const availablePluginsSortValidator = checkSortFactory(SORTABLE_COLUMNS.AVAILABLE_PLUGINS)
  46. const videoRedundanciesSortValidator = checkSortFactory(SORTABLE_COLUMNS.VIDEO_REDUNDANCIES)
  47. const accountsFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.ACCOUNT_FOLLOWERS)
  48. const videoChannelsFollowersSortValidator = checkSortFactory(SORTABLE_COLUMNS.CHANNEL_FOLLOWERS)
  49. // ---------------------------------------------------------------------------
  50. export {
  51. usersSortValidator,
  52. abusesSortValidator,
  53. videoChannelsSortValidator,
  54. videoImportsSortValidator,
  55. videoCommentsValidator,
  56. videosSearchSortValidator,
  57. videosSortValidator,
  58. blacklistSortValidator,
  59. accountsSortValidator,
  60. instanceFollowersSortValidator,
  61. instanceFollowingSortValidator,
  62. jobsSortValidator,
  63. videoCommentThreadsSortValidator,
  64. videoRatesSortValidator,
  65. userSubscriptionsSortValidator,
  66. availablePluginsSortValidator,
  67. videoChannelsSearchSortValidator,
  68. accountsBlocklistSortValidator,
  69. serversBlocklistSortValidator,
  70. userNotificationsSortValidator,
  71. videoPlaylistsSortValidator,
  72. videoRedundanciesSortValidator,
  73. videoPlaylistsSearchSortValidator,
  74. accountsFollowersSortValidator,
  75. videoChannelsFollowersSortValidator,
  76. pluginsSortValidator
  77. }