2014-11-13 07:27:46 -05:00
|
|
|
async = require("async")
|
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
redis = require("redis-sharelatex")
|
2017-05-02 10:51:17 -04:00
|
|
|
rclient = redis.createClient(Settings.redis.realtime)
|
|
|
|
Keys = Settings.redis.realtime.key_schema
|
2014-11-13 07:27:46 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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-11-13 08:05:49 -05:00
|
|
|
logger.log project_id:project_id, client_id:client_id, "marking user as joined or connected"
|
2014-11-13 07:27:46 -05:00
|
|
|
|
|
|
|
multi = rclient.multi()
|
|
|
|
|
2017-05-02 10:51:17 -04:00
|
|
|
multi.sadd Keys.clientsInProject({project_id}), client_id
|
|
|
|
multi.expire Keys.clientsInProject({project_id}), FOUR_DAYS_IN_S
|
2014-11-13 07:27:46 -05:00
|
|
|
|
2017-05-02 10:51:17 -04:00
|
|
|
multi.hset Keys.connectedUser({project_id, client_id}), "last_updated_at", Date.now()
|
|
|
|
multi.hset Keys.connectedUser({project_id, client_id}), "user_id", user._id
|
|
|
|
multi.hset Keys.connectedUser({project_id, client_id}), "first_name", user.first_name or ""
|
|
|
|
multi.hset Keys.connectedUser({project_id, client_id}), "last_name", user.last_name or ""
|
|
|
|
multi.hset Keys.connectedUser({project_id, client_id}), "email", user.email or ""
|
2014-11-13 07:27:46 -05:00
|
|
|
|
|
|
|
if cursorData?
|
2017-05-02 10:51:17 -04:00
|
|
|
multi.hset Keys.connectedUser({project_id, client_id}), "cursorData", JSON.stringify(cursorData)
|
|
|
|
multi.expire Keys.connectedUser({project_id, client_id}), USER_TIMEOUT_IN_S
|
2014-11-13 07:27:46 -05:00
|
|
|
|
|
|
|
multi.exec (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, client_id:client_id, "problem marking user as connected"
|
|
|
|
callback(err)
|
|
|
|
|
|
|
|
markUserAsDisconnected: (project_id, client_id, callback)->
|
|
|
|
logger.log project_id:project_id, client_id:client_id, "marking user as disconnected"
|
|
|
|
multi = rclient.multi()
|
2017-05-02 10:51:17 -04:00
|
|
|
multi.srem Keys.clientsInProject({project_id}), client_id
|
|
|
|
multi.expire Keys.clientsInProject({project_id}), FOUR_DAYS_IN_S
|
|
|
|
multi.del Keys.connectedUser({project_id, client_id})
|
2014-11-13 07:27:46 -05:00
|
|
|
multi.exec callback
|
|
|
|
|
|
|
|
|
|
|
|
_getConnectedUser: (project_id, client_id, callback)->
|
2017-05-02 10:51:17 -04:00
|
|
|
rclient.hgetall Keys.connectedUser({project_id, client_id}), (err, result)->
|
2017-05-16 06:06:05 -04:00
|
|
|
if !result? or Object.keys(result).length == 0
|
2014-11-13 07:27:46 -05:00
|
|
|
result =
|
|
|
|
connected : false
|
|
|
|
client_id:client_id
|
|
|
|
else
|
|
|
|
result.connected = true
|
|
|
|
result.client_id = client_id
|
|
|
|
if result.cursorData?
|
2014-11-13 08:05:49 -05:00
|
|
|
try
|
|
|
|
result.cursorData = JSON.parse(result.cursorData)
|
|
|
|
catch e
|
|
|
|
logger.error {err: e, project_id, client_id, cursorData: result.cursorData}, "error parsing cursorData JSON"
|
|
|
|
return callback e
|
2014-11-13 07:27:46 -05:00
|
|
|
callback err, result
|
|
|
|
|
|
|
|
getConnectedUsers: (project_id, callback)->
|
|
|
|
self = @
|
2017-05-02 10:51:17 -04:00
|
|
|
rclient.smembers Keys.clientsInProject({project_id}), (err, results)->
|
2014-11-13 08:05:49 -05:00
|
|
|
return callback(err) if err?
|
2014-11-13 07:27:46 -05:00
|
|
|
jobs = results.map (client_id)->
|
|
|
|
(cb)->
|
|
|
|
self._getConnectedUser(project_id, client_id, cb)
|
2014-11-13 08:05:49 -05:00
|
|
|
async.series jobs, (err, users = [])->
|
|
|
|
return callback(err) if err?
|
2014-11-13 07:27:46 -05:00
|
|
|
users = users.filter (user) ->
|
2014-11-13 08:05:49 -05:00
|
|
|
user?.connected
|
|
|
|
callback null, users
|
2014-11-13 07:27:46 -05:00
|
|
|
|