overleaf/services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee

78 lines
2.3 KiB
CoffeeScript
Raw Normal View History

settings = require("settings-sharelatex")
request = require("request")
logger = require("logger-sharelatex")
oneSecond = 1000
makeRequest = (opts, callback)->
if !settings.apis.notifications?.url?
return callback(null, statusCode:200)
else
request(opts, callback)
2016-08-12 04:59:25 -04:00
module.exports =
getUserNotifications: (user_id, callback)->
2016-08-12 04:59:25 -04:00
opts =
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
json: true
timeout: oneSecond
method: "GET"
makeRequest 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, [])
else
if !unreadNotifications?
unreadNotifications = []
callback(null, unreadNotifications)
createNotification: (user_id, key, templateKey, messageOpts, expiryDateTime, callback)->
2016-08-12 09:40:59 -04:00
payload = {
key:key
messageOpts:messageOpts
templateKey:templateKey
forceCreate: true
2016-08-12 09:40:59 -04:00
}
if expiryDateTime?
payload.expires = expiryDateTime
2016-08-12 04:59:25 -04:00
opts =
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
timeout: oneSecond
method:"POST"
2016-08-12 09:40:59 -04:00
json: payload
logger.log opts:opts, "creating notification for user"
makeRequest opts, callback
markAsReadWithKey: (user_id, key, callback)->
2016-08-12 04:59:25 -04:00
opts =
uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
method: "DELETE"
timeout: oneSecond
json: {
key:key
}
logger.log user_id:user_id, key:key, "sending mark notification as read with key to notifications api"
makeRequest opts, callback
2016-08-12 04:59:25 -04:00
markAsRead: (user_id, notification_id, callback)->
opts =
method: "DELETE"
uri: "#{settings.apis.notifications?.url}/user/#{user_id}/notification/#{notification_id}"
timeout:oneSecond
logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
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