2016-01-21 15:42:50 -05:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
|
|
|
|
oneSecond = 1000
|
|
|
|
module.exports =
|
|
|
|
|
|
|
|
getUserNotifications: (user_id, callback)->
|
|
|
|
opts =
|
|
|
|
uri: "#{settings.apis.notifications.url}/user/#{user_id}"
|
|
|
|
json: true
|
2016-02-18 06:43:43 -05:00
|
|
|
timeout: oneSecond
|
2016-01-21 15:42:50 -05:00
|
|
|
request.get opts, (err, res, unreadNotifications)->
|
|
|
|
statusCode = if res? then res.statusCode else 500
|
|
|
|
if err? or statusCode != 200
|
|
|
|
e = new Error("something went wrong getting notifications, #{err}, #{statusCode}")
|
2016-02-04 09:28:31 -05:00
|
|
|
logger.err err:err, "something went wrong getting notifications"
|
2016-01-22 15:08:39 -05:00
|
|
|
callback(null, [])
|
2016-01-21 15:42:50 -05:00
|
|
|
else
|
|
|
|
if !unreadNotifications?
|
|
|
|
unreadNotifications = []
|
|
|
|
callback(null, unreadNotifications)
|
|
|
|
|
2016-02-05 09:13:38 -05:00
|
|
|
createNotification: (user_id, key, templateKey, messageOpts, callback)->
|
|
|
|
opts =
|
|
|
|
uri: "#{settings.apis.notifications.url}/user/#{user_id}"
|
2016-02-18 06:43:43 -05:00
|
|
|
timeout: oneSecond
|
2016-02-05 09:13:38 -05:00
|
|
|
json: {
|
|
|
|
key:key
|
|
|
|
messageOpts:messageOpts
|
|
|
|
templateKey:templateKey
|
|
|
|
}
|
2016-02-17 11:24:09 -05:00
|
|
|
logger.log opts:opts, "creating notification for user"
|
2016-02-05 09:13:38 -05:00
|
|
|
request.post opts, callback
|
|
|
|
|
|
|
|
markAsReadWithKey: (user_id, key, callback)->
|
|
|
|
opts =
|
|
|
|
uri: "#{settings.apis.notifications.url}/user/#{user_id}"
|
2016-02-18 06:43:43 -05:00
|
|
|
timeout: oneSecond
|
2016-02-05 09:13:38 -05:00
|
|
|
json: {
|
|
|
|
key:key
|
|
|
|
}
|
2016-02-18 06:43:43 -05:00
|
|
|
logger.log user_id:user_id, key:key, "sending mark notification as read with key to notifications api"
|
2016-02-05 09:13:38 -05:00
|
|
|
request.del opts, callback
|
|
|
|
|
|
|
|
|
2016-01-21 15:42:50 -05:00
|
|
|
markAsRead: (user_id, notification_id, callback)->
|
|
|
|
opts =
|
|
|
|
uri: "#{settings.apis.notifications.url}/user/#{user_id}/notification/#{notification_id}"
|
|
|
|
timeout:oneSecond
|
2016-02-18 06:43:43 -05:00
|
|
|
logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
|
2016-01-21 15:42:50 -05:00
|
|
|
request.del opts, callback
|