user-notifications.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import express from 'express'
  2. import { body, query } from 'express-validator'
  3. import { isNotEmptyIntArray, toBooleanOrNull } from '../../helpers/custom-validators/misc'
  4. import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications'
  5. import { logger } from '../../helpers/logger'
  6. import { areValidationErrors } from './shared'
  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('abuseAsModerator')
  24. .custom(isUserNotificationSettingValid).withMessage('Should have a valid 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. body('autoInstanceFollowing')
  42. .custom(isUserNotificationSettingValid).withMessage('Should have a valid new instance following notification setting'),
  43. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  44. logger.debug('Checking updateNotificationSettingsValidator parameters', { parameters: req.body })
  45. if (areValidationErrors(req, res)) return
  46. return next()
  47. }
  48. ]
  49. const markAsReadUserNotificationsValidator = [
  50. body('ids')
  51. .optional()
  52. .custom(isNotEmptyIntArray).withMessage('Should have a valid notification ids to mark as read'),
  53. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  54. logger.debug('Checking markAsReadUserNotificationsValidator parameters', { parameters: req.body })
  55. if (areValidationErrors(req, res)) return
  56. return next()
  57. }
  58. ]
  59. // ---------------------------------------------------------------------------
  60. export {
  61. listUserNotificationsValidator,
  62. updateNotificationSettingsValidator,
  63. markAsReadUserNotificationsValidator
  64. }