Explorar o código

fix update-in-place bug for array ops

Brian Gough %!s(int64=11) %!d(string=hai) anos
pai
achega
be2136de7c

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

@@ -56,6 +56,10 @@ module.exports = UpdateCompressor =
 		return concattedUpdates
 
 	compressRawUpdates: (lastPreviousUpdate, rawUpdates) ->
+		if lastPreviousUpdate?.op?.length > 1
+			# if the last previous update was an array op, don't compress onto it.
+			# The avoids cases where array length changes but version number doesn't
+			return [lastPreviousUpdate].concat UpdateCompressor.compressRawUpdates(null,rawUpdates)
 		if lastPreviousUpdate?
 			rawUpdates = [lastPreviousUpdate].concat(rawUpdates)
 		updates = UpdateCompressor.convertToSingleOpUpdates(rawUpdates)

+ 2 - 1
services/track-changes/app/coffee/UpdatesManager.coffee

@@ -7,6 +7,7 @@ UpdateTrimmer = require "./UpdateTrimmer"
 logger = require "logger-sharelatex"
 async = require "async"
 DocArchiveManager = require "./DocArchiveManager"
+_ = require "underscore"
 
 module.exports = UpdatesManager =
 	compressAndSaveRawUpdates: (project_id, doc_id, rawUpdates, temporary, callback = (error) ->) ->
@@ -50,7 +51,7 @@ module.exports = UpdatesManager =
 				# compress them together with the new ones
 				[firstUpdate, additionalUpdates...] = compressedUpdates
 
-				if firstUpdate.v == lastCompressedUpdate.v
+				if firstUpdate.v == lastCompressedUpdate.v and _.isEqual(firstUpdate, lastCompressedUpdate)
 					# first update version hasn't changed, skip it and insert remaining updates
 					# this is an optimisation, we could update the existing op with itself
 					updateToModify = null

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

@@ -304,3 +304,25 @@ describe "UpdateCompressor", ->
 					meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id
 					v: 43
 				}]
+
+	describe "compressRawUpdates", ->
+		describe "merging in-place with an array op", ->
+			it "should not change the existing last updates", ->
+				expect(@UpdateCompressor.compressRawUpdates {
+					op: [ {"p":1000,"d":"hello"}, {"p":1000,"i":"HELLO()"} ]
+					meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id
+					v: 42
+				}, [{
+					op: [{ p: 1006, i: "WORLD" }]
+					meta: ts: @ts2, user_id: @user_id
+					v: 43
+				}])
+				.to.deep.equal [{
+					op: [{"p":1000,"d":"hello"}, {"p":1000,"i":"HELLO()"} ]
+					meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id
+					v: 42
+				},{
+					op: [{"p":1006,"i":"WORLD"}]
+					meta: start_ts: @ts2, end_ts: @ts2, user_id: @user_id
+					v: 43
+				}]