Allow source and user_id to be included when setting a document

This commit is contained in:
James Allen 2014-03-11 12:47:26 +00:00
parent 5d45e191f3
commit 4f878e000b
6 changed files with 41 additions and 38 deletions

View file

@ -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"

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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", ->