hooked up editor controller to connected users manager

This commit is contained in:
Henry Oswald 2014-07-10 17:01:19 +01:00
parent e1dd58aca8
commit 01ad313133
3 changed files with 30 additions and 0 deletions

View file

@ -15,6 +15,7 @@ buildUserKey = (project_id, user_id)-> return "connected_user:#{project_id}:#{us
module.exports =
markUserAsConnected: (project_id, user_id, callback = (err)->)->
logger.log project_id:project_id, user_id:user_id, "marking user as connected"
async.series [
(cb)->
rclient.sadd buildProjectSetKey(project_id), user_id, cb
@ -23,6 +24,7 @@ module.exports =
], callback
marksUserAsDisconnected: (project_id, user_id, callback)->
logger.log project_id:project_id, user_id:user_id, "marking user as disconnected"
async.series [
(cb)->
rclient.srem buildProjectSetKey(project_id), user_id, cb

View file

@ -17,6 +17,7 @@ settings = require('settings-sharelatex')
slReqIdHelper = require('soa-req-id')
tpdsPollingBackgroundTasks = require('../ThirdPartyDataStore/TpdsPollingBackgroundTasks')
async = require('async')
ConnectedUsersManager = require("../ConnectedUsers/ConnectedUsersManager")
_ = require('underscore')
rclientPub = require("redis").createClient(settings.redis.web.port, settings.redis.web.host)
rclientPub.auth(settings.redis.web.password)
@ -57,11 +58,17 @@ module.exports = EditorController =
AuthorizationManager.setPrivilegeLevelOnClient client, privilegeLevel
callback null, ProjectEditorHandler.buildProjectModelView(project), privilegeLevel, EditorController.protocolVersion
# can be done affter the connection has happened
EditorRealTimeController.emitToRoom(project_id, "ConnectedUsers.userConnected", user)
ConnectedUsersManager.markUserAsConnected project_id, user._id, ->
leaveProject: (client, user) ->
self = @
client.get "project_id", (error, project_id) ->
return if error? or !project_id?
EditorRealTimeController.emitToRoom(project_id, "clientTracking.clientDisconnected", client.id)
EditorRealTimeController.emitToRoom(project_id, "ConnectedUsers.userDissconected", user)
ConnectedUsersManager.marksUserAsDisconnected project_id, user._id, ->
logger.log user_id:user._id, project_id:project_id, "user leaving project"
self.flushProjectIfEmpty(project_id)

View file

@ -53,6 +53,9 @@ describe "EditorController", ->
addUserToProject: sinon.stub().callsArgWith(3)
@ProjectDeleter =
deleteProject: sinon.stub()
@ConnectedUsersManager =
marksUserAsDisconnected:sinon.stub()
markUserAsConnected:sinon.stub()
@EditorController = SandboxedModule.require modulePath, requires:
"../../infrastructure/Server" : io : @io
@ -73,6 +76,7 @@ describe "EditorController", ->
'./EditorRealTimeController':@EditorRealTimeController = {}
"../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
"../TrackChanges/TrackChangesManager": @TrackChangesManager = {}
"../ConnectedUsers/ConnectedUsersManager":@ConnectedUsersManager
'redis':createClient:-> auth:->
"logger-sharelatex": @logger =
log: sinon.stub()
@ -85,6 +89,8 @@ describe "EditorController", ->
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
@ProjectGetter.populateProjectWithUsers = sinon.stub().callsArgWith(1, null, @project)
@AuthorizationManager.setPrivilegeLevelOnClient = sinon.stub()
@EditorRealTimeController.emitToRoom = sinon.stub()
@ConnectedUsersManager.markUserAsConnected.callsArgWith(2)
describe "when authorized", ->
beforeEach ->
@ -116,6 +122,13 @@ describe "EditorController", ->
it "should return the project model view, privilege level and protocol version", ->
@callback.calledWith(null, @projectModelView, "owner", @EditorController.protocolVersion).should.equal true
it "should emit the to the room that the user has connected", ->
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "ConnectedUsers.userConnected", @user).should.equal true
it "should mark the user as connected with the ConnectedUsersManager", ->
@ConnectedUsersManager.markUserAsConnected.calledWith(@project_id, @user_id).should.equal true
describe "when not authorized", ->
beforeEach ->
@AuthorizationManager.getPrivilegeLevelForProject =
@ -143,6 +156,7 @@ describe "EditorController", ->
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.flushProjectIfEmpty = sinon.stub()
@EditorController.leaveProject @client, @user
@ConnectedUsersManager.marksUserAsDisconnected.callsArgWith(2)
it "should call the flush project if empty function", ->
@EditorController.flushProjectIfEmpty
@ -154,6 +168,13 @@ describe "EditorController", ->
.calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
.should.equal true
it "should emit the to the room that the user has connected", ->
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "ConnectedUsers.userDissconected", @user).should.equal true
it "should mark the user as connected with the ConnectedUsersManager", ->
@ConnectedUsersManager.marksUserAsDisconnected.calledWith(@project_id, @user_id).should.equal true
describe "joinDoc", ->
beforeEach ->
@client.join = sinon.stub()