Notifications.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(
  46. user_id,
  47. notification,
  48. function (err, count) {
  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. },
  87. removeNotificationId(user_id, notification_id, callback) {
  88. const searchOps = {
  89. user_id: ObjectId(user_id),
  90. _id: ObjectId(notification_id),
  91. }
  92. const updateOperation = { $unset: { templateKey: true, messageOpts: true } }
  93. db.notifications.updateOne(searchOps, updateOperation, callback)
  94. },
  95. removeNotificationKey(user_id, notification_key, callback) {
  96. const searchOps = {
  97. user_id: ObjectId(user_id),
  98. key: notification_key,
  99. }
  100. const updateOperation = { $unset: { templateKey: true } }
  101. db.notifications.updateOne(searchOps, updateOperation, callback)
  102. },
  103. removeNotificationByKeyOnly(notification_key, callback) {
  104. const searchOps = { key: notification_key }
  105. const updateOperation = { $unset: { templateKey: true } }
  106. db.notifications.updateOne(searchOps, updateOperation, callback)
  107. },
  108. // hard delete of doc, rather than removing the templateKey
  109. deleteNotificationByKeyOnly(notification_key, callback) {
  110. const searchOps = { key: notification_key }
  111. db.notifications.deleteOne(searchOps, callback)
  112. },
  113. }
  114. ;['getUserNotifications', 'addNotification'].map(method =>
  115. metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)
  116. )