2016-01-21 15:42:50 -05:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
|
|
|
|
oneSecond = 1000
|
2016-06-24 09:06:04 -04:00
|
|
|
|
|
|
|
makeRequest = (opts, callback)->
|
2016-06-24 10:11:22 -04:00
|
|
|
if !settings.apis.notifications?.url?
|
2016-06-28 11:07:11 -04:00
|
|
|
return callback(null, statusCode:200)
|
2016-06-24 09:06:04 -04:00
|
|
|
else
|
|
|
|
request(opts, callback)
|
|
|
|
|
2016-08-12 04:59:25 -04:00
|
|
|
module.exports =
|
2016-01-21 15:42:50 -05:00
|
|
|
|
|
|
|
getUserNotifications: (user_id, callback)->
|
2016-08-12 04:59:25 -04:00
|
|
|
opts =
|
2016-06-24 10:23:57 -04:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
|
2016-01-21 15:42:50 -05:00
|
|
|
json: true
|
2016-02-18 06:43:43 -05:00
|
|
|
timeout: oneSecond
|
2016-06-24 09:06:04 -04:00
|
|
|
method: "GET"
|
2016-06-28 11:07:11 -04:00
|
|
|
makeRequest opts, (err, res, unreadNotifications)->
|
2016-01-21 15:42:50 -05: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 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-08-30 08:07:37 -04:00
|
|
|
createNotification: (user_id, key, templateKey, messageOpts, expiryDateTime, callback)->
|
2016-08-12 09:40:59 -04:00
|
|
|
payload = {
|
|
|
|
key:key
|
|
|
|
messageOpts:messageOpts
|
|
|
|
templateKey:templateKey
|
2016-08-30 08:07:37 -04:00
|
|
|
forceCreate: true
|
2016-08-12 09:40:59 -04:00
|
|
|
}
|
|
|
|
if expiryDateTime?
|
|
|
|
payload.expires = expiryDateTime
|
2016-08-12 04:59:25 -04:00
|
|
|
opts =
|
2016-06-24 10:23:57 -04:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
|
2016-02-18 06:43:43 -05:00
|
|
|
timeout: oneSecond
|
2016-06-24 09:06:04 -04:00
|
|
|
method:"POST"
|
2016-08-12 09:40:59 -04:00
|
|
|
json: payload
|
2016-02-17 11:24:09 -05:00
|
|
|
logger.log opts:opts, "creating notification for user"
|
2016-06-28 11:07:11 -04:00
|
|
|
makeRequest opts, callback
|
2016-02-05 09:13:38 -05:00
|
|
|
|
|
|
|
markAsReadWithKey: (user_id, key, callback)->
|
2016-08-12 04:59:25 -04:00
|
|
|
opts =
|
2016-06-24 10:23:57 -04:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
|
2016-06-24 09:06:04 -04:00
|
|
|
method: "DELETE"
|
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-06-28 11:07:11 -04:00
|
|
|
makeRequest opts, callback
|
2016-08-12 04:59:25 -04:00
|
|
|
|
2016-02-05 09:13:38 -05:00
|
|
|
|
2016-01-21 15:42:50 -05:00
|
|
|
markAsRead: (user_id, notification_id, callback)->
|
|
|
|
opts =
|
2016-06-24 09:06:04 -04:00
|
|
|
method: "DELETE"
|
2016-06-24 10:23:57 -04:00
|
|
|
uri: "#{settings.apis.notifications?.url}/user/#{user_id}/notification/#{notification_id}"
|
2016-01-21 15:42:50 -05:00
|
|
|
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-06-28 11:07:11 -04:00
|
|
|
makeRequest opts, callback
|
2016-08-11 09:04:11 -04:00
|
|
|
|
|
|
|
# removes notification by key, without regard for user_id,
|
|
|
|
# should not be exposed to user via ui/router
|
|
|
|
markAsReadByKeyOnly: (key, callback)->
|
|
|
|
opts =
|
2016-08-12 04:59:25 -04:00
|
|
|
uri: "#{settings.apis.notifications?.url}/key/#{key}"
|
2016-08-11 09:04:11 -04:00
|
|
|
method: "DELETE"
|
|
|
|
timeout: oneSecond
|
|
|
|
logger.log {key:key}, "sending mark notification as read with key-only to notifications api"
|
|
|
|
makeRequest opts, callback
|