From 01ad3131330ad1b5960a00d0cc2e1fc06dff15af Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Thu, 10 Jul 2014 17:01:19 +0100 Subject: [PATCH] hooked up editor controller to connected users manager --- .../ConnectedUsersManager.coffee | 2 ++ .../Features/Editor/EditorController.coffee | 7 +++++++ .../Editor/EditorControllerTests.coffee | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee b/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee index c4fd3b5623..a3674bb63c 100644 --- a/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee +++ b/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee @@ -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 diff --git a/services/web/app/coffee/Features/Editor/EditorController.coffee b/services/web/app/coffee/Features/Editor/EditorController.coffee index 8c95af2862..1599da008a 100644 --- a/services/web/app/coffee/Features/Editor/EditorController.coffee +++ b/services/web/app/coffee/Features/Editor/EditorController.coffee @@ -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) diff --git a/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee b/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee index a5883f2720..928148bc42 100644 --- a/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee @@ -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()