Notifications.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Settings = require('settings-sharelatex')
  16. const logger = require('logger-sharelatex')
  17. const mongojs = require('mongojs')
  18. const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, [
  19. 'notifications'
  20. ])
  21. const { ObjectId } = require('mongojs')
  22. const metrics = require('metrics-sharelatex')
  23. module.exports = Notifications = {
  24. getUserNotifications(user_id, callback) {
  25. if (callback == null) {
  26. callback = function(err, notifications) {}
  27. }
  28. const query = {
  29. user_id: ObjectId(user_id),
  30. templateKey: { $exists: true }
  31. }
  32. return db.notifications.find(query, (err, notifications) =>
  33. callback(err, notifications)
  34. )
  35. },
  36. _countExistingNotifications(user_id, notification, callback) {
  37. if (callback == null) {
  38. callback = function(err, count) {}
  39. }
  40. const query = {
  41. user_id: ObjectId(user_id),
  42. key: notification.key
  43. }
  44. return db.notifications.count(query, function(err, count) {
  45. if (err != null) {
  46. return callback(err)
  47. }
  48. return callback(null, count)
  49. })
  50. },
  51. addNotification(user_id, notification, callback) {
  52. return this._countExistingNotifications(user_id, notification, function(
  53. err,
  54. count
  55. ) {
  56. if (err != null) {
  57. return callback(err)
  58. }
  59. if (count !== 0 && !notification.forceCreate) {
  60. return callback()
  61. }
  62. const doc = {
  63. user_id: ObjectId(user_id),
  64. key: notification.key,
  65. messageOpts: notification.messageOpts,
  66. templateKey: notification.templateKey
  67. }
  68. // TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
  69. // a datetime in the future when the document should be automatically removed.
  70. // in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
  71. // see `README.md` for instruction on creating TTL index
  72. if (notification.expires != null) {
  73. try {
  74. doc.expires = new Date(notification.expires)
  75. const _testValue = doc.expires.toISOString()
  76. } catch (error) {
  77. err = error
  78. logger.error(
  79. { user_id, expires: notification.expires },
  80. 'error converting `expires` field to Date'
  81. )
  82. return callback(err)
  83. }
  84. }
  85. return db.notifications.update(
  86. { user_id: doc.user_id, key: notification.key },
  87. doc,
  88. { upsert: true },
  89. callback
  90. )
  91. })
  92. },
  93. removeNotificationId(user_id, notification_id, callback) {
  94. const searchOps = {
  95. user_id: ObjectId(user_id),
  96. _id: ObjectId(notification_id)
  97. }
  98. const updateOperation = { $unset: { templateKey: true, messageOpts: true } }
  99. return db.notifications.update(searchOps, updateOperation, callback)
  100. },
  101. removeNotificationKey(user_id, notification_key, callback) {
  102. const searchOps = {
  103. user_id: ObjectId(user_id),
  104. key: notification_key
  105. }
  106. const updateOperation = { $unset: { templateKey: true } }
  107. return db.notifications.update(searchOps, updateOperation, callback)
  108. },
  109. removeNotificationByKeyOnly(notification_key, callback) {
  110. const searchOps = { key: notification_key }
  111. const updateOperation = { $unset: { templateKey: true } }
  112. return db.notifications.update(searchOps, updateOperation, callback)
  113. },
  114. // hard delete of doc, rather than removing the templateKey
  115. deleteNotificationByKeyOnly(notification_key, callback) {
  116. const searchOps = { key: notification_key }
  117. return db.notifications.remove(searchOps, { justOne: true }, callback)
  118. }
  119. }
  120. ;['getUserNotifications', 'addNotification'].map(method =>
  121. metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)
  122. )