2014-11-07 12:38:12 -05:00
|
|
|
XMLHttpRequest = require("../../libs/XMLHttpRequest").XMLHttpRequest
|
|
|
|
io = require("socket.io-client")
|
|
|
|
|
2014-11-13 06:48:49 -05:00
|
|
|
request = require "request"
|
2014-11-07 12:38:12 -05:00
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
redis = require "redis-sharelatex"
|
|
|
|
rclient = redis.createClient(Settings.redis.web)
|
|
|
|
|
|
|
|
uid = require('uid-safe').sync
|
|
|
|
signature = require("cookie-signature")
|
|
|
|
|
|
|
|
io.util.request = () ->
|
|
|
|
xhr = new XMLHttpRequest()
|
|
|
|
_open = xhr.open
|
|
|
|
xhr.open = () =>
|
|
|
|
_open.apply(xhr, arguments)
|
|
|
|
if Client.cookie?
|
|
|
|
xhr.setRequestHeader("Cookie", Client.cookie)
|
|
|
|
return xhr
|
|
|
|
|
|
|
|
module.exports = Client =
|
|
|
|
cookie: null
|
|
|
|
|
|
|
|
setSession: (session, callback = (error) ->) ->
|
|
|
|
sessionId = uid(24)
|
|
|
|
session.cookie = {}
|
|
|
|
rclient.set "sess:" + sessionId, JSON.stringify(session), (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
secret = Settings.security.sessionSecret
|
|
|
|
cookieKey = 's:' + signature.sign(sessionId, secret)
|
|
|
|
Client.cookie = "#{Settings.cookieName}=#{cookieKey}"
|
|
|
|
callback()
|
|
|
|
|
|
|
|
unsetSession: (callback = (error) ->) ->
|
|
|
|
Client.cookie = null
|
|
|
|
callback()
|
|
|
|
|
|
|
|
connect: (cookie) ->
|
|
|
|
client = io.connect("http://localhost:3026", 'force new connection': true)
|
|
|
|
return client
|
2014-11-13 06:48:49 -05:00
|
|
|
|
|
|
|
getConnectedClients: (callback = (error, clients) ->) ->
|
|
|
|
request.get {
|
|
|
|
url: "http://localhost:3026/clients"
|
|
|
|
json: true
|
|
|
|
}, (error, response, data) ->
|
|
|
|
callback error, data
|
|
|
|
|
|
|
|
getConnectedClient: (client_id, callback = (error, clients) ->) ->
|
|
|
|
request.get {
|
|
|
|
url: "http://localhost:3026/clients/#{client_id}"
|
|
|
|
json: true
|
|
|
|
}, (error, response, data) ->
|
|
|
|
callback error, data
|
2014-11-07 12:38:12 -05:00
|
|
|
|