DiffManager.coffee 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. UpdatesManager = require "./UpdatesManager"
  2. DocumentUpdaterManager = require "./DocumentUpdaterManager"
  3. DiffGenerator = require "./DiffGenerator"
  4. logger = require "logger-sharelatex"
  5. module.exports = DiffManager =
  6. getLatestDocAndUpdates: (project_id, doc_id, fromVersion, toVersion, callback = (error, content, version, updates) ->) ->
  7. # retrieve the document before retreiving the updates,
  8. # because updates are written to mongo after the document
  9. DocumentUpdaterManager.getDocument project_id, doc_id, (error, content, version) ->
  10. return callback(error) if error?
  11. UpdatesManager.getDocUpdatesWithUserInfo project_id, doc_id, from: fromVersion, to: toVersion, (error, updates) ->
  12. return callback(error) if error?
  13. callback(null, content, version, updates)
  14. getDiff: (project_id, doc_id, fromVersion, toVersion, callback = (error, diff) ->) ->
  15. DiffManager.getDocumentBeforeVersion project_id, doc_id, fromVersion, (error, startingContent, updates) ->
  16. if error?
  17. if error.message == "broken-history"
  18. return callback(null, "history unavailable")
  19. else
  20. return callback(error)
  21. updatesToApply = []
  22. for update in updates.slice().reverse()
  23. if update.v <= toVersion
  24. updatesToApply.push update
  25. try
  26. diff = DiffGenerator.buildDiff startingContent, updatesToApply
  27. catch e
  28. return callback(e)
  29. callback(null, diff)
  30. getDocumentBeforeVersion: (project_id, doc_id, version, callback = (error, document, rewoundUpdates) ->) ->
  31. logger.log project_id: project_id, doc_id: doc_id, version: version, "getting document before version"
  32. DiffManager.getLatestDocAndUpdates project_id, doc_id, version, null, (error, content, version, updates) ->
  33. return callback(error) if error?
  34. # bail out if we hit a broken update
  35. for u in updates when u.broken
  36. return callback new Error "broken-history"
  37. # discard any updates which are ahead of this document version
  38. while updates[0]?.v >= version
  39. updates.shift()
  40. lastUpdate = updates[0]
  41. if lastUpdate? and lastUpdate.v != version - 1
  42. return callback new Error("latest update version, #{lastUpdate.v}, does not match doc version, #{version}")
  43. tryUpdates = updates.slice().reverse()
  44. try
  45. startingContent = DiffGenerator.rewindUpdates content, tryUpdates
  46. # tryUpdates is reversed, and any unapplied ops are marked as broken
  47. catch e
  48. return callback(e)
  49. callback(null, startingContent, tryUpdates)