mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
39 lines
No EOL
1.7 KiB
CoffeeScript
39 lines
No EOL
1.7 KiB
CoffeeScript
UpdatesManager = require "./UpdatesManager"
|
|
DocumentUpdaterManager = require "./DocumentUpdaterManager"
|
|
DiffGenerator = require "./DiffGenerator"
|
|
logger = require "logger-sharelatex"
|
|
|
|
module.exports = DiffManager =
|
|
getLatestDocAndUpdates: (project_id, doc_id, fromDate, toDate, callback = (error, lines, version, updates) ->) ->
|
|
UpdatesManager.getUpdates doc_id, from: fromDate, to: toDate, (error, updates) ->
|
|
return callback(error) if error?
|
|
DocumentUpdaterManager.getDocument project_id, doc_id, (error, lines, version) ->
|
|
return callback(error) if error?
|
|
callback(null, lines, version, updates)
|
|
|
|
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"
|
|
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
|
|
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
|
|
updatesToApply.push update
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, updatesToApply: updatesToApply, "got updates to apply"
|
|
|
|
try
|
|
startingContent = DiffGenerator.rewindUpdates lines.join("\n"), updates
|
|
logger.log project_id: project_id, doc_id: doc_id, startingContent: startingContent, "rewound doc"
|
|
diff = DiffGenerator.buildDiff startingContent, updatesToApply
|
|
catch e
|
|
return callback(e)
|
|
|
|
callback(null, diff) |