Explorar o código

Mark Notification as read by key alone

Shane Kilkelly %!s(int64=10) %!d(string=hai) anos
pai
achega
826295167f

+ 3 - 1
services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee

@@ -3,6 +3,8 @@ NotificationsHandler = require("./NotificationsHandler")
 
 module.exports =
 
+	# Note: notification keys should be url-safe
+
 	groupPlan: (user, licence)->
 		key : "join-sub-#{licence.subscription_id}"
 		create: (callback = ->)->
@@ -26,4 +28,4 @@ module.exports =
 			logger.log {user_id: user._id, project_id: project._id, invite_id: invite._id, key: @key}, "creating project invite notification for user"
 			NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, callback
 		read:  (callback=()->) ->
-			NotificationsHandler.markAsReadWithKey user._id, @key, callback
+			NotificationsHandler.markAsReadByKeyOnly @key, callback

+ 10 - 0
services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee

@@ -61,3 +61,13 @@ module.exports =
 			timeout:oneSecond
 		logger.log user_id:user_id, notification_id:notification_id, "sending mark notification as read to notifications api"
 		makeRequest opts, callback
+
+	# removes notification by key, without regard for user_id,
+	# should not be exposed to user via ui/router
+	markAsReadByKeyOnly: (key, callback)->
+		opts =
+			uri: "#{settings.apis.notifications?.url}/notification/key/#{key}"
+			method: "DELETE"
+			timeout: oneSecond
+		logger.log {key:key}, "sending mark notification as read with key-only to notifications api"
+		makeRequest opts, callback

+ 14 - 1
services/web/test/UnitTests/coffee/Notifications/NotificationsHandlerTests.coffee

@@ -68,4 +68,17 @@ describe 'NotificationsHandler', ->
 				args.timeout.should.equal 1000
 				expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts}
 				assert.deepEqual(args.json, expectedJson)
-				done()
+				done()
+
+	describe "markAsReadByKeyOnly", ->
+		beforeEach ->
+			@key = "some key here"
+
+		it 'should send a delete request when a delete has been received to mark a notification', (done)->
+			@handler.markAsReadByKeyOnly @key, =>
+				opts =
+					uri: "#{notificationUrl}/notification/key/#{@key}"
+					timeout:1000
+					method: "DELETE"
+				@request.calledWith(opts).should.equal true
+				done()