overleaf/services/real-time/app/coffee/Router.coffee

107 lines
3.9 KiB
CoffeeScript
Raw Normal View History

2014-11-17 08:12:49 -05:00
metrics = require "metrics-sharelatex"
logger = require "logger-sharelatex"
settings = require "settings-sharelatex"
2014-11-10 06:27:08 -05:00
WebsocketController = require "./WebsocketController"
HttpController = require "./HttpController"
HttpApiController = require "./HttpApiController"
Utils = require "./Utils"
bodyParser = require "body-parser"
basicAuth = require('basic-auth-connect')
httpAuth = basicAuth (user, pass)->
isValid = user == settings.internal.realTime.user and pass == settings.internal.realTime.pass
if !isValid
logger.err user:user, pass:pass, "invalid login details"
return isValid
module.exports = Router =
_handleError: (callback = ((error) ->), error, client, method, extraAttrs = {}) ->
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
if error.message in ["not authorized", "doc updater could not load requested ops"]
logger.warn attrs, error.message
return callback {message: error.message}
else
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 in real-time service"}
configure: (app, io, session) ->
app.set("io", io)
app.get "/clients", HttpController.getConnectedClients
app.get "/clients/:client_id", HttpController.getConnectedClient
app.post "/project/:project_id/message/:message", httpAuth, bodyParser.json(limit: "5mb"), HttpApiController.sendMessage
session.on 'connection', (error, client, session) ->
if error?
logger.err err: error, "error when client connected"
client?.disconnect()
return
2014-11-17 08:12:49 -05:00
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-21 06:48:59 -05:00
if !session or !session.user?
user = {_id: "anonymous-user"}
else
user = session.user
2014-11-10 06:27:08 -05:00
client.on "joinProject", (data = {}, callback) ->
2014-11-12 10:54:55 -05:00
WebsocketController.joinProject client, user, data.project_id, (err, args...) ->
if err?
Router._handleError callback, err, client, "joinProject", {project_id: data.project_id, user_id: user?.id}
2014-11-12 10:54:55 -05:00
else
callback(null, args...)
client.on "disconnect", () ->
2014-11-17 08:12:49 -05:00
metrics.inc('socket-io.disconnect')
WebsocketController.leaveProject io, client, (err) ->
if err?
Router._handleError null, err, client, "leaveProject"
2014-11-12 10:54:55 -05: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?
Router._handleError callback, err, client, "joinDoc", {doc_id, fromVersion}
2014-11-12 10:54:55 -05:00
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?
Router._handleError callback, err, client, "leaveDoc"
2014-11-12 11:51:48 -05:00
else
callback(null, args...)
client.on "clientTracking.getConnectedUsers", (callback = (error, users) ->) ->
WebsocketController.getConnectedUsers client, (err, users) ->
if err?
Router._handleError callback, err, client, "clientTracking.getConnectedUsers"
else
callback(null, users)
client.on "clientTracking.updatePosition", (cursorData, callback = (error) ->) ->
WebsocketController.updateClientPosition client, cursorData, (err) ->
if err?
Router._handleError callback, err, client, "clientTracking.updatePosition"
2014-11-14 05:12:35 -05: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}
else
callback()