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

227 lines
10 KiB
CoffeeScript
Raw Normal View History

2014-11-10 11:27:08 +00:00
logger = require "logger-sharelatex"
2014-11-17 13:12:49 +00:00
metrics = require "metrics-sharelatex"
2014-11-10 11:27:08 +00:00
WebApiManager = require "./WebApiManager"
2014-11-12 15:54:55 +00:00
AuthorizationManager = require "./AuthorizationManager"
DocumentUpdaterManager = require "./DocumentUpdaterManager"
ConnectedUsersManager = require "./ConnectedUsersManager"
WebsocketLoadBalancer = require "./WebsocketLoadBalancer"
Utils = require "./Utils"
2014-11-10 11:27:08 +00:00
module.exports = WebsocketController =
# If the protocol version changes when the client reconnects,
# it will force a full refresh of the page. Useful for non-backwards
# compatible protocol changes. Use only in extreme need.
PROTOCOL_VERSION: 2
joinProject: (client, user, project_id, callback = (error, project, privilegeLevel, protocolVersion) ->) ->
user_id = user?._id
2014-11-12 15:54:55 +00:00
logger.log {user_id, project_id, client_id: client.id}, "user joining project"
2014-11-17 13:12:49 +00:00
metrics.inc "editor.join-project"
WebApiManager.joinProject project_id, user, (error, project, privilegeLevel) ->
2014-11-10 11:27:08 +00:00
return callback(error) if error?
if !privilegeLevel or privilegeLevel == ""
err = new Error("not authorized")
2014-11-12 15:54:55 +00:00
logger.error {err, project_id, user_id, client_id: client.id}, "user is not authorized to join project"
return callback(err)
client.join project_id
2014-11-10 11:27:08 +00:00
2014-11-12 15:54:55 +00:00
client.set("privilege_level", privilegeLevel)
2014-11-10 11:27:08 +00:00
client.set("user_id", user_id)
client.set("project_id", project_id)
client.set("owner_id", project?.owner?._id)
client.set("first_name", user?.first_name)
client.set("last_name", user?.last_name)
client.set("email", user?.email)
client.set("connected_time", new Date())
client.set("signup_date", user?.signUpDate)
client.set("login_count", user?.loginCount)
2014-11-12 15:54:55 +00:00
callback null, project, privilegeLevel, WebsocketController.PROTOCOL_VERSION
2014-11-17 12:46:27 +00:00
logger.log {user_id, project_id, client_id: client.id}, "user joined project"
2014-11-12 15:54:55 +00:00
# No need to block for setting the user as connected in the cursor tracking
ConnectedUsersManager.updateUserPosition project_id, client.id, user, null, () ->
2014-11-14 16:51:55 +00:00
# We want to flush a project if there are no more (local) connected clients
# but we need to wait for the triggering client to disconnect. How long we wait
# is determined by FLUSH_IF_EMPTY_DELAY.
FLUSH_IF_EMPTY_DELAY: 500 #ms
leaveProject: (io, client, callback = (error) ->) ->
2014-11-17 13:12:49 +00:00
metrics.inc "editor.leave-project"
2014-11-14 16:51:55 +00:00
Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
return callback(error) if error?
logger.log {project_id, user_id, client_id: client.id}, "client leaving project"
WebsocketLoadBalancer.emitToRoom project_id, "clientTracking.clientDisconnected", client.id
# bail out if the client had not managed to authenticate or join
# the project. Prevents downstream errors in docupdater from
# flushProjectToMongoAndDelete with null project_id.
if not user_id?
logger.log {client_id: client.id}, "client leaving, unknown user"
return callback()
if not project_id?
logger.log {user_id: user_id, client_id: client.id}, "client leaving, not in project"
return callback()
2014-11-14 16:51:55 +00:00
# We can do this in the background
ConnectedUsersManager.markUserAsDisconnected project_id, client.id, (err) ->
if err?
logger.error {err, project_id, user_id, client_id: client.id}, "error marking client as disconnected"
setTimeout () ->
remainingClients = io.sockets.clients(project_id)
if remainingClients.length == 0
# Flush project in the background
DocumentUpdaterManager.flushProjectToMongoAndDelete project_id, (err) ->
if err?
logger.error {err, project_id, user_id, client_id: client.id}, "error flushing to doc updater after leaving project"
callback()
, WebsocketController.FLUSH_IF_EMPTY_DELAY
2017-09-21 15:56:09 +00:00
joinDoc: (client, doc_id, fromVersion = -1, options, callback = (error, doclines, version, ops, ranges) ->) ->
2014-11-17 13:12:49 +00:00
metrics.inc "editor.join-doc"
Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
2014-11-17 12:46:27 +00:00
return callback(error) if error?
return callback(new Error("no project_id found on client")) if !project_id?
logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joining doc"
2014-11-12 15:54:55 +00:00
2014-11-17 12:46:27 +00:00
AuthorizationManager.assertClientCanViewProject client, (error) ->
2014-11-12 15:54:55 +00:00
return callback(error) if error?
DocumentUpdaterManager.getDocument project_id, doc_id, fromVersion, (error, lines, version, ranges, ops) ->
2014-11-12 15:54:55 +00:00
return callback(error) if error?
2017-09-21 14:07:15 +00:00
2014-11-12 15:54:55 +00:00
# Encode any binary bits of data so it can go via WebSockets
# See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
2017-09-21 14:07:15 +00:00
encodeForWebsockets = (text) -> unescape(encodeURIComponent(text))
2014-11-12 15:54:55 +00:00
escapedLines = []
for line in lines
try
2017-09-21 14:07:15 +00:00
line = encodeForWebsockets(line)
2014-11-12 15:54:55 +00:00
catch err
logger.err {err, project_id, doc_id, fromVersion, line, client_id: client.id}, "error encoding line uri component"
return callback(err)
escapedLines.push line
2017-09-21 13:23:16 +00:00
if options.encodeRanges
2017-09-21 14:07:15 +00:00
try
for comment in ranges?.comments or []
2017-09-22 08:34:10 +00:00
comment.op.c = encodeForWebsockets(comment.op.c) if comment.op.c?
2017-09-21 14:07:15 +00:00
for change in ranges?.changes or []
change.op.i = encodeForWebsockets(change.op.i) if change.op.i?
change.op.d = encodeForWebsockets(change.op.d) if change.op.d?
2017-09-21 14:07:15 +00:00
catch err
logger.err {err, project_id, doc_id, fromVersion, ranges, client_id: client.id}, "error encoding range uri component"
return callback(err)
AuthorizationManager.addAccessToDoc client, doc_id
2014-11-12 15:54:55 +00:00
client.join(doc_id)
callback null, escapedLines, version, ops, ranges
2014-11-17 12:46:27 +00:00
logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joined doc"
2014-11-12 16:51:48 +00:00
leaveDoc: (client, doc_id, callback = (error) ->) ->
2014-11-17 13:12:49 +00:00
metrics.inc "editor.leave-doc"
Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
logger.log {user_id, project_id, doc_id, client_id: client.id}, "client leaving doc"
client.leave doc_id
2016-09-05 11:35:49 +00:00
# we could remove permission when user leaves a doc, but because
# the connection is per-project, we continue to allow access
# after the initial joinDoc since we know they are already authorised.
## AuthorizationManager.removeAccessToDoc client, doc_id
callback()
updateClientPosition: (client, cursorData, callback = (error) ->) ->
2014-11-17 13:12:49 +00:00
metrics.inc "editor.update-client-position", 0.1
Utils.getClientAttributes client, [
"project_id", "first_name", "last_name", "email", "user_id"
], (error, {project_id, first_name, last_name, email, user_id}) ->
return callback(error) if error?
logger.log {user_id, project_id, client_id: client.id, cursorData: cursorData}, "updating client position"
AuthorizationManager.assertClientCanViewProjectAndDoc client, cursorData.doc_id, (error) ->
if error?
2016-12-08 11:12:07 +00:00
logger.warn {err: error, client_id: client.id, project_id, user_id}, "silently ignoring unauthorized updateClientPosition. Client likely hasn't called joinProject yet."
2016-09-02 15:34:14 +00:00
return callback()
cursorData.id = client.id
cursorData.user_id = user_id if user_id?
cursorData.email = email if email?
# Don't store anonymous users in redis to avoid influx
if !user_id or user_id == 'anonymous-user'
cursorData.name = ""
callback()
else
cursorData.name = if first_name && last_name
"#{first_name} #{last_name}"
else if first_name
first_name
else if last_name
last_name
else
""
ConnectedUsersManager.updateUserPosition(project_id, client.id, {
first_name: first_name,
last_name: last_name,
email: email,
_id: user_id
}, {
row: cursorData.row,
column: cursorData.column,
doc_id: cursorData.doc_id
}, callback)
WebsocketLoadBalancer.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData)
getConnectedUsers: (client, callback = (error, users) ->) ->
2014-11-17 13:12:49 +00:00
metrics.inc "editor.get-connected-users"
Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
return callback(error) if error?
2014-11-17 12:46:27 +00:00
return callback(new Error("no project_id found on client")) if !project_id?
logger.log {user_id, project_id, client_id: client.id}, "getting connected users"
AuthorizationManager.assertClientCanViewProject client, (error) ->
return callback(error) if error?
ConnectedUsersManager.getConnectedUsers project_id, (error, users) ->
return callback(error) if error?
callback null, users
2014-11-17 12:46:27 +00:00
logger.log {user_id, project_id, client_id: client.id}, "got connected users"
2014-11-14 10:12:35 +00:00
applyOtUpdate: (client, doc_id, update, callback = (error) ->) ->
2014-11-17 12:46:27 +00:00
Utils.getClientAttributes client, ["user_id", "project_id"], (error, {user_id, project_id}) ->
return callback(error) if error?
return callback(new Error("no project_id found on client")) if !project_id?
WebsocketController._assertClientCanApplyUpdate client, doc_id, update, (error) ->
2014-11-17 12:46:27 +00:00
if error?
2017-11-10 15:01:23 +00:00
logger.warn {err: error, doc_id, client_id: client.id, version: update.v}, "client is not authorized to make update"
2014-11-17 12:46:27 +00:00
setTimeout () ->
# Disconnect, but give the client the chance to receive the error
client.disconnect()
, 100
return callback(error)
update.meta ||= {}
update.meta.source = client.id
update.meta.user_id = user_id
2014-11-17 13:12:49 +00:00
metrics.inc "editor.doc-update", 0.3
2017-03-15 15:45:18 +00:00
logger.log {user_id, doc_id, project_id, client_id: client.id, version: update.v}, "sending update to doc updater"
DocumentUpdaterManager.queueChange project_id, doc_id, update, (error) ->
if error?
logger.error {err: error, project_id, doc_id, client_id: client.id, version: update.v}, "document was not available for update"
client.disconnect()
callback(error)
_assertClientCanApplyUpdate: (client, doc_id, update, callback) ->
AuthorizationManager.assertClientCanEditProjectAndDoc client, doc_id, (error) ->
if error?
if error.message == "not authorized" and WebsocketController._isCommentUpdate(update)
# This might be a comment op, which we only need read-only priveleges for
AuthorizationManager.assertClientCanViewProjectAndDoc client, doc_id, callback
else
return callback(error)
else
return callback(null)
_isCommentUpdate: (update) ->
for op in update.op
if !op.c?
return false
return true