my-notifications.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'multer'
  2. import express from 'express'
  3. import { HttpStatusCode, UserNotificationSetting } from '@peertube/peertube-models'
  4. import { getFormattedObjects } from '@server/helpers/utils.js'
  5. import { UserNotificationModel } from '@server/models/user/user-notification.js'
  6. import {
  7. asyncMiddleware,
  8. asyncRetryTransactionMiddleware,
  9. authenticate,
  10. paginationValidator,
  11. setDefaultPagination,
  12. setDefaultSort,
  13. userNotificationsSortValidator
  14. } from '../../../middlewares/index.js'
  15. import {
  16. listUserNotificationsValidator,
  17. markAsReadUserNotificationsValidator,
  18. updateNotificationSettingsValidator
  19. } from '../../../middlewares/validators/users/user-notifications.js'
  20. import { UserNotificationSettingModel } from '../../../models/user/user-notification-setting.js'
  21. import { meRouter } from './me.js'
  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 values: UserNotificationSetting = {
  54. newVideoFromSubscription: body.newVideoFromSubscription,
  55. newCommentOnMyVideo: body.newCommentOnMyVideo,
  56. abuseAsModerator: body.abuseAsModerator,
  57. videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
  58. blacklistOnMyVideo: body.blacklistOnMyVideo,
  59. myVideoPublished: body.myVideoPublished,
  60. myVideoImportFinished: body.myVideoImportFinished,
  61. newFollow: body.newFollow,
  62. newUserRegistration: body.newUserRegistration,
  63. commentMention: body.commentMention,
  64. newInstanceFollower: body.newInstanceFollower,
  65. autoInstanceFollowing: body.autoInstanceFollowing,
  66. abuseNewMessage: body.abuseNewMessage,
  67. abuseStateChange: body.abuseStateChange,
  68. newPeerTubeVersion: body.newPeerTubeVersion,
  69. newPluginVersion: body.newPluginVersion,
  70. myVideoStudioEditionFinished: body.myVideoStudioEditionFinished
  71. }
  72. await UserNotificationSettingModel.updateUserSettings(values, user.id)
  73. return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
  74. }
  75. async function listUserNotifications (req: express.Request, res: express.Response) {
  76. const user = res.locals.oauth.token.User
  77. const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
  78. return res.json(getFormattedObjects(resultList.data, resultList.total))
  79. }
  80. async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
  81. const user = res.locals.oauth.token.User
  82. await UserNotificationModel.markAsRead(user.id, req.body.ids)
  83. return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
  84. }
  85. async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
  86. const user = res.locals.oauth.token.User
  87. await UserNotificationModel.markAllAsRead(user.id)
  88. return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
  89. }