NotificationsHandler.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const settings = require('@overleaf/settings')
  2. const request = require('request')
  3. const logger = require('@overleaf/logger')
  4. const _ = require('lodash')
  5. const { promisifyAll } = require('@overleaf/promise-utils')
  6. const notificationsApi = _.get(settings, ['apis', 'notifications', 'url'])
  7. const oneSecond = 1000
  8. const makeRequest = function (opts, callback) {
  9. if (notificationsApi) {
  10. request(opts, callback)
  11. } else {
  12. callback(null, { statusCode: 200 })
  13. }
  14. }
  15. const NotificationsHandler = {
  16. getUserNotifications(userId, callback) {
  17. const opts = {
  18. uri: `${notificationsApi}/user/${userId}`,
  19. json: true,
  20. timeout: oneSecond,
  21. method: 'GET',
  22. }
  23. makeRequest(opts, function (err, res, unreadNotifications) {
  24. const statusCode = res ? res.statusCode : 500
  25. if (err || statusCode !== 200) {
  26. logger.err(
  27. { err, statusCode },
  28. 'something went wrong getting notifications'
  29. )
  30. callback(null, [])
  31. } else {
  32. if (unreadNotifications == null) {
  33. unreadNotifications = []
  34. }
  35. callback(null, unreadNotifications)
  36. }
  37. })
  38. },
  39. createNotification(
  40. userId,
  41. key,
  42. templateKey,
  43. messageOpts,
  44. expiryDateTime,
  45. forceCreate,
  46. callback
  47. ) {
  48. if (!callback) {
  49. callback = forceCreate
  50. forceCreate = true
  51. }
  52. const payload = {
  53. key,
  54. messageOpts,
  55. templateKey,
  56. forceCreate,
  57. }
  58. if (expiryDateTime) {
  59. payload.expires = expiryDateTime
  60. }
  61. const opts = {
  62. uri: `${notificationsApi}/user/${userId}`,
  63. timeout: oneSecond,
  64. method: 'POST',
  65. json: payload,
  66. }
  67. makeRequest(opts, callback)
  68. },
  69. markAsReadWithKey(userId, key, callback) {
  70. const opts = {
  71. uri: `${notificationsApi}/user/${userId}`,
  72. method: 'DELETE',
  73. timeout: oneSecond,
  74. json: {
  75. key,
  76. },
  77. }
  78. makeRequest(opts, callback)
  79. },
  80. markAsRead(userId, notificationId, callback) {
  81. const opts = {
  82. method: 'DELETE',
  83. uri: `${notificationsApi}/user/${userId}/notification/${notificationId}`,
  84. timeout: oneSecond,
  85. }
  86. makeRequest(opts, callback)
  87. },
  88. // removes notification by key, without regard for user_id,
  89. // should not be exposed to user via ui/router
  90. markAsReadByKeyOnly(key, callback) {
  91. const opts = {
  92. uri: `${notificationsApi}/key/${key}`,
  93. method: 'DELETE',
  94. timeout: oneSecond,
  95. }
  96. makeRequest(opts, callback)
  97. },
  98. previewMarkAsReadByKeyOnlyBulk(key, callback) {
  99. const opts = {
  100. uri: `${notificationsApi}/key/${key}/count`,
  101. method: 'GET',
  102. timeout: 10 * oneSecond,
  103. json: true,
  104. }
  105. makeRequest(opts, (err, res, body) => {
  106. if (err) return callback(err)
  107. if (res.statusCode !== 200) {
  108. return callback(
  109. new Error('cannot preview bulk delete notification: ' + key)
  110. )
  111. }
  112. callback(null, (body && body.count) || 0)
  113. })
  114. },
  115. markAsReadByKeyOnlyBulk(key, callback) {
  116. const opts = {
  117. uri: `${notificationsApi}/key/${key}/bulk`,
  118. method: 'DELETE',
  119. timeout: 10 * oneSecond,
  120. json: true,
  121. }
  122. makeRequest(opts, (err, res, body) => {
  123. if (err) return callback(err)
  124. if (res.statusCode !== 200) {
  125. return callback(new Error('cannot bulk delete notification: ' + key))
  126. }
  127. callback(null, (body && body.count) || 0)
  128. })
  129. },
  130. }
  131. NotificationsHandler.promises = promisifyAll(NotificationsHandler)
  132. module.exports = NotificationsHandler