From 4a6374efe8d56b5cd85ca32518f6a1433a14326b Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 18 Dec 2015 12:38:42 +0000 Subject: [PATCH] fix read order when retrieving diffs --- services/track-changes/app/coffee/DiffManager.coffee | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/services/track-changes/app/coffee/DiffManager.coffee b/services/track-changes/app/coffee/DiffManager.coffee index 91d41a909f..bb110b1824 100644 --- a/services/track-changes/app/coffee/DiffManager.coffee +++ b/services/track-changes/app/coffee/DiffManager.coffee @@ -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}")