DiffManager.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. HistoryManager = require "./HistoryManager"
  2. DocumentUpdaterManager = require "./DocumentUpdaterManager"
  3. MongoManager = require "./MongoManager"
  4. DiffGenerator = require "./DiffGenerator"
  5. logger = require "logger-sharelatex"
  6. module.exports = DiffManager =
  7. getLatestDocAndUpdates: (project_id, doc_id, fromDate, toDate, callback = (error, lines, version, updates) ->) ->
  8. HistoryManager.processUncompressedUpdatesWithLock doc_id, (error) ->
  9. return callback(error) if error?
  10. DocumentUpdaterManager.getDocument project_id, doc_id, (error, lines, version) ->
  11. return callback(error) if error?
  12. MongoManager.getUpdatesBetweenDates doc_id, fromDate, toDate, (error, updates) ->
  13. return callback(error) if error?
  14. callback(null, lines, version, updates)
  15. getDiff: (project_id, doc_id, fromDate, toDate, callback = (error, diff) ->) ->
  16. logger.log project_id: project_id, doc_id: doc_id, from: fromDate, to: toDate, "getting diff"
  17. DiffManager.getLatestDocAndUpdates project_id, doc_id, fromDate, null, (error, lines, version, updates) ->
  18. return callback(error) if error?
  19. logger.log lines: lines, version: version, updates: updates, "got doc and updates"
  20. lastUpdate = updates[0]
  21. if lastUpdate? and lastUpdate.v != version
  22. return callback new Error("latest update version, #{lastUpdate.v}, does not match doc version, #{version}")
  23. updatesToApply = []
  24. for update in updates.reverse()
  25. if update.meta.start_ts <= toDate
  26. updatesToApply.push update
  27. logger.log project_id: project_id, doc_id: doc_id, updatesToApply: updatesToApply, "got updates to apply"
  28. try
  29. startingContent = DiffGenerator.rewindUpdates lines.join("\n"), updates
  30. logger.log project_id: project_id, doc_id: doc_id, startingContent: startingContent, "rewound doc"
  31. diff = DiffGenerator.buildDiff startingContent, updatesToApply
  32. catch e
  33. return callback(e)
  34. callback(null, diff)