2014-11-17 13:12:49 +00:00
|
|
|
metrics = require "metrics-sharelatex"
|
2014-11-07 17:38:12 +00:00
|
|
|
logger = require "logger-sharelatex"
|
2014-11-10 11:27:08 +00:00
|
|
|
WebsocketController = require "./WebsocketController"
|
2014-11-13 11:48:49 +00:00
|
|
|
HttpController = require "./HttpController"
|
2014-11-13 12:03:43 +00:00
|
|
|
Utils = require "./Utils"
|
2014-11-07 17:38:12 +00:00
|
|
|
|
|
|
|
module.exports = Router =
|
2014-11-17 12:23:30 +00:00
|
|
|
_handleError: (callback = ((error) ->), error, client, method, extraAttrs = {}) ->
|
2014-11-13 15:27:18 +00:00
|
|
|
Utils.getClientAttributes client, ["project_id", "doc_id", "user_id"], (_, attrs) ->
|
|
|
|
for key, value of extraAttrs
|
|
|
|
attrs[key] = value
|
|
|
|
attrs.client_id = client.id
|
|
|
|
attrs.err = error
|
|
|
|
logger.error attrs, "server side error in #{method}"
|
|
|
|
# Don't return raw error to prevent leaking server side info
|
|
|
|
return callback {message: "Something went wrong"}
|
|
|
|
|
2014-11-07 17:38:12 +00:00
|
|
|
configure: (app, io, session) ->
|
2014-11-13 12:03:43 +00:00
|
|
|
app.set("io", io)
|
|
|
|
app.get "/clients", HttpController.getConnectedClients
|
|
|
|
app.get "/clients/:client_id", HttpController.getConnectedClient
|
|
|
|
|
2014-11-07 17:38:12 +00:00
|
|
|
session.on 'connection', (error, client, session) ->
|
|
|
|
if error?
|
|
|
|
logger.err err: error, "error when client connected"
|
|
|
|
client?.disconnect()
|
|
|
|
return
|
|
|
|
|
2014-11-17 13:12:49 +00:00
|
|
|
metrics.inc('socket-io.connection')
|
2014-11-07 17:38:12 +00:00
|
|
|
|
2014-11-12 15:54:55 +00:00
|
|
|
logger.log session: session, client_id: client.id, "client connected"
|
2014-11-07 17:38:12 +00:00
|
|
|
|
|
|
|
user = session.user
|
2014-11-10 11:27:08 +00:00
|
|
|
if !user? or !user._id?
|
2014-11-07 17:38:12 +00:00
|
|
|
logger.log "terminating session without authenticated user"
|
|
|
|
client.disconnect()
|
2014-11-10 11:27:08 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
client.on "joinProject", (data = {}, callback) ->
|
2014-11-12 15:54:55 +00:00
|
|
|
WebsocketController.joinProject client, user, data.project_id, (err, args...) ->
|
|
|
|
if err?
|
2014-11-13 15:27:18 +00:00
|
|
|
Router._handleError callback, err, client, "joinProject", {project_id: data.project_id, user_id: user?.id}
|
2014-11-12 15:54:55 +00:00
|
|
|
else
|
|
|
|
callback(null, args...)
|
|
|
|
|
2014-11-17 12:23:30 +00:00
|
|
|
client.on "disconnect", () ->
|
2014-11-17 13:12:49 +00:00
|
|
|
metrics.inc('socket-io.disconnect')
|
2014-11-17 12:23:30 +00:00
|
|
|
WebsocketController.leaveProject io, client, (err) ->
|
|
|
|
if err?
|
|
|
|
Router._handleError null, err, client, "leaveProject"
|
|
|
|
|
2014-11-12 15:54:55 +00:00
|
|
|
|
|
|
|
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 15:27:18 +00:00
|
|
|
Router._handleError callback, err, client, "joinDoc", {doc_id, fromVersion}
|
2014-11-12 15:54:55 +00:00
|
|
|
else
|
|
|
|
callback(null, args...)
|
2014-11-12 16:51:48 +00:00
|
|
|
|
|
|
|
client.on "leaveDoc", (doc_id, callback) ->
|
|
|
|
WebsocketController.leaveDoc client, doc_id, (err, args...) ->
|
|
|
|
if err?
|
2014-11-13 15:27:18 +00:00
|
|
|
Router._handleError callback, err, client, "leaveDoc"
|
2014-11-12 16:51:48 +00:00
|
|
|
else
|
2014-11-13 11:48:49 +00:00
|
|
|
callback(null, args...)
|
2014-11-13 13:05:49 +00:00
|
|
|
|
2014-11-13 15:27:18 +00:00
|
|
|
client.on "clientTracking.getConnectedUsers", (callback = (error, users) ->) ->
|
2014-11-13 13:05:49 +00:00
|
|
|
WebsocketController.getConnectedUsers client, (err, users) ->
|
|
|
|
if err?
|
2014-11-13 15:27:18 +00:00
|
|
|
Router._handleError callback, err, client, "clientTracking.getConnectedUsers"
|
2014-11-13 13:05:49 +00:00
|
|
|
else
|
|
|
|
callback(null, users)
|
2014-11-13 15:27:18 +00:00
|
|
|
|
|
|
|
client.on "clientTracking.updatePosition", (cursorData, callback = (error) ->) ->
|
|
|
|
WebsocketController.updateClientPosition client, cursorData, (err) ->
|
|
|
|
if err?
|
|
|
|
Router._handleError callback, err, client, "clientTracking.updatePosition"
|
2014-11-14 10:12:35 +00:00
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
|
|
|
client.on "applyOtUpdate", (doc_id, update, callback = (error) ->) ->
|
|
|
|
WebsocketController.applyOtUpdate client, doc_id, update, (err) ->
|
|
|
|
if err?
|
|
|
|
Router._handleError callback, err, client, "applyOtUpdate", {doc_id, update}
|
2014-11-13 15:27:18 +00:00
|
|
|
else
|
|
|
|
callback()
|