diff --git a/services/track-changes/.gitignore b/services/track-changes/.gitignore index 40d016e39d..c39cd12b4d 100644 --- a/services/track-changes/.gitignore +++ b/services/track-changes/.gitignore @@ -1,4 +1,6 @@ **.swp node_modules/ app/js +app.js test/unit/js +test/acceptance/js diff --git a/services/track-changes/app.coffee b/services/track-changes/app.coffee index 85aa7ced6b..b208df2368 100644 --- a/services/track-changes/app.coffee +++ b/services/track-changes/app.coffee @@ -1,20 +1,26 @@ +Settings = require "settings-sharelatex" express = require "express" app = express() -ConversoinManager = require "./app/js/ConversionManager" +HistoryManager = require "./app/js/HistoryManager" logger = require "logger-sharelatex" logger.initialize("history") -app.post express.bodyParser(), "/doc/:doc_id/flush", (req, res, next) -> - project_id = req.params.project_id - docOps = req.body.docOps - logger.log doc_id: doc_id, "compressing doc history" - ConversionManager.convertAndSaveRawOps doc_id, docOps, (error) -> +app.post "/doc/:doc_id/history", express.bodyParser(), (req, res, next) -> + doc_id = req.params.doc_id + docOps = req.body.docOps + version = req.body.version + logger.log doc_id: doc_id, version: version, "compressing doc history" + HistoryManager.compressAndSaveRawUpdates doc_id, docOps, (error) -> return next(error) if error? - res.send 204 # No content + res.send 204 app.use (error, req, res, next) -> logger.error err: error, "an internal error occured" req.send 500 -app.listen(3014) +app.listen (Settings.port ||= 3014), (error) -> + if error? + logger.error err: error, "could not start history server" + logger.log "history api listening on port 3014" + diff --git a/services/track-changes/app/coffee/HistoryManager.coffee b/services/track-changes/app/coffee/HistoryManager.coffee index bf7a4f6510..32b3ddf758 100644 --- a/services/track-changes/app/coffee/HistoryManager.coffee +++ b/services/track-changes/app/coffee/HistoryManager.coffee @@ -1,6 +1,7 @@ {db, ObjectId} = require "./mongojs" UpdateCompressor = require "./UpdateCompressor" logger = require "logger-sharelatex" +async = require "async" module.exports = HistoryManager = getLastCompressedUpdate: (doc_id, callback = (error, update) ->) -> @@ -25,15 +26,19 @@ module.exports = HistoryManager = else callback null, null - insertCompressedUpdates: (doc_id, docUpdates, callback = (error) ->) -> - db.docHistory.update { - doc_id: ObjectId(doc_id) - }, { - $push: - docUpdates: - $each: docUpdates - }, { - upsert: true + insertCompressedUpdates: (doc_id, updates, callback = (error) ->) -> + jobs = [] + for update in updates + do (update) -> + jobs.push (callback) -> HistoryManager.insertCompressedUpdate doc_id, update, callback + async.series jobs, callback + + insertCompressedUpdate: (doc_id, update, callback = (error) ->) -> + logger.log doc_id: doc_id, update: update, "inserting compressed update" + db.docHistory.insert { + doc_id: ObjectId(doc_id.toString()) + op: update.op + meta: update.meta }, callback compressAndSaveRawUpdates: (doc_id, rawUpdates, callback = (error) ->) -> @@ -41,12 +46,9 @@ module.exports = HistoryManager = if length == 0 return callback() - HistoryManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) -> return callback(error) if error? - compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates - HistoryManager.insertCompressedUpdates doc_id, compressedUpdates, (error) -> return callback(error) if error? logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates" diff --git a/services/track-changes/config/settings.testing.coffee b/services/track-changes/config/settings.testing.coffee index 528c6a7114..0e4e181dfd 100755 --- a/services/track-changes/config/settings.testing.coffee +++ b/services/track-changes/config/settings.testing.coffee @@ -1,3 +1,4 @@ module.exports = mongo: url: 'mongodb://127.0.0.1/sharelatexTesting' + port: 3014 diff --git a/services/track-changes/package.json b/services/track-changes/package.json index e1a8dba4a6..9f1cf78669 100644 --- a/services/track-changes/package.json +++ b/services/track-changes/package.json @@ -1,14 +1,15 @@ { - "name": "history-sharelatex", - "version": "0.0.1", - "dependencies": { - "async": "", - "chai": "", - "express": "3.3.5", - "sandboxed-module": "", - "sinon": "", - "mongojs": "0.7.2", - "settings": "git+ssh://git@bitbucket.org:sharelatex/settings-sharelatex.git#master", - "logger": "git+ssh://git@bitbucket.org:sharelatex/logger-sharelatex.git#bunyan" - } + "name": "history-sharelatex", + "version": "0.0.1", + "dependencies": { + "async": "", + "chai": "", + "express": "3.3.5", + "sandboxed-module": "", + "sinon": "", + "mongojs": "~0.9.11", + "settings": "git+ssh://git@bitbucket.org:sharelatex/settings-sharelatex.git#master", + "logger": "git+ssh://git@bitbucket.org:sharelatex/logger-sharelatex.git#bunyan", + "request": "~2.33.0" + } } diff --git a/services/track-changes/rakefile.rb b/services/track-changes/rakefile.rb index 6baa462558..a3b3cd6ecd 100644 --- a/services/track-changes/rakefile.rb +++ b/services/track-changes/rakefile.rb @@ -38,6 +38,12 @@ namespace 'compile' do end puts 'finished app compile' end + sh %{coffee -c app.coffee} do |ok, res| + if ! ok + raise "error compiling root app file: #{res}" + end + puts 'finished root app file compile' + end end desc "compiles unit tests" diff --git a/services/track-changes/test/acceptance/coffee/AppendingUpdatesTests.coffee b/services/track-changes/test/acceptance/coffee/AppendingUpdatesTests.coffee new file mode 100644 index 0000000000..c901917ac1 --- /dev/null +++ b/services/track-changes/test/acceptance/coffee/AppendingUpdatesTests.coffee @@ -0,0 +1,38 @@ +sinon = require "sinon" +chai = require("chai") +chai.should() +mongojs = require "../../../app/js/mongojs" +db = mongojs.db +ObjectId = mongojs.ObjectId +Settings = require "settings-sharelatex" +request = require "request" + +describe "Appending doc ops to the history", -> + describe "when the history does not exist yet", -> + before (done) -> + @doc_id = ObjectId().toString() + @user_id = ObjectId().toString() + @updates = [{ + op: [{ i: "f", p: 3 }] + meta: { ts: Date.now(), user_id: @user_id } + }, { + op: [{ i: "o", p: 4 }] + meta: { ts: Date.now(), user_id: @user_id } + }, { + op: [{ i: "o", p: 5 }] + meta: { ts: Date.now(), user_id: @user_id } + }] + @version = 3 + + request.post { + url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history" + json: + version: @version + docOps: @updates + }, (@error, @response, @body) => + done() + + it "should return a successful response", -> + @response.statusCode.should.equal 204 + +