Просмотр исходного кода

Rework how invite expiry functions.

Shane Kilkelly 10 лет назад
Родитель
Сommit
e53394919f

+ 2 - 2
services/web/app/coffee/Features/Notifications/NotificationsBuilder.coffee

@@ -12,7 +12,7 @@ module.exports =
 				groupName: licence.name
 				subscription_id: licence.subscription_id
 			logger.log user_id:user._id, key:key, "creating notification key for user"
-			NotificationsHandler.createNotification user._id, @key, "notification_group_invite", messageOpts, callback
+			NotificationsHandler.createNotification user._id, @key, "notification_group_invite", messageOpts, null, callback
 
 		read: (callback = ->)->
 			NotificationsHandler.markAsReadWithKey user._id, @key, callback
@@ -26,6 +26,6 @@ module.exports =
 				projectId: project._id.toString()
 				token: invite.token
 			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
+			NotificationsHandler.createNotification user._id, @key, "notification_project_invite", messageOpts, invite.expires, callback
 		read:  (callback=()->) ->
 			NotificationsHandler.markAsReadByKeyOnly @key, callback

+ 9 - 6
services/web/app/coffee/Features/Notifications/NotificationsHandler.coffee

@@ -29,16 +29,19 @@ module.exports =
 					unreadNotifications = []
 				callback(null, unreadNotifications)
 
-	createNotification: (user_id, key, templateKey, messageOpts, callback)->
+	createNotification: (user_id, key, templateKey, messageOpts, expiryDateTime, callback)->
+		payload = {
+				key:key
+				messageOpts:messageOpts
+				templateKey:templateKey
+		}
+		if expiryDateTime?
+			payload.expires = expiryDateTime
 		opts =
 			uri: "#{settings.apis.notifications?.url}/user/#{user_id}"
 			timeout: oneSecond
 			method:"POST"
-			json: {
-				key:key
-				messageOpts:messageOpts
-				templateKey:templateKey
-			}
+			json: payload
 		logger.log opts:opts, "creating notification for user"
 		makeRequest opts, callback
 

+ 10 - 3
services/web/app/coffee/models/ProjectInvite.coffee

@@ -6,7 +6,13 @@ Schema = mongoose.Schema
 ObjectId = Schema.ObjectId
 
 
-THIRTY_DAYS_IN_SECONDS = 60 * 60 * 24 * 30
+EXPIRY_IN_SECONDS = 60 * 60 * 24 * 30
+
+ExpiryDate = () ->
+	timestamp = new Date()
+	timestamp.setSeconds(timestamp.getSeconds() + EXPIRY_IN_SECONDS)
+	return timestamp
+
 
 
 ProjectInviteSchema = new Schema(
@@ -16,7 +22,8 @@ ProjectInviteSchema = new Schema(
 		sendingUserId:  ObjectId
 		projectId:      ObjectId
 		privileges:     String
-		createdAt:      {type: Date, default: Date.now, index: {expireAfterSeconds: THIRTY_DAYS_IN_SECONDS}}
+		createdAt:      {type: Date, default: Date.now}
+		expires:        {type: Date, default: ExpiryDate, index: {expireAfterSeconds: 10}}
 	},
 	{
 		collection: 'projectInvites'
@@ -29,7 +36,7 @@ conn = mongoose.createConnection(Settings.mongo.url, server: poolSize: Settings.
 
 ProjectInvite = conn.model('ProjectInvite', ProjectInviteSchema)
 
-
 mongoose.model 'ProjectInvite', ProjectInviteSchema
 exports.ProjectInvite = ProjectInvite
 exports.ProjectInviteSchema = ProjectInviteSchema
+exports.EXPIRY_IN_SECONDS = EXPIRY_IN_SECONDS

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

@@ -60,9 +60,10 @@ describe 'NotificationsHandler', ->
 			@key = "some key here"
 			@messageOpts = {value:12344}
 			@templateKey = "renderThisHtml"
+			@expiry = null
 
 		it "should post the message over", (done)->
-			@handler.createNotification user_id, @key, @templateKey, @messageOpts, =>
+			@handler.createNotification user_id, @key, @templateKey, @messageOpts, @expiry, =>
 				args = @request.args[0][0]
 				args.uri.should.equal "#{notificationUrl}/user/#{user_id}"
 				args.timeout.should.equal 1000
@@ -70,6 +71,22 @@ describe 'NotificationsHandler', ->
 				assert.deepEqual(args.json, expectedJson)
 				done()
 
+		describe 'when expiry date is supplied', ->
+			beforeEach ->
+				@key = "some key here"
+				@messageOpts = {value:12344}
+				@templateKey = "renderThisHtml"
+				@expiry = new Date()
+
+			it 'should post the message over with expiry field', (done) ->
+				@handler.createNotification user_id, @key, @templateKey, @messageOpts, @expiry, =>
+					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, expires: @expiry}
+					assert.deepEqual(args.json, expectedJson)
+					done()
+
 	describe "markAsReadByKeyOnly", ->
 		beforeEach ->
 			@key = "some key here"