2014-03-05 15:59:40 +00:00
|
|
|
UpdatesManager = require "./UpdatesManager"
|
2014-03-04 14:05:17 +00:00
|
|
|
DocumentUpdaterManager = require "./DocumentUpdaterManager"
|
|
|
|
DiffGenerator = require "./DiffGenerator"
|
2014-03-04 15:27:03 +00:00
|
|
|
logger = require "logger-sharelatex"
|
2014-03-04 14:05:17 +00:00
|
|
|
|
2014-03-04 13:02:48 +00:00
|
|
|
module.exports = DiffManager =
|
2014-03-10 16:03:03 +00:00
|
|
|
getLatestDocAndUpdates: (project_id, doc_id, fromVersion, toVersion, callback = (error, content, version, updates) ->) ->
|
2014-03-19 17:44:16 +00:00
|
|
|
UpdatesManager.getDocUpdatesWithUserInfo project_id, doc_id, from: fromVersion, to: toVersion, (error, updates) ->
|
2014-03-04 14:05:17 +00:00
|
|
|
return callback(error) if error?
|
2014-03-10 16:03:03 +00:00
|
|
|
DocumentUpdaterManager.getDocument project_id, doc_id, (error, content, version) ->
|
2014-03-04 14:05:17 +00:00
|
|
|
return callback(error) if error?
|
2014-03-10 16:03:03 +00:00
|
|
|
callback(null, content, version, updates)
|
2014-03-04 14:05:17 +00:00
|
|
|
|
2014-03-06 10:45:51 +00: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"
|
2014-03-10 16:03:03 +00:00
|
|
|
DiffManager.getDocumentBeforeVersion project_id, doc_id, fromVersion, (error, startingContent, updates) ->
|
2014-03-04 14:05:17 +00:00
|
|
|
return callback(error) if error?
|
2014-03-04 15:27:03 +00:00
|
|
|
|
2014-03-04 14:05:17 +00:00
|
|
|
updatesToApply = []
|
2014-03-10 16:03:03 +00:00
|
|
|
for update in updates.slice().reverse()
|
2014-03-06 10:45:51 +00:00
|
|
|
if update.v <= toVersion
|
2014-03-04 14:05:17 +00:00
|
|
|
updatesToApply.push update
|
|
|
|
|
|
|
|
try
|
|
|
|
diff = DiffGenerator.buildDiff startingContent, updatesToApply
|
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
|
2014-03-10 16:03:03 +00:00
|
|
|
callback(null, diff)
|
|
|
|
|
|
|
|
getDocumentBeforeVersion: (project_id, doc_id, version, callback = (error, document, rewoundUpdates) ->) ->
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, version: version, "getting document before version"
|
|
|
|
DiffManager.getLatestDocAndUpdates project_id, doc_id, version, null, (error, content, version, updates) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
|
|
|
|
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}")
|
|
|
|
|
|
|
|
try
|
|
|
|
startingContent = DiffGenerator.rewindUpdates content, updates.slice().reverse()
|
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
|
|
|
|
callback(null, startingContent, updates)
|