NotificationsController.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const Notifications = require('./Notifications')
  14. const logger = require('logger-sharelatex')
  15. const metrics = require('@overleaf/metrics')
  16. module.exports = {
  17. getUserNotifications(req, res) {
  18. logger.log(
  19. { user_id: req.params.user_id },
  20. 'getting user unread notifications'
  21. )
  22. metrics.inc('getUserNotifications')
  23. return Notifications.getUserNotifications(
  24. req.params.user_id,
  25. (err, notifications) => res.json(notifications)
  26. )
  27. },
  28. addNotification(req, res) {
  29. logger.log(
  30. { user_id: req.params.user_id, notification: req.body },
  31. 'adding notification'
  32. )
  33. metrics.inc('addNotification')
  34. return Notifications.addNotification(
  35. req.params.user_id,
  36. req.body,
  37. function (err, notifications) {
  38. if (err != null) {
  39. return res.sendStatus(500)
  40. } else {
  41. return res.sendStatus(200)
  42. }
  43. }
  44. )
  45. },
  46. removeNotificationId(req, res) {
  47. logger.log(
  48. {
  49. user_id: req.params.user_id,
  50. notification_id: req.params.notification_id,
  51. },
  52. 'mark id notification as read'
  53. )
  54. metrics.inc('removeNotificationId')
  55. return Notifications.removeNotificationId(
  56. req.params.user_id,
  57. req.params.notification_id,
  58. (err, notifications) => res.sendStatus(200)
  59. )
  60. },
  61. removeNotificationKey(req, res) {
  62. logger.log(
  63. { user_id: req.params.user_id, notification_key: req.body.key },
  64. 'mark key notification as read'
  65. )
  66. metrics.inc('removeNotificationKey')
  67. return Notifications.removeNotificationKey(
  68. req.params.user_id,
  69. req.body.key,
  70. (err, notifications) => res.sendStatus(200)
  71. )
  72. },
  73. removeNotificationByKeyOnly(req, res) {
  74. const notification_key = req.params.key
  75. logger.log({ notification_key }, 'mark notification as read by key only')
  76. metrics.inc('removeNotificationKey')
  77. return Notifications.removeNotificationByKeyOnly(
  78. notification_key,
  79. (err, notifications) => res.sendStatus(200)
  80. )
  81. },
  82. countNotificationsByKeyOnly(req, res) {
  83. const notificationKey = req.params.key
  84. Notifications.countNotificationsByKeyOnly(notificationKey, (err, count) => {
  85. if (err) {
  86. logger.err({ err, notificationKey }, 'cannot count by key')
  87. return res.sendStatus(500)
  88. }
  89. res.json({ count })
  90. })
  91. },
  92. deleteUnreadNotificationsByKeyOnlyBulk(req, res) {
  93. const notificationKey = req.params.key
  94. Notifications.deleteUnreadNotificationsByKeyOnlyBulk(
  95. notificationKey,
  96. (err, count) => {
  97. if (err) {
  98. logger.err({ err, notificationKey }, 'cannot bulk remove by key')
  99. return res.sendStatus(500)
  100. }
  101. res.json({ count })
  102. }
  103. )
  104. },
  105. }