overleaf/services/notifications/app/coffee/Notifications.coffee

95 lines
3.1 KiB
CoffeeScript
Raw Normal View History

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
_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"
return callback(number)
2016-08-17 06:05:29 -04:00
else if number > 0 and notification.forceCreate
self.deleteNotificationByKeyOnly notification.key, callback
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
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
# 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
if notification.expires?
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)->
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)
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)->
searchOps =
2016-02-05 04:38:32 -05:00
user_id:ObjectId(user_id)
key: notification_key
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
# 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
metrics.timeAsyncMethod(
Notifications, 'getUserNotifications',
'Notifications.getUserNotifications',
logger
)
metrics.timeAsyncMethod(
Notifications, 'addNotification',
'Notifications.addNotification',
logger
)