Browse Source

Add in methods for retrieving updates and doc version

James Allen 12 years ago
parent
commit
1d1dcdfa2f

+ 2 - 0
services/track-changes/app.coffee

@@ -2,6 +2,8 @@ Settings = require "settings-sharelatex"
 logger = require "logger-sharelatex"
 logger = require "logger-sharelatex"
 logger.initialize("track-changes")
 logger.initialize("track-changes")
 
 
+require("./app/js/MongoManager").ensureIndices()
+
 HttpController = require "./app/js/HttpController"
 HttpController = require "./app/js/HttpController"
 express = require "express"
 express = require "express"
 app = express()
 app = express()

+ 8 - 0
services/track-changes/app/coffee/DiffManager.coffee

@@ -0,0 +1,8 @@
+module.exports = DiffManager =
+	getDiff: (doc_id, fromDate, toDate, callback = (error, diff) ->) ->
+		# Flush diff
+		# Get doc content and version
+		# Get updates from Mongo
+		# Check version matches
+		# Build diff
+		# Return diff

+ 21 - 0
services/track-changes/app/coffee/DocumentUpdaterManager.coffee

@@ -0,0 +1,21 @@
+request = require "request"
+logger = require "logger-sharelatex"
+Settings = require "settings-sharelatex"
+
+module.exports = DocumentUpdaterManager =
+	getDocument: (project_id, doc_id, callback = (error, content, version) ->) ->
+		url = "#{Settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}"
+		logger.log project_id:project_id, doc_id: doc_id, "getting doc from document updater"
+		request.get url, (error, res, body)->
+			if error?
+				return callback(error)
+			if res.statusCode >= 200 and res.statusCode < 300
+				try
+					body = JSON.parse(body)
+				catch error
+					return callback(error)
+				callback null, body.lines, body.version
+			else
+				error = new Error("doc updater returned a non-success status code: #{res.statusCode}")
+				logger.error err: error, project_id:project_id, doc_id:doc_id, url: url, "error accessing doc updater"
+				callback error

+ 15 - 1
services/track-changes/app/coffee/MongoManager.coffee

@@ -37,4 +37,18 @@ module.exports = MongoManager =
 			op:     update.op
 			op:     update.op
 			meta:   update.meta
 			meta:   update.meta
 			v:      update.v
 			v:      update.v
-		}, callback
+		}, callback
+
+	getUpdatesBetweenDates:(doc_id, fromDate, toDate, callback = (error, updates) ->) ->
+		db.docHistory
+			.find({
+				doc_id: ObjectId(doc_id.toString())
+				"meta.start_ts" : { $gte: fromDate }
+				"meta.end_ts"   : { $lte: toDate }
+			})
+			.sort( "meta.end_ts": -1 )
+			.toArray callback
+
+	ensureIndices: (callback = (error) ->) ->
+		db.docHistory.ensureIndex { doc_id: 1, "meta.start_ts": 1, "meta.end_ts": 1 }, callback
+

+ 52 - 0
services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee

@@ -0,0 +1,52 @@
+sinon = require('sinon')
+chai = require('chai')
+should = chai.should()
+expect = chai.expect
+modulePath = "../../../../app/js/DocumentUpdaterManager.js"
+SandboxedModule = require('sandboxed-module')
+
+describe "DocumentUpdaterManager", ->
+	beforeEach ->
+		@DocumentUpdaterManager = SandboxedModule.require modulePath, requires:
+			"request": @request = {}
+			"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
+			'settings-sharelatex': @settings =
+				apis : documentupdater: url : "http://example.com"
+		@callback = sinon.stub()
+		@lines = ["one", "two", "three"]
+		@version = 42
+
+	describe "getDocument", ->
+		describe "successfully", ->
+			beforeEach ->
+				@body = JSON.stringify
+					lines: @lines
+					version: @version
+					ops: []
+				@request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
+				@DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
+
+			it 'should get the document from the document updater', ->
+				url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
+				@request.get.calledWith(url).should.equal true
+
+			it "should call the callback with the lines and version", ->
+				@callback.calledWith(null, @lines, @version, @ops).should.equal true
+
+		describe "when the document updater API returns an error", ->
+			beforeEach ->
+				@request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
+				@DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
+
+			it "should return an error to the callback", ->
+				@callback.calledWith(@error).should.equal true
+
+		describe "when the document updater returns a failure error code", ->
+			beforeEach ->
+				@request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
+				@DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
+
+			it "should return the callback with an error", ->
+				@callback
+					.calledWith(new Error("doc updater returned failure status code: 500"))
+					.should.equal true

+ 30 - 0
services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee

@@ -131,3 +131,33 @@ describe "MongoManager", ->
 
 
 		it "should call the callback", ->
 		it "should call the callback", ->
 			@callback.called.should.equal true
 			@callback.called.should.equal true
+
+	describe "getUpdatesBetweenDates", ->
+		beforeEach ->
+			@updates = ["mock-update"]
+			@db.docHistory = {}
+			@db.docHistory.find = sinon.stub().returns @db.docHistory
+			@db.docHistory.sort = sinon.stub().returns @db.docHistory
+			@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
+
+			@from = new Date(Date.now())
+			@to = new Date(Date.now() + 100000)
+
+			@MongoManager.getUpdatesBetweenDates @doc_id, @from, @to, @callback
+
+		it "should find the updates for the doc", ->
+			@db.docHistory.find
+				.calledWith({
+					doc_id: ObjectId(@doc_id)
+					"meta.start_ts": { $gte: @from }
+					"meta.end_ts": { $lte: @to }
+				})
+				.should.equal true
+
+		it "should sort in descending timestamp order", ->
+			@db.docHistory.sort
+				.calledWith("meta.end_ts": -1)
+				.should.equal true
+
+		it "should call the call back with the updates", ->
+			@callback.calledWith(null, @updates).should.equal true