2014-03-05 10:59:40 -05:00
|
|
|
UpdatesManager = require "./UpdatesManager"
|
2014-03-04 09:05:17 -05:00
|
|
|
DocumentUpdaterManager = require "./DocumentUpdaterManager"
|
|
|
|
DiffGenerator = require "./DiffGenerator"
|
2014-03-04 10:27:03 -05:00
|
|
|
logger = require "logger-sharelatex"
|
2014-03-04 09:05:17 -05:00
|
|
|
|
2014-03-04 08:02:48 -05:00
|
|
|
module.exports = DiffManager =
|
2014-03-06 05:45:51 -05:00
|
|
|
getLatestDocAndUpdates: (project_id, doc_id, fromVersion, toVersion, callback = (error, lines, version, updates) ->) ->
|
|
|
|
UpdatesManager.getUpdates doc_id, from: fromVersion, to: toVersion, (error, updates) ->
|
2014-03-04 09:05:17 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
DocumentUpdaterManager.getDocument project_id, doc_id, (error, lines, version) ->
|
|
|
|
return callback(error) if error?
|
2014-03-05 10:59:40 -05:00
|
|
|
callback(null, lines, version, updates)
|
2014-03-04 09:05:17 -05:00
|
|
|
|
2014-03-06 05:45:51 -05:00
|
|
|
getDiff: (project_id, doc_id, fromVersion, toVersion, callback = (error, diff) ->) ->
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, from: fromVersion, to: toVersion, "getting diff"
|
|
|
|
DiffManager.getLatestDocAndUpdates project_id, doc_id, fromVersion, null, (error, lines, version, updates) ->
|
2014-03-04 09:05:17 -05:00
|
|
|
return callback(error) if error?
|
2014-03-04 10:27:03 -05:00
|
|
|
|
|
|
|
logger.log lines: lines, version: version, updates: updates, "got doc and updates"
|
|
|
|
|
|
|
|
lastUpdate = updates[0]
|
2014-03-05 11:31:38 -05:00
|
|
|
if lastUpdate? and lastUpdate.v != version - 1
|
2014-03-04 09:05:17 -05:00
|
|
|
return callback new Error("latest update version, #{lastUpdate.v}, does not match doc version, #{version}")
|
|
|
|
|
|
|
|
updatesToApply = []
|
2014-03-04 10:27:03 -05:00
|
|
|
for update in updates.reverse()
|
2014-03-06 05:45:51 -05:00
|
|
|
if update.v <= toVersion
|
2014-03-04 09:05:17 -05:00
|
|
|
updatesToApply.push update
|
|
|
|
|
2014-03-04 10:27:03 -05:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, updatesToApply: updatesToApply, "got updates to apply"
|
|
|
|
|
2014-03-04 09:05:17 -05:00
|
|
|
try
|
|
|
|
startingContent = DiffGenerator.rewindUpdates lines.join("\n"), updates
|
2014-03-04 10:27:03 -05:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, startingContent: startingContent, "rewound doc"
|
2014-03-04 09:05:17 -05:00
|
|
|
diff = DiffGenerator.buildDiff startingContent, updatesToApply
|
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
|
|
|
|
callback(null, diff)
|