Add in leaveDoc end point

This commit is contained in:
James Allen 2014-11-12 16:51:48 +00:00
parent eb8ccc0298
commit 8b923d2fda
3 changed files with 39 additions and 5 deletions

View file

@ -62,4 +62,13 @@ module.exports = Router =
return callback {message: "Something went wrong"} return callback {message: "Something went wrong"}
else else
callback(null, args...) callback(null, args...)
client.on "leaveDoc", (doc_id, callback) ->
WebsocketController.leaveDoc client, doc_id, (err, args...) ->
if err?
Router._getClientData client, (_, client) ->
logger.error {err, client, doc_id}, "server side error in leaveDoc"
# Don't return raw error to prevent leaking server side info
return callback {message: "Something went wrong"}
else
callback(null, args...)

View file

@ -34,9 +34,8 @@ module.exports = WebsocketController =
callback null, project, privilegeLevel, WebsocketController.PROTOCOL_VERSION callback null, project, privilegeLevel, WebsocketController.PROTOCOL_VERSION
joinDoc: (client, doc_id, fromVersion = -1, callback = (error, doclines, version, ops) ->) -> joinDoc: (client, doc_id, fromVersion = -1, callback = (error, doclines, version, ops) ->) ->
client.get "user_id", (error, user_id) -> WebsocketController._getClientData client, (error, {client_id, user_id, project_id}) ->
client.get "project_id", (error, project_id) -> logger.log {user_id, project_id, doc_id, fromVersion, client_id}, "client joining doc"
logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joining doc"
AuthorizationManager.assertClientCanViewProject client, (error) -> AuthorizationManager.assertClientCanViewProject client, (error) ->
return callback(error) if error? return callback(error) if error?
@ -57,4 +56,16 @@ module.exports = WebsocketController =
escapedLines.push line escapedLines.push line
client.join(doc_id) client.join(doc_id)
callback null, escapedLines, version, ops callback null, escapedLines, version, ops
leaveDoc: (client, doc_id, callback = (error) ->) ->
WebsocketController._getClientData client, (error, {client_id, user_id, project_id}) ->
logger.log {user_id, project_id, doc_id, client_id}, "client leaving doc"
client.leave doc_id
callback()
# Only used in logging.
_getClientData: (client, callback = (error, data) ->) ->
client.get "user_id", (error, user_id) ->
client.get "project_id", (error, project_id) ->
callback null, {client_id: client.id, project_id, user_id}

View file

@ -23,6 +23,7 @@ describe 'WebsocketController', ->
set: sinon.stub() set: sinon.stub()
get: (param, cb) -> cb null, @params[param] get: (param, cb) -> cb null, @params[param]
join: sinon.stub() join: sinon.stub()
leave: sinon.stub()
@WebsocketController = SandboxedModule.require modulePath, requires: @WebsocketController = SandboxedModule.require modulePath, requires:
"./WebApiManager": @WebApiManager = {} "./WebApiManager": @WebApiManager = {}
"./AuthorizationManager": @AuthorizationManager = {} "./AuthorizationManager": @AuthorizationManager = {}
@ -153,4 +154,17 @@ describe 'WebsocketController', ->
@callback.calledWith(@err).should.equal true @callback.calledWith(@err).should.equal true
it "should not call the DocumentUpdaterManager", -> it "should not call the DocumentUpdaterManager", ->
@DocumentUpdaterManager.getDocument.called.should.equal false @DocumentUpdaterManager.getDocument.called.should.equal false
describe "leaveDoc", ->
beforeEach ->
@doc_id = "doc-id-123"
@client.params.project_id = @project_id
@WebsocketController.leaveDoc @client, @doc_id, @callback
it "should remove the client from the doc_id room", ->
@client.leave
.calledWith(@doc_id).should.equal true
it "should call the callback", ->
@callback.called.should.equal true