Notifications.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let Notifications
  15. const logger = require('logger-sharelatex')
  16. const { db, ObjectId } = require('./mongodb')
  17. const metrics = require('@overleaf/metrics')
  18. module.exports = Notifications = {
  19. getUserNotifications(user_id, callback) {
  20. if (callback == null) {
  21. callback = function (err, notifications) {}
  22. }
  23. const query = {
  24. user_id: ObjectId(user_id),
  25. templateKey: { $exists: true }
  26. }
  27. db.notifications.find(query).toArray(callback)
  28. },
  29. _countExistingNotifications(user_id, notification, callback) {
  30. if (callback == null) {
  31. callback = function (err, count) {}
  32. }
  33. const query = {
  34. user_id: ObjectId(user_id),
  35. key: notification.key
  36. }
  37. return db.notifications.count(query, function (err, count) {
  38. if (err != null) {
  39. return callback(err)
  40. }
  41. return callback(null, count)
  42. })
  43. },
  44. addNotification(user_id, notification, callback) {
  45. return this._countExistingNotifications(user_id, notification, function (
  46. err,
  47. count
  48. ) {
  49. if (err != null) {
  50. return callback(err)
  51. }
  52. if (count !== 0 && !notification.forceCreate) {
  53. return callback()
  54. }
  55. const doc = {
  56. user_id: ObjectId(user_id),
  57. key: notification.key,
  58. messageOpts: notification.messageOpts,
  59. templateKey: notification.templateKey
  60. }
  61. // TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
  62. // a datetime in the future when the document should be automatically removed.
  63. // in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
  64. // see `README.md` for instruction on creating TTL index
  65. if (notification.expires != null) {
  66. try {
  67. doc.expires = new Date(notification.expires)
  68. const _testValue = doc.expires.toISOString()
  69. } catch (error) {
  70. err = error
  71. logger.error(
  72. { user_id, expires: notification.expires },
  73. 'error converting `expires` field to Date'
  74. )
  75. return callback(err)
  76. }
  77. }
  78. db.notifications.updateOne(
  79. { user_id: doc.user_id, key: notification.key },
  80. { $set: doc },
  81. { upsert: true },
  82. callback
  83. )
  84. })
  85. },
  86. removeNotificationId(user_id, notification_id, callback) {
  87. const searchOps = {
  88. user_id: ObjectId(user_id),
  89. _id: ObjectId(notification_id)
  90. }
  91. const updateOperation = { $unset: { templateKey: true, messageOpts: true } }
  92. db.notifications.updateOne(searchOps, updateOperation, callback)
  93. },
  94. removeNotificationKey(user_id, notification_key, callback) {
  95. const searchOps = {
  96. user_id: ObjectId(user_id),
  97. key: notification_key
  98. }
  99. const updateOperation = { $unset: { templateKey: true } }
  100. db.notifications.updateOne(searchOps, updateOperation, callback)
  101. },
  102. removeNotificationByKeyOnly(notification_key, callback) {
  103. const searchOps = { key: notification_key }
  104. const updateOperation = { $unset: { templateKey: true } }
  105. db.notifications.updateOne(searchOps, updateOperation, callback)
  106. },
  107. // hard delete of doc, rather than removing the templateKey
  108. deleteNotificationByKeyOnly(notification_key, callback) {
  109. const searchOps = { key: notification_key }
  110. db.notifications.deleteOne(searchOps, callback)
  111. }
  112. }
  113. ;['getUserNotifications', 'addNotification'].map((method) =>
  114. metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)
  115. )