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

90 lines
3.1 KiB
CoffeeScript
Raw Normal View History

2016-01-14 12:35:16 +00:00
Settings = require 'settings-sharelatex'
logger = require('logger-sharelatex')
mongojs = require('mongojs')
db = mongojs(Settings.mongo?.url, ['notifications'])
2016-01-14 20:42:48 +00:00
ObjectId = require("mongojs").ObjectId
2017-03-17 09:47:45 +00:00
metrics = require('metrics-sharelatex')
2016-01-14 12:35:16 +00:00
2017-03-17 09:47:45 +00:00
module.exports = Notifications =
2016-01-14 12:35:16 +00:00
2016-01-14 20:42:48 +00:00
getUserNotifications: (user_id, callback = (err, notifications)->)->
query =
2016-01-21 16:34:12 +00:00
user_id: ObjectId(user_id)
2016-01-14 20:42:48 +00:00
templateKey: {"$exists":true}
db.notifications.find query, (err, notifications)->
callback err, notifications
2016-01-14 12:35:16 +00:00
_checkExistingNotifcationAndOverride : (user_id, notification, callback)->
self = @
2016-01-14 20:42:48 +00:00
query =
2016-01-21 16:34:12 +00:00
user_id: ObjectId(user_id)
2016-01-14 12:35:16 +00:00
key: notification.key
2016-01-14 20:42:48 +00:00
db.notifications.count query, (err, number)->
2016-08-17 10:05:29 +00:00
if number > 0 and !notification.forceCreate
2016-01-14 20:42:48 +00:00
logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user"
return callback(number)
2016-08-17 10:05:29 +00: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 20:42:48 +00:00
else
doc =
2016-01-21 16:34:12 +00:00
user_id: ObjectId(user_id)
2016-01-14 20:42:48 +00: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 20:42:48 +00:00
db.notifications.insert(doc, callback)
2016-01-14 12:35:16 +00:00
2016-02-05 09:38:32 +00:00
removeNotificationId: (user_id, notification_id, callback)->
searchOps =
2016-01-21 16:34:12 +00:00
user_id:ObjectId(user_id)
2016-01-14 20:42:48 +00:00
_id:ObjectId(notification_id)
updateOperation =
2016-01-21 13:40:24 +00:00
"$unset": {templateKey:true, messageOpts: true}
2016-01-14 12:35:16 +00:00
db.notifications.update searchOps, updateOperation, callback
2016-02-05 09:38:32 +00:00
removeNotificationKey: (user_id, notification_key, callback)->
searchOps =
2016-02-05 09:38:32 +00: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 09:38:32 +00: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 09:47:45 +00:00
2017-03-17 15:54:15 +00:00
[
'getUserNotifications',
'addNotification'
].map (method) ->
metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)