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-10 11:05:05 -04:00
|
|
|
buildProjectSetKey = (project_id)-> return "users_in_project:#{project_id}"
|
2014-07-04 11:18:46 -04:00
|
|
|
buildUserKey = (project_id, user_id)-> return "connected_user:#{project_id}:#{user_id}"
|
|
|
|
|
|
|
|
|
2014-07-14 07:41:06 -04:00
|
|
|
module.exports =
|
2014-07-04 11:18:46 -04:00
|
|
|
|
|
|
|
markUserAsConnected: (project_id, user_id, callback = (err)->)->
|
2014-07-10 12:01:19 -04:00
|
|
|
logger.log project_id:project_id, user_id:user_id, "marking user as connected"
|
2014-07-04 11:18:46 -04:00
|
|
|
async.series [
|
|
|
|
(cb)->
|
|
|
|
rclient.sadd buildProjectSetKey(project_id), user_id, cb
|
|
|
|
(cb)->
|
2014-07-14 08:20:29 -04:00
|
|
|
rclient.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S, cb
|
|
|
|
(cb)->
|
|
|
|
rclient.setex buildUserKey(project_id, user_id), ONE_HOUR_IN_S, new Date(), cb
|
2014-07-14 07:41:06 -04:00
|
|
|
], (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, user_id:user_id, "problem marking user as connected"
|
|
|
|
callback(err)
|
2014-07-04 11:18:46 -04:00
|
|
|
|
2014-07-10 12:01:42 -04:00
|
|
|
markUserAsDisconnected: (project_id, user_id, callback)->
|
2014-07-10 12:01:19 -04:00
|
|
|
logger.log project_id:project_id, user_id:user_id, "marking user as disconnected"
|
2014-07-10 11:05:05 -04:00
|
|
|
async.series [
|
|
|
|
(cb)->
|
|
|
|
rclient.srem buildProjectSetKey(project_id), user_id, cb
|
2014-07-14 08:20:29 -04:00
|
|
|
(cb)->
|
|
|
|
rclient.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S, cb
|
2014-07-10 11:05:05 -04:00
|
|
|
(cb)->
|
|
|
|
rclient.del buildUserKey(project_id, user_id), cb
|
|
|
|
], callback
|
|
|
|
|
2014-07-04 11:18:46 -04:00
|
|
|
_getConnectedUser: (project_id, user_id, callback)->
|
|
|
|
rclient.get buildUserKey(project_id, user_id), (err, result)->
|
|
|
|
if !result?
|
|
|
|
connected = false
|
|
|
|
else
|
|
|
|
connected = true
|
|
|
|
|
|
|
|
callback err, {connected:connected, user_id:user_id}
|
|
|
|
|
|
|
|
getConnectedUsers: (project_id, callback)->
|
|
|
|
self = @
|
2014-07-14 07:41:06 -04:00
|
|
|
rclient.smembers buildProjectSetKey(project_id), (err, results)->
|
2014-07-04 11:18:46 -04:00
|
|
|
jobs = results.map (user_id)->
|
|
|
|
(cb)->
|
|
|
|
self._getConnectedUser(project_id, user_id, cb)
|
|
|
|
async.series jobs, (err, users)->
|
|
|
|
users = _.filter users, (user)->
|
|
|
|
user.connected
|
|
|
|
user_ids = _.map users, (user)->
|
|
|
|
user.user_id
|
|
|
|
callback err, user_ids
|
|
|
|
|
|
|
|
|