From 13aaf042d908d09b5b951daee4bc9574d28daee0 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Mon, 14 Jul 2014 12:41:06 +0100 Subject: [PATCH] connected user manager logic works correctly --- .../ConnectedUsers/ConnectedUsersManager.coffee | 16 +++++++++------- .../ConnectedUsersManagerTests.coffee | 5 +++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee b/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee index 5c90ffeaf1..c275eabcba 100644 --- a/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee +++ b/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee @@ -1,6 +1,7 @@ _ = require("underscore") async = require("async") Settings = require('settings-sharelatex') +logger = require("logger-sharelatex") redis = require('redis') rclient = redis.createClient(Settings.redis.web.port, Settings.redis.web.host) rclient.auth(Settings.redis.web.password) @@ -12,7 +13,7 @@ buildProjectSetKey = (project_id)-> return "users_in_project:#{project_id}" buildUserKey = (project_id, user_id)-> return "connected_user:#{project_id}:#{user_id}" -module.exports = +module.exports = markUserAsConnected: (project_id, user_id, callback = (err)->)-> logger.log project_id:project_id, user_id:user_id, "marking user as connected" @@ -20,8 +21,12 @@ module.exports = (cb)-> rclient.sadd buildProjectSetKey(project_id), user_id, cb (cb)-> - rclient.setex buildUserKey(project_id, user_id), new Date(), ONE_HOUR_IN_S * 6, cb - ], callback + ttl = ONE_HOUR_IN_S * 6 + rclient.setex buildUserKey(project_id, user_id), ttl, new Date(), cb + ], (err)-> + if err? + logger.err err:err, project_id:project_id, user_id:user_id, "problem marking user as connected" + callback(err) markUserAsDisconnected: (project_id, user_id, callback)-> logger.log project_id:project_id, user_id:user_id, "marking user as disconnected" @@ -32,8 +37,6 @@ module.exports = rclient.del buildUserKey(project_id, user_id), cb ], callback - - _getConnectedUser: (project_id, user_id, callback)-> rclient.get buildUserKey(project_id, user_id), (err, result)-> if !result? @@ -45,8 +48,7 @@ module.exports = getConnectedUsers: (project_id, callback)-> self = @ - rclient.get "connected_users_list:#{project_id}", (err, results)-> - + rclient.smembers buildProjectSetKey(project_id), (err, results)-> jobs = results.map (user_id)-> (cb)-> self._getConnectedUser(project_id, user_id, cb) diff --git a/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee b/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee index 8417d267e0..00b2457b0b 100644 --- a/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee @@ -23,6 +23,7 @@ describe "ConnectedUsersManager", -> get: sinon.stub() srem:sinon.stub() del:sinon.stub() + smembers:sinon.stub() tk.freeze(new Date()) @ConnectedUsersManager = SandboxedModule.require modulePath, requires: @@ -45,7 +46,7 @@ describe "ConnectedUsersManager", -> it "should set a key with the date and give it a ttl", (done)-> @ConnectedUsersManager.markUserAsConnected @project_id, @user_id, (err)=> console.log @rClient.setex.args[0], "connected_user:#{@project_id}:#{@user_id}" - @rClient.setex.calledWith("connected_user:#{@project_id}:#{@user_id}", new Date(), 60 * 60 * 6).should.equal true + @rClient.setex.calledWith("connected_user:#{@project_id}:#{@user_id}", 60 * 60 * 6, new Date()).should.equal true done() it "should push the user_id on to the project list", (done)-> @@ -92,7 +93,7 @@ describe "ConnectedUsersManager", -> beforeEach -> @users = ["1234", "5678", "9123"] - @rClient.get.callsArgWith(1, null, @users) + @rClient.smembers.callsArgWith(1, null, @users) @ConnectedUsersManager._getConnectedUser = sinon.stub() @ConnectedUsersManager._getConnectedUser.withArgs(@project_id, @users[0]).callsArgWith(2, null, {connected:true, user_id:@users[0]}) @ConnectedUsersManager._getConnectedUser.withArgs(@project_id, @users[1]).callsArgWith(2, null, {connected:false, user_id:@users[1]})