Notifications.coffee 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Settings = require 'settings-sharelatex'
  2. logger = require('logger-sharelatex')
  3. mongojs = require('mongojs')
  4. db = mongojs(Settings.mongo?.url, ['notifications'])
  5. ObjectId = require("mongojs").ObjectId
  6. metrics = require('metrics-sharelatex')
  7. module.exports = Notifications =
  8. getUserNotifications: (user_id, callback = (err, notifications)->)->
  9. query =
  10. user_id: ObjectId(user_id)
  11. templateKey: {"$exists":true}
  12. db.notifications.find query, (err, notifications)->
  13. callback err, notifications
  14. _checkExistingNotifcationAndOverride : (user_id, notification, callback)->
  15. self = @
  16. query =
  17. user_id: ObjectId(user_id)
  18. key: notification.key
  19. db.notifications.count query, (err, number)->
  20. if number > 0 and !notification.forceCreate
  21. logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user"
  22. return callback(number)
  23. else if number > 0 and notification.forceCreate
  24. self.deleteNotificationByKeyOnly notification.key, callback
  25. else
  26. callback()
  27. addNotification: (user_id, notification, callback)->
  28. @_checkExistingNotifcationAndOverride user_id, notification, (err)->
  29. if err?
  30. callback err
  31. else
  32. doc =
  33. user_id: ObjectId(user_id)
  34. key: notification.key
  35. messageOpts: notification.messageOpts
  36. templateKey: notification.templateKey
  37. # TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
  38. # a datetime in the future when the document should be automatically removed.
  39. # in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
  40. # see `README.md` for instruction on creating TTL index
  41. if notification.expires?
  42. try
  43. doc.expires = new Date(notification.expires)
  44. _testValue = doc.expires.toISOString()
  45. catch err
  46. logger.error {user_id, expires: notification.expires}, "error converting `expires` field to Date"
  47. return callback(err)
  48. db.notifications.insert(doc, callback)
  49. removeNotificationId: (user_id, notification_id, callback)->
  50. searchOps =
  51. user_id:ObjectId(user_id)
  52. _id:ObjectId(notification_id)
  53. updateOperation =
  54. "$unset": {templateKey:true, messageOpts: true}
  55. db.notifications.update searchOps, updateOperation, callback
  56. removeNotificationKey: (user_id, notification_key, callback)->
  57. searchOps =
  58. user_id:ObjectId(user_id)
  59. key: notification_key
  60. updateOperation =
  61. "$unset": {templateKey:true}
  62. db.notifications.update searchOps, updateOperation, callback
  63. removeNotificationByKeyOnly: (notification_key, callback)->
  64. searchOps =
  65. key: notification_key
  66. updateOperation =
  67. "$unset": {templateKey:true}
  68. db.notifications.update searchOps, updateOperation, callback
  69. # hard delete of doc, rather than removing the templateKey
  70. deleteNotificationByKeyOnly: (notification_key, callback)->
  71. searchOps =
  72. key: notification_key
  73. db.notifications.remove searchOps, {justOne: true}, callback
  74. [
  75. 'getUserNotifications',
  76. 'addNotification'
  77. ].map (method) ->
  78. metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)