diff --git a/services/document-updater/app/coffee/DocumentManager.coffee b/services/document-updater/app/coffee/DocumentManager.coffee index aa64ac3d7f..38f8a9dac4 100644 --- a/services/document-updater/app/coffee/DocumentManager.coffee +++ b/services/document-updater/app/coffee/DocumentManager.coffee @@ -42,7 +42,7 @@ module.exports = DocumentManager = return callback(error) if error? callback null, lines, version, ops - setDoc: (project_id, doc_id, newLines, _callback = (error) ->) -> + setDoc: (project_id, doc_id, newLines, source, user_id, _callback = (error) ->) -> timer = new Metrics.Timer("docManager.setDoc") callback = (args...) -> timer.done() @@ -68,6 +68,8 @@ module.exports = DocumentManager = v: version meta: type: "external" + source: source + user_id: user_id UpdateManager.applyUpdates project_id, doc_id, [update], (error) -> return callback(error) if error? DocumentManager.flushDocIfLoaded project_id, doc_id, (error) -> @@ -114,9 +116,9 @@ module.exports = DocumentManager = UpdateManager = require "./UpdateManager" UpdateManager.lockUpdatesAndDo DocumentManager.getDocAndRecentOps, project_id, doc_id, fromVersion, callback - setDocWithLock: (project_id, doc_id, lines, callback = (error) ->) -> + setDocWithLock: (project_id, doc_id, lines, source, user_id, callback = (error) ->) -> UpdateManager = require "./UpdateManager" - UpdateManager.lockUpdatesAndDo DocumentManager.setDoc, project_id, doc_id, lines, callback + UpdateManager.lockUpdatesAndDo DocumentManager.setDoc, project_id, doc_id, lines, source, user_id, callback flushDocIfLoadedWithLock: (project_id, doc_id, callback = (error) ->) -> UpdateManager = require "./UpdateManager" diff --git a/services/document-updater/app/coffee/HttpController.coffee b/services/document-updater/app/coffee/HttpController.coffee index 391d02ee37..ef9fb38e19 100644 --- a/services/document-updater/app/coffee/HttpController.coffee +++ b/services/document-updater/app/coffee/HttpController.coffee @@ -32,9 +32,11 @@ module.exports = HttpController = doc_id = req.params.doc_id project_id = req.params.project_id lines = req.body.lines - logger.log project_id: project_id, doc_id: doc_id, lines: lines, "setting doc via http" + source = req.body.source + user_id = req.body.user_id + logger.log project_id: project_id, doc_id: doc_id, lines: lines, source: source, user_id: user_id, "setting doc via http" timer = new Metrics.Timer("http.setDoc") - DocumentManager.setDocWithLock project_id, doc_id, lines, (error) -> + DocumentManager.setDocWithLock project_id, doc_id, lines, source, user_id, (error) -> timer.done() return next(error) if error? logger.log project_id: project_id, doc_id: doc_id, "set doc via http" diff --git a/services/document-updater/test/acceptance/coffee/SettingADocumentTests.coffee b/services/document-updater/test/acceptance/coffee/SettingADocumentTests.coffee index cc0f30834a..a02cb0250a 100644 --- a/services/document-updater/test/acceptance/coffee/SettingADocumentTests.coffee +++ b/services/document-updater/test/acceptance/coffee/SettingADocumentTests.coffee @@ -18,6 +18,8 @@ describe "Setting a document", -> v: 0 @result = ["one", "one and a half", "two", "three"] @newLines = ["these", "are", "the", "new", "lines"] + @source = "dropbox" + @user_id = "user-id-123" MockWebApi.insertDoc @project_id, @doc_id, { lines: @lines } @@ -30,7 +32,7 @@ describe "Setting a document", -> DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) => throw error if error? setTimeout () => - DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, (error, res, body) => + DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) => @statusCode = res.statusCode done() , 200 diff --git a/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee b/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee index 4ddef90d26..ec70023876 100644 --- a/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee +++ b/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee @@ -43,11 +43,13 @@ module.exports = DocUpdaterClient = request.post "http://localhost:3003/project/#{project_id}/doc/#{doc_id}/flush", (error, res, body) -> callback error, res, body - setDocLines: (project_id, doc_id, lines, callback = (error) ->) -> + setDocLines: (project_id, doc_id, lines, source, user_id, callback = (error) ->) -> request.post { url: "http://localhost:3003/project/#{project_id}/doc/#{doc_id}" json: lines: lines + source: source + user_id: user_id }, (error, res, body) -> callback error, res, body diff --git a/services/document-updater/test/unit/coffee/DocumentManager/setDocTests.coffee b/services/document-updater/test/unit/coffee/DocumentManager/setDocTests.coffee index d4b5e931b8..b827b584f8 100644 --- a/services/document-updater/test/unit/coffee/DocumentManager/setDocTests.coffee +++ b/services/document-updater/test/unit/coffee/DocumentManager/setDocTests.coffee @@ -22,6 +22,8 @@ describe "DocumentManager - setDoc", -> @version = 42 @ops = ["mock-ops"] @callback = sinon.stub() + @source = "dropbox" + @user_id = "mock-user-id" describe "with plain tex lines", -> beforeEach -> @@ -34,7 +36,7 @@ describe "DocumentManager - setDoc", -> @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops) @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null) @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2) - @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @callback + @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback it "should get the current doc lines", -> @DocumentManager.getDoc @@ -48,7 +50,20 @@ describe "DocumentManager - setDoc", -> it "should apply the diff as a ShareJS op", -> @UpdateManager.applyUpdates - .calledWith(@project_id, @doc_id, [doc: @doc_id, v: @version, op: @ops, meta: { type: "external" }]) + .calledWith( + @project_id, + @doc_id, + [ + doc: @doc_id, + v: @version, + op: @ops, + meta: { + type: "external" + source: @source + user_id: @user_id + } + ] + ) .should.equal true it "should flush the doc to Mongo", -> @@ -62,30 +77,6 @@ describe "DocumentManager - setDoc", -> it "should time the execution", -> @Metrics.Timer::done.called.should.equal true - describe "with json lines", -> - beforeEach -> - @beforeLines = [text: "before", text: "lines"] - @afterLines = ["after", "lines"] - - describe "successfully", -> - beforeEach -> - @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version) - @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops) - @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null) - @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2) - @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @callback - - it "should get the current doc lines", -> - @DocumentManager.getDoc - .calledWith(@project_id, @doc_id) - .should.equal true - - it "should return not try to get a diff", -> - @DiffCodec.diffAsShareJsOp.called.should.equal false - - it "should call the callback", -> - @callback.calledWith(null).should.equal true - describe "without new lines", -> beforeEach -> @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version) @@ -96,7 +87,7 @@ describe "DocumentManager - setDoc", -> it "should not try to get the doc lines", -> @DocumentManager.getDoc.called.should.equal false - + diff --git a/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee b/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee index 2c3924c030..dd2a7c1d59 100644 --- a/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee +++ b/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee @@ -19,6 +19,8 @@ describe "HttpController - setDoc", -> @project_id = "project-id-123" @doc_id = "doc-id-123" @lines = ["one", "two", "three"] + @source = "dropbox" + @user_id = "user-id-123" @res = send: sinon.stub() @req = @@ -27,16 +29,18 @@ describe "HttpController - setDoc", -> doc_id: @doc_id body: lines: @lines + source: @source + user_id: @user_id @next = sinon.stub() describe "successfully", -> beforeEach -> - @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(3) + @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5) @HttpController.setDoc(@req, @res, @next) it "should set the doc", -> @DocumentManager.setDocWithLock - .calledWith(@project_id, @doc_id) + .calledWith(@project_id, @doc_id, @lines, @source, @user_id) .should.equal true it "should return a successful No Content response", -> @@ -46,7 +50,7 @@ describe "HttpController - setDoc", -> it "should log the request", -> @logger.log - .calledWith(doc_id: @doc_id, project_id: @project_id, lines: @lines, "setting doc via http") + .calledWith(doc_id: @doc_id, project_id: @project_id, lines: @lines, source: @source, user_id: @user_id, "setting doc via http") .should.equal true it "should time the request", -> @@ -54,7 +58,7 @@ describe "HttpController - setDoc", -> describe "when an errors occurs", -> beforeEach -> - @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(3, new Error("oops")) + @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5, new Error("oops")) @HttpController.setDoc(@req, @res, @next) it "should call next with the error", ->