2014-11-07 12:38:12 -05:00
|
|
|
Metrics = require "metrics-sharelatex"
|
|
|
|
logger = require "logger-sharelatex"
|
2014-11-10 06:27:08 -05:00
|
|
|
WebsocketController = require "./WebsocketController"
|
2014-11-13 06:48:49 -05:00
|
|
|
HttpController = require "./HttpController"
|
2014-11-13 07:03:43 -05:00
|
|
|
Utils = require "./Utils"
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
module.exports = Router =
|
|
|
|
configure: (app, io, session) ->
|
2014-11-13 07:03:43 -05:00
|
|
|
app.set("io", io)
|
|
|
|
app.get "/clients", HttpController.getConnectedClients
|
|
|
|
app.get "/clients/:client_id", HttpController.getConnectedClient
|
|
|
|
|
2014-11-07 12:38:12 -05:00
|
|
|
session.on 'connection', (error, client, session) ->
|
|
|
|
if error?
|
|
|
|
logger.err err: error, "error when client connected"
|
|
|
|
client?.disconnect()
|
|
|
|
return
|
|
|
|
|
|
|
|
Metrics.inc('socket-io.connection')
|
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
logger.log session: session, client_id: client.id, "client connected"
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
user = session.user
|
2014-11-10 06:27:08 -05:00
|
|
|
if !user? or !user._id?
|
2014-11-07 12:38:12 -05:00
|
|
|
logger.log "terminating session without authenticated user"
|
|
|
|
client.disconnect()
|
2014-11-10 06:27:08 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
client.on "joinProject", (data = {}, callback) ->
|
2014-11-12 10:54:55 -05:00
|
|
|
WebsocketController.joinProject client, user, data.project_id, (err, args...) ->
|
|
|
|
if err?
|
2014-11-13 07:03:43 -05:00
|
|
|
logger.error {err, user_id: user?.id, project_id: data.project_id}, "server side error in joinProject"
|
2014-11-12 10:54:55 -05:00
|
|
|
# Don't return raw error to prevent leaking server side info
|
|
|
|
return callback {message: "Something went wrong"}
|
|
|
|
else
|
|
|
|
callback(null, args...)
|
|
|
|
|
|
|
|
|
|
|
|
client.on "joinDoc", (doc_id, fromVersion, callback) ->
|
|
|
|
# fromVersion is optional
|
|
|
|
if typeof fromVersion == "function"
|
|
|
|
callback = fromVersion
|
|
|
|
fromVersion = -1
|
|
|
|
|
|
|
|
WebsocketController.joinDoc client, doc_id, fromVersion, (err, args...) ->
|
|
|
|
if err?
|
2014-11-13 07:03:43 -05:00
|
|
|
Utils.getClientAttributes client, ["project_id", "user_id"], (_, {project_id, user_id}) ->
|
|
|
|
logger.error {err, client_id: client.id, user_id, project_id, doc_id, fromVersion}, "server side error in joinDoc"
|
2014-11-12 10:54:55 -05:00
|
|
|
# Don't return raw error to prevent leaking server side info
|
|
|
|
return callback {message: "Something went wrong"}
|
|
|
|
else
|
|
|
|
callback(null, args...)
|
2014-11-12 11:51:48 -05:00
|
|
|
|
|
|
|
client.on "leaveDoc", (doc_id, callback) ->
|
|
|
|
WebsocketController.leaveDoc client, doc_id, (err, args...) ->
|
|
|
|
if err?
|
2014-11-13 07:03:43 -05:00
|
|
|
Utils.getClientAttributes client, ["project_id", "user_id"], (_, {project_id, user_id}) ->
|
|
|
|
logger.error {err, client_id: client.id, user_id, project_id, doc_id}, "server side error in leaveDoc"
|
2014-11-12 11:51:48 -05:00
|
|
|
# Don't return raw error to prevent leaking server side info
|
|
|
|
return callback {message: "Something went wrong"}
|
|
|
|
else
|
2014-11-13 06:48:49 -05:00
|
|
|
callback(null, args...)
|
2014-11-13 08:05:49 -05:00
|
|
|
|
|
|
|
client.on "getConnectedUsers", (callback = (error, users) ->) ->
|
|
|
|
WebsocketController.getConnectedUsers client, (err, users) ->
|
|
|
|
if err?
|
|
|
|
Utils.getClientAttributes client, ["project_id", "user_id", "doc_id"], (_, {project_id, user_id, doc_id}) ->
|
|
|
|
logger.error {err, client_id: client.id, user_id, project_id, doc_id}, "server side error in getConnectedUsers"
|
|
|
|
# Don't return raw error to prevent leaking server side info
|
|
|
|
return callback {message: "Something went wrong"}
|
|
|
|
else
|
|
|
|
callback(null, users)
|
2014-11-13 07:03:43 -05:00
|
|
|
|