2016-01-14 07:35:16 -05:00
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
mongojs = require('mongojs')
|
|
|
|
db = mongojs(Settings.mongo?.url, ['notifications'])
|
2016-01-14 15:42:48 -05:00
|
|
|
ObjectId = require("mongojs").ObjectId
|
2017-03-17 05:47:45 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2017-03-17 05:47:45 -04:00
|
|
|
module.exports = Notifications =
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2016-01-14 15:42:48 -05:00
|
|
|
getUserNotifications: (user_id, callback = (err, notifications)->)->
|
|
|
|
query =
|
2016-01-21 11:34:12 -05:00
|
|
|
user_id: ObjectId(user_id)
|
2016-01-14 15:42:48 -05:00
|
|
|
templateKey: {"$exists":true}
|
|
|
|
db.notifications.find query, (err, notifications)->
|
|
|
|
callback err, notifications
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2016-08-17 05:52:35 -04:00
|
|
|
|
|
|
|
_checkExistingNotifcationAndOverride : (user_id, notification, callback)->
|
|
|
|
self = @
|
2016-01-14 15:42:48 -05:00
|
|
|
query =
|
2016-01-21 11:34:12 -05:00
|
|
|
user_id: ObjectId(user_id)
|
2016-01-14 07:35:16 -05:00
|
|
|
key: notification.key
|
2016-01-14 15:42:48 -05:00
|
|
|
db.notifications.count query, (err, number)->
|
2016-08-17 06:05:29 -04:00
|
|
|
if number > 0 and !notification.forceCreate
|
2016-01-14 15:42:48 -05:00
|
|
|
logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user"
|
2016-08-17 05:52:35 -04:00
|
|
|
return callback(number)
|
2016-08-17 06:05:29 -04:00
|
|
|
else if number > 0 and notification.forceCreate
|
2016-08-17 09:10:36 -04:00
|
|
|
self.deleteNotificationByKeyOnly notification.key, callback
|
2016-08-17 05:52:35 -04:00
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
|
|
|
addNotification: (user_id, notification, callback)->
|
|
|
|
@_checkExistingNotifcationAndOverride user_id, notification, (err)->
|
|
|
|
if err?
|
|
|
|
callback err
|
2016-01-14 15:42:48 -05:00
|
|
|
else
|
2016-08-11 05:01:21 -04:00
|
|
|
doc =
|
2016-01-21 11:34:12 -05:00
|
|
|
user_id: ObjectId(user_id)
|
2016-01-14 15:42:48 -05:00
|
|
|
key: notification.key
|
|
|
|
messageOpts: notification.messageOpts
|
|
|
|
templateKey: notification.templateKey
|
2016-08-12 06:38:17 -04:00
|
|
|
# TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
|
|
|
|
# a datetime in the future when the document should be automatically removed.
|
|
|
|
# in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
|
|
|
|
# see `README.md` for instruction on creating TTL index
|
2016-08-12 04:54:08 -04:00
|
|
|
if notification.expires?
|
2016-08-15 03:54:45 -04:00
|
|
|
try
|
|
|
|
doc.expires = new Date(notification.expires)
|
|
|
|
_testValue = doc.expires.toISOString()
|
|
|
|
catch err
|
|
|
|
logger.error {user_id, expires: notification.expires}, "error converting `expires` field to Date"
|
|
|
|
return callback(err)
|
2016-01-14 15:42:48 -05:00
|
|
|
db.notifications.insert(doc, callback)
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2016-02-05 04:38:32 -05:00
|
|
|
removeNotificationId: (user_id, notification_id, callback)->
|
2016-08-11 05:01:21 -04:00
|
|
|
searchOps =
|
2016-01-21 11:34:12 -05:00
|
|
|
user_id:ObjectId(user_id)
|
2016-01-14 15:42:48 -05:00
|
|
|
_id:ObjectId(notification_id)
|
2016-08-11 05:01:21 -04:00
|
|
|
updateOperation =
|
2016-01-21 08:40:24 -05:00
|
|
|
"$unset": {templateKey:true, messageOpts: true}
|
2016-01-14 07:35:16 -05:00
|
|
|
db.notifications.update searchOps, updateOperation, callback
|
2016-02-05 04:38:32 -05:00
|
|
|
|
|
|
|
removeNotificationKey: (user_id, notification_key, callback)->
|
2016-08-11 05:01:21 -04:00
|
|
|
searchOps =
|
2016-02-05 04:38:32 -05:00
|
|
|
user_id:ObjectId(user_id)
|
|
|
|
key: notification_key
|
2016-08-11 05:01:21 -04:00
|
|
|
updateOperation =
|
|
|
|
"$unset": {templateKey:true}
|
|
|
|
db.notifications.update searchOps, updateOperation, callback
|
|
|
|
|
|
|
|
removeNotificationByKeyOnly: (notification_key, callback)->
|
|
|
|
searchOps =
|
|
|
|
key: notification_key
|
|
|
|
updateOperation =
|
2016-02-05 04:38:32 -05:00
|
|
|
"$unset": {templateKey:true}
|
|
|
|
db.notifications.update searchOps, updateOperation, callback
|
2016-08-17 09:10:36 -04:00
|
|
|
|
|
|
|
# hard delete of doc, rather than removing the templateKey
|
|
|
|
deleteNotificationByKeyOnly: (notification_key, callback)->
|
|
|
|
searchOps =
|
|
|
|
key: notification_key
|
|
|
|
db.notifications.remove searchOps, {justOne: true}, callback
|
2017-03-17 05:47:45 -04:00
|
|
|
|
|
|
|
|
2017-03-17 11:54:15 -04:00
|
|
|
[
|
|
|
|
'getUserNotifications',
|
|
|
|
'addNotification'
|
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)
|