user-notifications.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import * as express from 'express'
  2. import { body, query } from 'express-validator'
  3. import { logger } from '../../helpers/logger'
  4. import { areValidationErrors } from './utils'
  5. import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications'
  6. import { isNotEmptyIntArray, toBooleanOrNull } from '../../helpers/custom-validators/misc'
  7. const listUserNotificationsValidator = [
  8. query('unread')
  9. .optional()
  10. .customSanitizer(toBooleanOrNull)
  11. .isBoolean().withMessage('Should have a valid unread boolean'),
  12. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  13. logger.debug('Checking listUserNotificationsValidator parameters', { parameters: req.query })
  14. if (areValidationErrors(req, res)) return
  15. return next()
  16. }
  17. ]
  18. const updateNotificationSettingsValidator = [
  19. body('newVideoFromSubscription')
  20. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new video from subscription notification setting'),
  21. body('newCommentOnMyVideo')
  22. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new comment on my video notification setting'),
  23. body('videoAbuseAsModerator')
  24. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new video abuse as moderator notification setting'),
  25. body('videoAutoBlacklistAsModerator')
  26. .custom(isUserNotificationSettingValid).withMessage('Should have a valid video auto blacklist notification setting'),
  27. body('blacklistOnMyVideo')
  28. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new blacklist on my video notification setting'),
  29. body('myVideoImportFinished')
  30. .custom(isUserNotificationSettingValid).withMessage('Should have a valid video import finished video notification setting'),
  31. body('myVideoPublished')
  32. .custom(isUserNotificationSettingValid).withMessage('Should have a valid video published notification setting'),
  33. body('commentMention')
  34. .custom(isUserNotificationSettingValid).withMessage('Should have a valid comment mention notification setting'),
  35. body('newFollow')
  36. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new follow notification setting'),
  37. body('newUserRegistration')
  38. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new user registration notification setting'),
  39. body('newInstanceFollower')
  40. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new instance follower notification setting'),
  41. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  42. logger.debug('Checking updateNotificationSettingsValidator parameters', { parameters: req.body })
  43. if (areValidationErrors(req, res)) return
  44. return next()
  45. }
  46. ]
  47. const markAsReadUserNotificationsValidator = [
  48. body('ids')
  49. .optional()
  50. .custom(isNotEmptyIntArray).withMessage('Should have a valid notification ids to mark as read'),
  51. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  52. logger.debug('Checking markAsReadUserNotificationsValidator parameters', { parameters: req.body })
  53. if (areValidationErrors(req, res)) return
  54. return next()
  55. }
  56. ]
  57. // ---------------------------------------------------------------------------
  58. export {
  59. listUserNotificationsValidator,
  60. updateNotificationSettingsValidator,
  61. markAsReadUserNotificationsValidator
  62. }