Jelajahi Sumber

Change how notification expiry works

Set the `expires` field to be a date in the future, after which the
document should be removed.
Shane Kilkelly 10 tahun lalu
induk
melakukan
88f5f29982

+ 2 - 2
services/notifications/README.md

@@ -7,8 +7,8 @@ An API for managing user notifications in ShareLaTeX
 database indexes
 ================
 
-For notification expiry to work, a ttl index on `notifications.expiresFrom` must be created:
+For notification expiry to work, a TTL index on `notifications.expires` must be created:
 
 ```javascript
-db.notifications.createIndex({expiresFrom: 1}, {expireAfterSeconds: (60*60*24*30)})
+db.notifications.createIndex({expires: 1}, {expireAfterSeconds: 10})
 ```

+ 5 - 5
services/notifications/app/coffee/Notifications.coffee

@@ -27,12 +27,12 @@ module.exports =
 					key: notification.key
 					messageOpts: notification.messageOpts
 					templateKey: notification.templateKey
-				# ttl index on `expiresFrom` field
-				# in Mongo, TTL indexes only work on date fields,
-				# and ignore the document when that field is missing
+				# TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
+				# a datetime in the future when the document should be automatically removed.
+				# in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
+				# see `README.md` for instruction on creating TTL index
 				if notification.expires?
-					doc.expires = notification.expires
-					doc.expiresFrom = new Date()
+					doc.expires =  new Date(notification.expires)
 				db.notifications.insert(doc, callback)
 
 	removeNotificationId: (user_id, notification_id, callback)->

+ 5 - 7
services/notifications/test/unit/coffee/NotificationsTests.coffee

@@ -55,7 +55,7 @@ describe 'Notifications Tests', ->
 				user_id: @stubbedNotification.user_id,
 				key:"notification-key",
 				messageOpts:"some info",
-				templateKey:"template-key",
+				templateKey:"template-key"
 			}
 
 		it 'should insert the notification into the collection', (done)->
@@ -81,25 +81,23 @@ describe 'Notifications Tests', ->
 					key:"notification-key",
 					messageOpts:"some info",
 					templateKey:"template-key",
-					expires: true
+					expires: '2922-02-13T09:32:56.289Z'
 				}
 				@expectedDocument = {
 					user_id: @stubbedNotification.user_id,
 					key:"notification-key",
 					messageOpts:"some info",
 					templateKey:"template-key",
-					expires: true,
-					expiresFrom: new Date()
+					expires: new Date(@stubbedNotification.expires),
 				}
 
-			it 'should add an `expiresFrom` Date field to the inserted notification', (done)->
+			it 'should add an `expires` Date field to the document', (done)->
 				@insertStub.callsArgWith(1, null)
 				@countStub.callsArgWith(1, null, 0)
 
 				@notifications.addNotification user_id, @stubbedNotification, (err)=>
 					@insertStub.callCount.should.equal 1
-					Object.keys(@insertStub.lastCall.args[0]).should.deep.equal Object.keys(@expectedDocument)
-					@insertStub.firstCall.args[0].expiresFrom.should.be.instanceof Date
+					@insertStub.calledWith(@expectedDocument).should.equal true
 					done()
 
 	describe 'removeNotificationId', ->