overleaf/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee

80 lines
2.8 KiB
CoffeeScript
Raw Normal View History

_ = require("underscore")
async = require("async")
Settings = require('settings-sharelatex')
logger = require("logger-sharelatex")
2014-09-26 13:52:00 +00:00
redis = require("redis-sharelatex")
rclient = redis.createClient(Settings.redis.web)
ONE_HOUR_IN_S = 60 * 60
ONE_DAY_IN_S = ONE_HOUR_IN_S * 24
FOUR_DAYS_IN_S = ONE_DAY_IN_S * 4
USER_TIMEOUT_IN_S = ONE_HOUR_IN_S / 4
2014-07-14 14:30:55 +00:00
2014-07-17 14:25:22 +00:00
buildProjectSetKey = (project_id)-> return "clients_in_project:#{project_id}"
buildUserKey = (project_id, client_id)-> return "connected_user:#{project_id}:#{client_id}"
module.exports =
# Use the same method for when a user connects, and when a user sends a cursor
# update. This way we don't care if the connected_user key has expired when
# we receive a cursor update.
updateUserPosition: (project_id, client_id, user, cursorData, callback = (err)->)->
2014-07-17 14:25:22 +00:00
logger.log project_id:project_id, client_id:client_id, "marking user as connected"
2014-07-14 14:38:55 +00:00
multi = rclient.multi()
multi.sadd buildProjectSetKey(project_id), client_id
2014-07-14 14:38:55 +00:00
multi.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S
multi.hset buildUserKey(project_id, client_id), "last_updated_at", Date.now()
2014-07-17 14:25:22 +00:00
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
if cursorData?
multi.hset buildUserKey(project_id, client_id), "cursorData", JSON.stringify(cursorData)
2014-07-17 14:25:22 +00:00
multi.expire buildUserKey(project_id, client_id), USER_TIMEOUT_IN_S
2014-07-14 14:38:55 +00:00
multi.exec (err)->
if err?
2014-07-17 14:25:22 +00:00
logger.err err:err, project_id:project_id, client_id:client_id, "problem marking user as connected"
callback(err)
2014-07-17 14:25:22 +00:00
markUserAsDisconnected: (project_id, client_id, callback)->
logger.log project_id:project_id, client_id:client_id, "marking user as disconnected"
2014-07-14 14:38:55 +00:00
multi = rclient.multi()
2014-07-17 14:25:22 +00:00
multi.srem buildProjectSetKey(project_id), client_id
2014-07-14 14:38:55 +00:00
multi.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S
2014-07-17 14:25:22 +00:00
multi.del buildUserKey(project_id, client_id)
2014-07-14 14:38:55 +00:00
multi.exec callback
2014-07-17 14:25:22 +00:00
_getConnectedUser: (project_id, client_id, callback)->
rclient.hgetall buildUserKey(project_id, client_id), (err, result)->
if !result?
2014-07-14 14:30:55 +00:00
result =
connected : false
2014-07-17 14:25:22 +00:00
client_id:client_id
else
2014-07-14 14:30:55 +00:00
result.connected = true
2014-07-17 14:25:22 +00:00
result.client_id = client_id
if result.cursorData?
result.cursorData = JSON.parse(result.cursorData)
2014-07-14 14:30:55 +00:00
callback err, result
getConnectedUsers: (project_id, callback)->
self = @
rclient.smembers buildProjectSetKey(project_id), (err, results)->
2014-07-17 14:25:22 +00:00
jobs = results.map (client_id)->
(cb)->
2014-07-17 14:25:22 +00:00
self._getConnectedUser(project_id, client_id, cb)
async.series jobs, (err, users)->
users = _.filter users, (user)->
user.connected
2014-07-14 14:30:55 +00:00
callback err, users