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
|
2016-01-14 07:35:16 -05:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
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
|
|
|
|
|
|
|
addNotification: (user_id, notification, callback)->
|
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)->
|
|
|
|
if number > 0
|
|
|
|
logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user"
|
|
|
|
callback number
|
|
|
|
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
|
|
|
|
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-01-14 07:35:16 -05: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-01-14 07:35:16 -05: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)->
|
|
|
|
searchOps =
|
|
|
|
user_id:ObjectId(user_id)
|
|
|
|
key: notification_key
|
|
|
|
updateOperation =
|
|
|
|
"$unset": {templateKey:true}
|
|
|
|
db.notifications.update searchOps, updateOperation, callback
|