DiffManager.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. UpdatesManager.getDocUpdatesWithUserInfo project_id, doc_id, from: fromVersion, to: toVersion, (error, updates) ->
  8. return callback(error) if error?
  9. DocumentUpdaterManager.getDocument project_id, doc_id, (error, content, version) ->
  10. return callback(error) if error?
  11. callback(null, content, version, updates)
  12. getDiff: (project_id, doc_id, fromVersion, toVersion, callback = (error, diff) ->) ->
  13. logger.log project_id: project_id, doc_id: doc_id, from: fromVersion, to: toVersion, "getting diff"
  14. DiffManager.getDocumentBeforeVersion project_id, doc_id, fromVersion, (error, startingContent, updates) ->
  15. if error?
  16. if error.message == "broken-history"
  17. return callback(null, "history unavailable")
  18. else
  19. return callback(error)
  20. updatesToApply = []
  21. for update in updates.slice().reverse()
  22. if update.v <= toVersion
  23. updatesToApply.push update
  24. try
  25. diff = DiffGenerator.buildDiff startingContent, updatesToApply
  26. catch e
  27. return callback(e)
  28. callback(null, diff)
  29. getDocumentBeforeVersion: (project_id, doc_id, version, callback = (error, document, rewoundUpdates) ->) ->
  30. logger.log project_id: project_id, doc_id: doc_id, version: version, "getting document before version"
  31. DiffManager.getLatestDocAndUpdates project_id, doc_id, version, null, (error, content, version, updates) ->
  32. return callback(error) if error?
  33. # bail out if we hit a broken update
  34. for u in updates when u.broken
  35. return callback new Error "broken-history"
  36. lastUpdate = updates[0]
  37. if lastUpdate? and lastUpdate.v != version - 1
  38. return callback new Error("latest update version, #{lastUpdate.v}, does not match doc version, #{version}")
  39. try
  40. startingContent = DiffGenerator.rewindUpdates content, updates.slice().reverse()
  41. catch e
  42. return callback(e)
  43. callback(null, startingContent, updates)