Bladeren bron

don't call notifications if notifications has no url

Henry Oswald 10 jaren geleden
bovenliggende
commit
3887db8898

+ 15 - 4
services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee

@@ -3,6 +3,13 @@ request = require("request")
 logger = require("logger-sharelatex")
 logger = require("logger-sharelatex")
 
 
 oneSecond = 1000
 oneSecond = 1000
+
+makeRequest = (opts, callback)->
+	if !settings.apis.notifications.url?
+		return callback()
+	else
+		request(opts, callback)
+
 module.exports = 
 module.exports = 
 
 
 	getUserNotifications: (user_id, callback)->
 	getUserNotifications: (user_id, callback)->
@@ -10,7 +17,8 @@ module.exports =
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}"
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}"
 			json: true
 			json: true
 			timeout: oneSecond
 			timeout: oneSecond
-		request.get opts, (err, res, unreadNotifications)->
+			method: "GET"
+		request opts, (err, res, unreadNotifications)->
 			statusCode =  if res? then res.statusCode else 500
 			statusCode =  if res? then res.statusCode else 500
 			if err? or statusCode != 200
 			if err? or statusCode != 200
 				e = new Error("something went wrong getting notifications, #{err}, #{statusCode}")
 				e = new Error("something went wrong getting notifications, #{err}, #{statusCode}")
@@ -25,28 +33,31 @@ module.exports =
 		opts = 
 		opts = 
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}"
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}"
 			timeout: oneSecond
 			timeout: oneSecond
+			method:"POST"
 			json: {
 			json: {
 				key:key
 				key:key
 				messageOpts:messageOpts
 				messageOpts:messageOpts
 				templateKey:templateKey
 				templateKey:templateKey
 			}
 			}
 		logger.log opts:opts, "creating notification for user"
 		logger.log opts:opts, "creating notification for user"
-		request.post opts, callback
+		request opts, callback
 
 
 	markAsReadWithKey: (user_id, key, callback)->
 	markAsReadWithKey: (user_id, key, callback)->
 		opts = 
 		opts = 
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}"
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}"
+			method: "DELETE"
 			timeout: oneSecond
 			timeout: oneSecond
 			json: {
 			json: {
 				key:key
 				key:key
 			}
 			}
 		logger.log user_id:user_id, key:key, "sending mark notification as read with key to notifications api"
 		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)->
 	markAsRead: (user_id, notification_id, callback)->
 		opts =
 		opts =
+			method: "DELETE"
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}/notification/#{notification_id}"
 			uri: "#{settings.apis.notifications.url}/user/#{user_id}/notification/#{notification_id}"
 			timeout:oneSecond
 			timeout:oneSecond
 		logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
 		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

+ 8 - 9
services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee

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