don't call notifications if notifications has no url

This commit is contained in:
Henry Oswald 2016-06-24 14:06:04 +01:00
parent 90b605061b
commit 3887db8898
2 changed files with 23 additions and 13 deletions

View file

@ -3,6 +3,13 @@ request = require("request")
logger = require("logger-sharelatex")
oneSecond = 1000
makeRequest = (opts, callback)->
if !settings.apis.notifications.url?
return callback()
else
request(opts, callback)
module.exports =
getUserNotifications: (user_id, callback)->
@ -10,7 +17,8 @@ module.exports =
uri: "#{settings.apis.notifications.url}/user/#{user_id}"
json: true
timeout: oneSecond
request.get opts, (err, res, unreadNotifications)->
method: "GET"
request 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}")
@ -25,28 +33,31 @@ module.exports =
opts =
uri: "#{settings.apis.notifications.url}/user/#{user_id}"
timeout: oneSecond
method:"POST"
json: {
key:key
messageOpts:messageOpts
templateKey:templateKey
}
logger.log opts:opts, "creating notification for user"
request.post opts, callback
request opts, callback
markAsReadWithKey: (user_id, key, callback)->
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"
request.del opts, callback
request opts, callback
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"
request.del opts, callback
request opts, callback

View file

@ -12,10 +12,7 @@ describe 'NotificationsHandler', ->
notificationUrl = "notification.sharelatex.testing"
beforeEach ->
@request =
post: sinon.stub().callsArgWith(1)
del: sinon.stub().callsArgWith(1)
get: sinon.stub()
@request = sinon.stub().callsArgWith(1)
@handler = SandboxedModule.require modulePath, requires:
"settings-sharelatex": apis:{notifications:{url:notificationUrl}}
"request":@request
@ -26,18 +23,19 @@ describe 'NotificationsHandler', ->
describe "getUserNotifications", ->
it 'should get unread notifications', (done)->
stubbedNotifications = [{_id: notification_id, user_id: user_id}]
@request.get.callsArgWith(1, null, {statusCode:200}, stubbedNotifications)
@request.callsArgWith(1, null, {statusCode:200}, stubbedNotifications)
@handler.getUserNotifications user_id, (err, unreadNotifications)=>
stubbedNotifications.should.deep.equal unreadNotifications
getOpts =
uri: "#{notificationUrl}/user/#{user_id}"
json:true
timeout:1000
@request.get.calledWith(getOpts).should.equal true
method: "GET"
@request.calledWith(getOpts).should.equal true
done()
it 'should return empty arrays if there are no notifications', ->
@request.get.callsArgWith(1, null, {statusCode:200}, null)
@request.callsArgWith(1, null, {statusCode:200}, null)
@handler.getUserNotifications user_id, (err, unreadNotifications)=>
unreadNotifications.length.should.equal 0
@ -52,7 +50,8 @@ describe 'NotificationsHandler', ->
json:
key:@key
timeout:1000
@request.del.calledWith(opts).should.equal true
method: "DELETE"
@request.calledWith(opts).should.equal true
done()
@ -64,7 +63,7 @@ describe 'NotificationsHandler', ->
it "should post the message over", (done)->
@handler.createNotification user_id, @key, @templateKey, @messageOpts, =>
args = @request.post.args[0][0]
args = @request.args[0][0]
args.uri.should.equal "#{notificationUrl}/user/#{user_id}"
args.timeout.should.equal 1000
expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts}