DiffManager.coffee 1.5 KB

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