2014-07-04 11:18:46 -04:00
|
|
|
_ = require("underscore")
|
|
|
|
async = require("async")
|
|
|
|
Settings = require('settings-sharelatex')
|
2014-07-14 07:41:06 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2014-07-04 11:18:46 -04:00
|
|
|
redis = require('redis')
|
|
|
|
rclient = redis.createClient(Settings.redis.web.port, Settings.redis.web.host)
|
|
|
|
rclient.auth(Settings.redis.web.password)
|
|
|
|
|
|
|
|
|
|
|
|
ONE_HOUR_IN_S = 60 * 60
|
2014-07-14 08:20:29 -04:00
|
|
|
ONE_DAY_IN_S = ONE_HOUR_IN_S * 24
|
|
|
|
FOUR_DAYS_IN_S = ONE_DAY_IN_S * 4
|
2014-07-04 11:18:46 -04:00
|
|
|
|
2014-07-14 10:30:55 -04:00
|
|
|
USER_TIMEOUT_IN_S = ONE_HOUR_IN_S
|
|
|
|
|
2014-07-17 10:25:22 -04:00
|
|
|
buildProjectSetKey = (project_id)-> return "clients_in_project:#{project_id}"
|
|
|
|
buildUserKey = (project_id, client_id)-> return "connected_user:#{project_id}:#{client_id}"
|
2014-07-04 11:18:46 -04:00
|
|
|
|
|
|
|
|
2014-07-14 07:41:06 -04:00
|
|
|
module.exports =
|
2014-07-04 11:18:46 -04:00
|
|
|
|
2014-07-17 10:25:22 -04:00
|
|
|
markUserAsConnected: (project_id, client_id, user, callback = (err)->)->
|
|
|
|
logger.log project_id:project_id, client_id:client_id, "marking user as connected"
|
2014-07-14 10:38:55 -04:00
|
|
|
|
|
|
|
multi = rclient.multi()
|
2014-07-17 10:25:22 -04:00
|
|
|
multi.sadd buildProjectSetKey(project_id), client_id
|
2014-07-14 10:38:55 -04:00
|
|
|
multi.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S
|
2014-07-17 10:25:22 -04:00
|
|
|
multi.hset buildUserKey(project_id, client_id), "connected_at", Date.now()
|
|
|
|
multi.hset buildUserKey(project_id, client_id), "user_id", user._id
|
|
|
|
multi.hset buildUserKey(project_id, client_id), "first_name", user.first_name
|
|
|
|
multi.hset buildUserKey(project_id, client_id), "last_name", user.last_name
|
|
|
|
multi.hset buildUserKey(project_id, client_id), "email", user.email
|
|
|
|
multi.expire buildUserKey(project_id, client_id), USER_TIMEOUT_IN_S
|
2014-07-14 10:38:55 -04:00
|
|
|
multi.exec (err)->
|
2014-07-14 07:41:06 -04:00
|
|
|
if err?
|
2014-07-17 10:25:22 -04:00
|
|
|
logger.err err:err, project_id:project_id, client_id:client_id, "problem marking user as connected"
|
2014-07-14 07:41:06 -04:00
|
|
|
callback(err)
|
2014-07-04 11:18:46 -04:00
|
|
|
|
2014-07-17 10:25:22 -04:00
|
|
|
markUserAsDisconnected: (project_id, client_id, callback)->
|
|
|
|
logger.log project_id:project_id, client_id:client_id, "marking user as disconnected"
|
2014-07-14 10:38:55 -04:00
|
|
|
multi = rclient.multi()
|
2014-07-17 10:25:22 -04:00
|
|
|
multi.srem buildProjectSetKey(project_id), client_id
|
2014-07-14 10:38:55 -04:00
|
|
|
multi.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S
|
2014-07-17 10:25:22 -04:00
|
|
|
multi.del buildUserKey(project_id, client_id)
|
2014-07-14 10:38:55 -04:00
|
|
|
multi.exec callback
|
|
|
|
|
2014-07-10 11:05:05 -04:00
|
|
|
|
2014-07-17 10:25:22 -04:00
|
|
|
_getConnectedUser: (project_id, client_id, callback)->
|
|
|
|
rclient.hgetall buildUserKey(project_id, client_id), (err, result)->
|
2014-07-04 11:18:46 -04:00
|
|
|
if !result?
|
2014-07-14 10:30:55 -04:00
|
|
|
result =
|
|
|
|
connected : false
|
2014-07-17 10:25:22 -04:00
|
|
|
client_id:client_id
|
2014-07-04 11:18:46 -04:00
|
|
|
else
|
2014-07-14 10:30:55 -04:00
|
|
|
result.connected = true
|
2014-07-17 10:25:22 -04:00
|
|
|
result.client_id = client_id
|
2014-07-14 11:16:01 -04:00
|
|
|
if result.cursorData?
|
|
|
|
result.cursorData = JSON.parse(result.cursorData)
|
2014-07-14 10:30:55 -04:00
|
|
|
callback err, result
|
|
|
|
|
2014-07-17 10:25:22 -04:00
|
|
|
setUserCursorPosition: (project_id, client_id, cursorData, callback)->
|
2014-07-14 10:38:55 -04:00
|
|
|
multi = rclient.multi()
|
2014-07-17 10:25:22 -04:00
|
|
|
multi.hset buildUserKey(project_id, client_id), "cursorData", JSON.stringify(cursorData)
|
|
|
|
multi.expire buildUserKey(project_id, client_id), USER_TIMEOUT_IN_S
|
2014-07-14 10:38:55 -04:00
|
|
|
multi.exec callback
|
2014-07-04 11:18:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
getConnectedUsers: (project_id, callback)->
|
|
|
|
self = @
|
2014-07-14 07:41:06 -04:00
|
|
|
rclient.smembers buildProjectSetKey(project_id), (err, results)->
|
2014-07-17 10:25:22 -04:00
|
|
|
jobs = results.map (client_id)->
|
2014-07-04 11:18:46 -04:00
|
|
|
(cb)->
|
2014-07-17 10:25:22 -04:00
|
|
|
self._getConnectedUser(project_id, client_id, cb)
|
2014-07-04 11:18:46 -04:00
|
|
|
async.series jobs, (err, users)->
|
|
|
|
users = _.filter users, (user)->
|
|
|
|
user.connected
|
2014-07-14 10:30:55 -04:00
|
|
|
callback err, users
|
2014-07-04 11:18:46 -04:00
|
|
|
|