overleaf/services/track-changes/app/coffee/DiffManager.coffee

39 lines
1.7 KiB
CoffeeScript
Raw Normal View History

UpdatesManager = require "./UpdatesManager"
2014-03-04 09:05:17 -05:00
DocumentUpdaterManager = require "./DocumentUpdaterManager"
DiffGenerator = require "./DiffGenerator"
logger = require "logger-sharelatex"
2014-03-04 09:05:17 -05:00
module.exports = DiffManager =
2014-03-04 09:05:17 -05:00
getLatestDocAndUpdates: (project_id, doc_id, fromDate, toDate, callback = (error, lines, version, updates) ->) ->
UpdatesManager.getUpdates doc_id, from: fromDate, to: toDate, (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?
callback(null, lines, version, updates)
2014-03-04 09:05:17 -05:00
getDiff: (project_id, doc_id, fromDate, toDate, callback = (error, diff) ->) ->
logger.log project_id: project_id, doc_id: doc_id, from: fromDate, to: toDate, "getting diff"
2014-03-04 09:05:17 -05:00
DiffManager.getLatestDocAndUpdates project_id, doc_id, fromDate, null, (error, lines, version, updates) ->
return callback(error) if error?
logger.log lines: lines, version: version, updates: updates, "got doc and updates"
lastUpdate = updates[0]
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 = []
for update in updates.reverse()
if update.meta.start_ts <= toDate
2014-03-04 09:05:17 -05:00
updatesToApply.push update
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
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)