2
1

my-notifications.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import * as express from 'express'
  2. import 'multer'
  3. import {
  4. asyncMiddleware,
  5. asyncRetryTransactionMiddleware,
  6. authenticate,
  7. paginationValidator,
  8. setDefaultPagination,
  9. setDefaultSort,
  10. userNotificationsSortValidator
  11. } from '../../../middlewares'
  12. import { getFormattedObjects } from '../../../helpers/utils'
  13. import { UserNotificationModel } from '../../../models/account/user-notification'
  14. import { meRouter } from './me'
  15. import {
  16. listUserNotificationsValidator,
  17. markAsReadUserNotificationsValidator,
  18. updateNotificationSettingsValidator
  19. } from '../../../middlewares/validators/user-notifications'
  20. import { UserNotificationSetting } from '../../../../shared/models/users'
  21. import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting'
  22. const myNotificationsRouter = express.Router()
  23. meRouter.put('/me/notification-settings',
  24. authenticate,
  25. updateNotificationSettingsValidator,
  26. asyncRetryTransactionMiddleware(updateNotificationSettings)
  27. )
  28. myNotificationsRouter.get('/me/notifications',
  29. authenticate,
  30. paginationValidator,
  31. userNotificationsSortValidator,
  32. setDefaultSort,
  33. setDefaultPagination,
  34. listUserNotificationsValidator,
  35. asyncMiddleware(listUserNotifications)
  36. )
  37. myNotificationsRouter.post('/me/notifications/read',
  38. authenticate,
  39. markAsReadUserNotificationsValidator,
  40. asyncMiddleware(markAsReadUserNotifications)
  41. )
  42. myNotificationsRouter.post('/me/notifications/read-all',
  43. authenticate,
  44. asyncMiddleware(markAsReadAllUserNotifications)
  45. )
  46. export {
  47. myNotificationsRouter
  48. }
  49. // ---------------------------------------------------------------------------
  50. async function updateNotificationSettings (req: express.Request, res: express.Response) {
  51. const user = res.locals.oauth.token.User
  52. const body = req.body as UserNotificationSetting
  53. const query = {
  54. where: {
  55. userId: user.id
  56. }
  57. }
  58. const values: UserNotificationSetting = {
  59. newVideoFromSubscription: body.newVideoFromSubscription,
  60. newCommentOnMyVideo: body.newCommentOnMyVideo,
  61. videoAbuseAsModerator: body.videoAbuseAsModerator,
  62. videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
  63. blacklistOnMyVideo: body.blacklistOnMyVideo,
  64. myVideoPublished: body.myVideoPublished,
  65. myVideoImportFinished: body.myVideoImportFinished,
  66. newFollow: body.newFollow,
  67. newUserRegistration: body.newUserRegistration,
  68. commentMention: body.commentMention,
  69. newInstanceFollower: body.newInstanceFollower
  70. }
  71. await UserNotificationSettingModel.update(values, query)
  72. return res.status(204).end()
  73. }
  74. async function listUserNotifications (req: express.Request, res: express.Response) {
  75. const user = res.locals.oauth.token.User
  76. const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
  77. return res.json(getFormattedObjects(resultList.data, resultList.total))
  78. }
  79. async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
  80. const user = res.locals.oauth.token.User
  81. await UserNotificationModel.markAsRead(user.id, req.body.ids)
  82. return res.status(204).end()
  83. }
  84. async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
  85. const user = res.locals.oauth.token.User
  86. await UserNotificationModel.markAllAsRead(user.id)
  87. return res.status(204).end()
  88. }