fix read order when retrieving diffs

This commit is contained in:
Brian Gough 2015-12-18 12:38:42 +00:00
parent 911883a566
commit 4a6374efe8

View file

@ -5,9 +5,11 @@ logger = require "logger-sharelatex"
module.exports = DiffManager =
getLatestDocAndUpdates: (project_id, doc_id, fromVersion, toVersion, callback = (error, content, version, updates) ->) ->
UpdatesManager.getDocUpdatesWithUserInfo project_id, doc_id, from: fromVersion, to: toVersion, (error, updates) ->
# retrieve the document before retreiving the updates,
# because updates are written to mongo after the document
DocumentUpdaterManager.getDocument project_id, doc_id, (error, content, version) ->
return callback(error) if error?
DocumentUpdaterManager.getDocument project_id, doc_id, (error, content, version) ->
UpdatesManager.getDocUpdatesWithUserInfo project_id, doc_id, from: fromVersion, to: toVersion, (error, updates) ->
return callback(error) if error?
callback(null, content, version, updates)
@ -41,6 +43,10 @@ module.exports = DiffManager =
for u in updates when u.broken
return callback new Error "broken-history"
# discard any updates which are ahead of this document version
while updates[0]?.v >= version
updates.shift()
lastUpdate = updates[0]
if lastUpdate? and lastUpdate.v != version - 1
return callback new Error("latest update version, #{lastUpdate.v}, does not match doc version, #{version}")