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

Merge pull request #19 from sharelatex/ja-track-changes

Ignore comment updates
James Allen 9 лет назад
Родитель
Сommit
d0d7756517

+ 3 - 0
services/track-changes/app/coffee/DiffGenerator.coffee

@@ -47,6 +47,9 @@ module.exports = DiffGenerator =
 
 		else if op.d?
 			return content.slice(0, op.p) + op.d + content.slice(op.p)
+		
+		else
+			return content
 
 	rewindUpdates: (content, updates) ->
 		for update in updates.reverse()

+ 4 - 2
services/track-changes/app/coffee/UpdateCompressor.coffee

@@ -24,7 +24,9 @@ module.exports = UpdateCompressor =
 	convertToSingleOpUpdates: (updates) ->
 		splitUpdates = []
 		for update in updates
-			if update.op.length == 0
+			# Reject any non-insert or delete ops, i.e. comments
+			ops = update.op.filter (o) -> o.i? or o.d?
+			if ops.length == 0
 				splitUpdates.push
 					op: UpdateCompressor.NOOP
 					meta:
@@ -33,7 +35,7 @@ module.exports = UpdateCompressor =
 						user_id:  update.meta.user_id
 					v: update.v
 			else
-				for op in update.op
+				for op in ops
 					splitUpdates.push
 						op: op
 						meta:

+ 22 - 0
services/track-changes/test/acceptance/coffee/AppendingUpdatesTests.coffee

@@ -233,6 +233,28 @@ describe "Appending doc ops to the history", ->
 			expect(@updates[0].pack[0].v).to.equal 3
 			expect(@updates[0].pack[1].v).to.equal 4
 
+	describe "when there is a comment update", ->
+		before (done) ->
+			@project_id = ObjectId().toString()
+			@doc_id = ObjectId().toString()
+			@user_id = ObjectId().toString()
+			MockWebApi.projects[@project_id] = features: versioning: false
+			TrackChangesClient.pushRawUpdates @project_id, @doc_id, [{
+				op: [{ c: "foo", p: 3 }, {d: "bar", p: 6}]
+				meta: { ts: Date.now(), user_id: @user_id }
+				v: 3
+			}], (error) =>
+				throw error if error?
+				TrackChangesClient.flushAndGetCompressedUpdates @project_id, @doc_id, (error, @updates) =>
+					throw error if error?
+					done()
+
+		it "should ignore the comment op", ->
+			expect(@updates[0].pack[0].op).to.deep.equal [{d: "bar", p: 6}]
+
+		it "should insert the correct version numbers into mongo", ->
+			expect(@updates[0].pack[0].v).to.equal 3
+
 	describe "when the project has versioning enabled", ->
 		before (done) ->
 			@project_id = ObjectId().toString()

+ 16 - 0
services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee

@@ -54,6 +54,22 @@ describe "UpdateCompressor", ->
 				v: 42
 			}]
 
+		it "should ignore comment ops", ->
+			expect(@UpdateCompressor.convertToSingleOpUpdates [{
+				op:   [ @op1 = { p: 0, i: "Foo" }, @op2 = { p: 9, c: "baz"}, @op3 = { p: 6, i: "bar"} ]
+				meta: { ts: @ts1, user_id: @user_id }
+				v: 42
+			}])
+			.to.deep.equal [{
+				op: @op1,
+				meta: { start_ts: @ts1, end_ts: @ts1, user_id: @user_id },
+				v: 42
+			}, {
+				op: @op3,
+				meta: { start_ts: @ts1, end_ts: @ts1, user_id: @user_id },
+				v: 42
+			}]
+
 	describe "concatUpdatesWithSameVersion", ->
 		it "should concat updates with the same version", ->
 			expect(@UpdateCompressor.concatUpdatesWithSameVersion [{