From 2a7c33d7ca767ba0d0f746a2e823cf4e83b42643 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 9 Dec 2015 14:57:04 +0000 Subject: [PATCH] added /check endpoint for documents --- services/track-changes/app.coffee | 2 ++ .../track-changes/app/coffee/DiffGenerator.coffee | 6 ++++-- .../app/coffee/HttpController.coffee | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/services/track-changes/app.coffee b/services/track-changes/app.coffee index 7d186ffe85..93d6b7fcd8 100644 --- a/services/track-changes/app.coffee +++ b/services/track-changes/app.coffee @@ -22,6 +22,8 @@ app.post "/project/:project_id/doc/:doc_id/flush", HttpController.flushDoc app.get "/project/:project_id/doc/:doc_id/diff", HttpController.getDiff +app.get "/project/:project_id/doc/:doc_id/check", HttpController.checkDoc + app.get "/project/:project_id/updates", HttpController.getUpdates app.post "/project/:project_id/flush", HttpController.flushProject diff --git a/services/track-changes/app/coffee/DiffGenerator.coffee b/services/track-changes/app/coffee/DiffGenerator.coffee index e57033ab40..6e3b36643b 100644 --- a/services/track-changes/app/coffee/DiffGenerator.coffee +++ b/services/track-changes/app/coffee/DiffGenerator.coffee @@ -11,11 +11,13 @@ module.exports = DiffGenerator = ConsistencyError: ConsistencyError rewindUpdate: (content, update) -> - for op in update.op by -1 + for op, i in update.op by -1 try content = DiffGenerator.rewindOp content, op catch e - if e instanceof ConsistencyError + if e instanceof ConsistencyError and i = update.op.length - 1 + # catch known case where the last op in an array has been + # merged into a later op logger.error {update, op: JSON.stringify(op)}, "marking op as broken" op.broken = true else diff --git a/services/track-changes/app/coffee/HttpController.coffee b/services/track-changes/app/coffee/HttpController.coffee index ecc62e1e3d..1a90c16fd4 100644 --- a/services/track-changes/app/coffee/HttpController.coffee +++ b/services/track-changes/app/coffee/HttpController.coffee @@ -30,6 +30,21 @@ module.exports = HttpController = return next(error) if error? res.send 204 + checkDoc: (req, res, next = (error) ->) -> + doc_id = req.params.doc_id + project_id = req.params.project_id + logger.log project_id: project_id, doc_id: doc_id, "checking doc history" + DiffManager.getDocumentBeforeVersion project_id, doc_id, 1, (error, document, rewoundUpdates) -> + return next(error) if error? + broken = [] + for update in rewoundUpdates + for op in update.op when op.broken is true + broken.push op + if broken.length > 0 + res.send broken + else + res.send 204 + getDiff: (req, res, next = (error) ->) -> doc_id = req.params.doc_id project_id = req.params.project_id