Notifications.js 3.6 KB

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