2016-01-21 20:42:50 +00:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
|
|
|
|
oneSecond = 1000
|
2016-06-24 13:06:04 +00:00
|
|
|
|
|
|
|
makeRequest = (opts, callback)->
|
2016-06-24 14:11:22 +00:00
|
|
|
if !settings.apis.notifications?.url?
|
2016-06-28 15:07:11 +00:00
|
|
|
return callback(null, statusCode:200)
|
2016-06-24 13:06:04 +00:00
|
|
|
else
|
|
|
|
request(opts, callback)
|
|
|
|
|
2016-01-21 20:42:50 +00:00
|
|
|
module.exports =
|
|
|
|
|
|
|
|
getUserNotifications: (user_id, callback)->
|
|
|
|
opts =
|
2016-06-24 14:23:57 +00:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
|
2016-01-21 20:42:50 +00:00
|
|
|
json: true
|
2016-02-18 11:43:43 +00:00
|
|
|
timeout: oneSecond
|
2016-06-24 13:06:04 +00:00
|
|
|
method: "GET"
|
2016-06-28 15:07:11 +00:00
|
|
|
makeRequest opts, (err, res, unreadNotifications)->
|
2016-01-21 20:42:50 +00:00
|
|
|
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 14:28:31 +00:00
|
|
|
logger.err err:err, "something went wrong getting notifications"
|
2016-01-22 20:08:39 +00:00
|
|
|
callback(null, [])
|
2016-01-21 20:42:50 +00:00
|
|
|
else
|
|
|
|
if !unreadNotifications?
|
|
|
|
unreadNotifications = []
|
|
|
|
callback(null, unreadNotifications)
|
|
|
|
|
2016-02-05 14:13:38 +00:00
|
|
|
createNotification: (user_id, key, templateKey, messageOpts, callback)->
|
|
|
|
opts =
|
2016-06-24 14:23:57 +00:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
|
2016-02-18 11:43:43 +00:00
|
|
|
timeout: oneSecond
|
2016-06-24 13:06:04 +00:00
|
|
|
method:"POST"
|
2016-02-05 14:13:38 +00:00
|
|
|
json: {
|
|
|
|
key:key
|
|
|
|
messageOpts:messageOpts
|
|
|
|
templateKey:templateKey
|
|
|
|
}
|
2016-02-17 16:24:09 +00:00
|
|
|
logger.log opts:opts, "creating notification for user"
|
2016-06-28 15:07:11 +00:00
|
|
|
makeRequest opts, callback
|
2016-02-05 14:13:38 +00:00
|
|
|
|
|
|
|
markAsReadWithKey: (user_id, key, callback)->
|
|
|
|
opts =
|
2016-06-24 14:23:57 +00:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
|
2016-06-24 13:06:04 +00:00
|
|
|
method: "DELETE"
|
2016-02-18 11:43:43 +00:00
|
|
|
timeout: oneSecond
|
2016-02-05 14:13:38 +00:00
|
|
|
json: {
|
|
|
|
key:key
|
|
|
|
}
|
2016-02-18 11:43:43 +00:00
|
|
|
logger.log user_id:user_id, key:key, "sending mark notification as read with key to notifications api"
|
2016-06-28 15:07:11 +00:00
|
|
|
makeRequest opts, callback
|
2016-02-05 14:13:38 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:42:50 +00:00
|
|
|
markAsRead: (user_id, notification_id, callback)->
|
|
|
|
opts =
|
2016-06-24 13:06:04 +00:00
|
|
|
method: "DELETE"
|
2016-06-24 14:23:57 +00:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}/notification/#{notification_id}"
|
2016-01-21 20:42:50 +00:00
|
|
|
timeout:oneSecond
|
2016-02-18 11:43:43 +00:00
|
|
|
logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
|
2016-06-28 15:07:11 +00:00
|
|
|
makeRequest opts, callback
|