2020-08-20 09:05:50 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-11-25 06:57:22 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2020-06-23 13:29:44 -04:00
|
|
|
const WebApiManager = require('./WebApiManager')
|
|
|
|
const AuthorizationManager = require('./AuthorizationManager')
|
|
|
|
const DocumentUpdaterManager = require('./DocumentUpdaterManager')
|
|
|
|
const ConnectedUsersManager = require('./ConnectedUsersManager')
|
|
|
|
const WebsocketLoadBalancer = require('./WebsocketLoadBalancer')
|
|
|
|
const RoomManager = require('./RoomManager')
|
2020-08-20 08:10:22 -04:00
|
|
|
const {
|
|
|
|
JoinLeaveEpochMismatchError,
|
|
|
|
NotAuthorizedError,
|
2021-07-13 07:04:45 -04:00
|
|
|
NotJoinedError,
|
2020-08-20 08:10:22 -04:00
|
|
|
} = require('./Errors')
|
2020-06-23 13:29:44 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
let WebsocketController
|
2020-06-23 13:29:44 -04: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,
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
joinProject(client, user, projectId, callback) {
|
2020-06-23 13:29:44 -04:00
|
|
|
if (client.disconnected) {
|
|
|
|
metrics.inc('editor.join-project.disconnected', 1, {
|
2021-07-13 07:04:45 -04:00
|
|
|
status: 'immediately',
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
const userId = user._id
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info(
|
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
userId,
|
|
|
|
projectId,
|
|
|
|
clientId: client.id,
|
|
|
|
remoteIp: client.remoteIp,
|
|
|
|
userAgent: client.userAgent,
|
2021-09-14 04:36:24 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
'user joining project'
|
|
|
|
)
|
2021-05-10 05:16:50 -04:00
|
|
|
metrics.inc('editor.join-project', 1, { status: client.transport })
|
2021-07-13 07:04:45 -04:00
|
|
|
WebApiManager.joinProject(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2021-07-13 07:04:45 -04:00
|
|
|
user,
|
2023-05-30 08:21:55 -04:00
|
|
|
function (error, project, privilegeLevel, userMetadata) {
|
2021-07-13 07:04:45 -04:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (client.disconnected) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ userId, projectId, clientId: client.id },
|
2021-09-14 04:36:24 -04:00
|
|
|
'client disconnected before joining project'
|
|
|
|
)
|
2021-07-13 07:04:45 -04:00
|
|
|
metrics.inc('editor.join-project.disconnected', 1, {
|
|
|
|
status: 'after-web-api-call',
|
|
|
|
})
|
|
|
|
return callback()
|
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
if (!privilegeLevel) {
|
|
|
|
return callback(new NotAuthorizedError())
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
client.ol_context = {}
|
|
|
|
client.ol_context.privilege_level = privilegeLevel
|
2023-03-20 10:10:40 -04:00
|
|
|
client.ol_context.user_id = userId
|
|
|
|
client.ol_context.project_id = projectId
|
2021-07-13 07:04:45 -04:00
|
|
|
client.ol_context.owner_id = project.owner && project.owner._id
|
|
|
|
client.ol_context.first_name = user.first_name
|
|
|
|
client.ol_context.last_name = user.last_name
|
|
|
|
client.ol_context.email = user.email
|
|
|
|
client.ol_context.connected_time = new Date()
|
|
|
|
client.ol_context.signup_date = user.signUpDate
|
|
|
|
client.ol_context.login_count = user.loginCount
|
2023-05-30 08:21:55 -04:00
|
|
|
client.ol_context.is_restricted_user = !!userMetadata.isRestrictedUser
|
|
|
|
client.ol_context.is_token_member = !!userMetadata.isTokenMember
|
|
|
|
client.ol_context.is_invited_member = !!userMetadata.isInvitedMember
|
2021-07-13 07:04:45 -04:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
RoomManager.joinProject(client, projectId, function (err) {
|
2020-08-20 09:05:50 -04:00
|
|
|
if (err) {
|
2021-07-13 07:04:45 -04:00
|
|
|
return callback(err)
|
2020-08-20 09:05:50 -04:00
|
|
|
}
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
userId,
|
|
|
|
projectId,
|
|
|
|
clientId: client.id,
|
2021-09-14 04:36:24 -04:00
|
|
|
privilegeLevel,
|
2023-05-30 08:21:55 -04:00
|
|
|
userMetadata,
|
2021-09-14 04:36:24 -04:00
|
|
|
},
|
2021-07-13 07:04:45 -04:00
|
|
|
'user joined project'
|
|
|
|
)
|
|
|
|
callback(
|
|
|
|
null,
|
|
|
|
project,
|
|
|
|
privilegeLevel,
|
|
|
|
WebsocketController.PROTOCOL_VERSION
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// No need to block for setting the user as connected in the cursor tracking
|
|
|
|
ConnectedUsersManager.updateUserPosition(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2021-07-13 07:04:45 -04:00
|
|
|
client.publicId,
|
|
|
|
user,
|
|
|
|
null,
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.warn(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ err, projectId, userId, clientId: client.id },
|
2021-07-13 07:04:45 -04:00
|
|
|
'background cursor update failed'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2020-06-23 13:29:44 -04: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) {
|
2023-03-20 10:10:40 -04:00
|
|
|
const { project_id: projectId, user_id: userId } = client.ol_context
|
|
|
|
if (!projectId) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return callback()
|
|
|
|
} // client did not join project
|
|
|
|
|
2021-05-10 05:16:50 -04:00
|
|
|
metrics.inc('editor.leave-project', 1, { status: client.transport })
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ projectId, userId, clientId: client.id },
|
2020-06-23 13:29:44 -04:00
|
|
|
'client leaving project'
|
|
|
|
)
|
|
|
|
WebsocketLoadBalancer.emitToRoom(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2020-06-23 13:29:44 -04:00
|
|
|
'clientTracking.clientDisconnected',
|
|
|
|
client.publicId
|
|
|
|
)
|
|
|
|
|
|
|
|
// We can do this in the background
|
|
|
|
ConnectedUsersManager.markUserAsDisconnected(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2020-06-23 13:29:44 -04:00
|
|
|
client.publicId,
|
|
|
|
function (err) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (err) {
|
|
|
|
logger.error(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ err, projectId, userId, clientId: client.id },
|
2020-06-23 13:29:44 -04:00
|
|
|
'error marking client as disconnected'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
RoomManager.leaveProjectAndDocs(client)
|
2020-07-07 06:06:02 -04:00
|
|
|
setTimeout(function () {
|
2023-03-20 10:10:40 -04:00
|
|
|
const remainingClients = io.sockets.clients(projectId)
|
2020-06-23 13:29:44 -04:00
|
|
|
if (remainingClients.length === 0) {
|
|
|
|
// Flush project in the background
|
|
|
|
DocumentUpdaterManager.flushProjectToMongoAndDelete(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2020-06-23 13:29:44 -04:00
|
|
|
function (err) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (err) {
|
|
|
|
logger.error(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ err, projectId, userId, clientId: client.id },
|
2020-06-23 13:29:44 -04:00
|
|
|
'error flushing to doc updater after leaving project'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
callback()
|
2020-06-23 13:29:44 -04:00
|
|
|
}, WebsocketController.FLUSH_IF_EMPTY_DELAY)
|
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
joinDoc(client, docId, fromVersion, options, callback) {
|
2020-06-23 13:29:44 -04:00
|
|
|
if (client.disconnected) {
|
|
|
|
metrics.inc('editor.join-doc.disconnected', 1, { status: 'immediately' })
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2020-07-13 05:42:50 -04:00
|
|
|
const joinLeaveEpoch = ++client.joinLeaveEpoch
|
2021-05-10 05:16:50 -04:00
|
|
|
metrics.inc('editor.join-doc', 1, { status: client.transport })
|
2023-03-20 10:10:40 -04:00
|
|
|
const {
|
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
|
|
|
is_restricted_user: isRestrictedUser,
|
|
|
|
} = client.ol_context
|
|
|
|
if (!projectId) {
|
2020-08-20 08:07:27 -04:00
|
|
|
return callback(new NotJoinedError())
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ userId, projectId, docId, fromVersion, clientId: client.id },
|
2020-06-23 13:29:44 -04:00
|
|
|
'client joining doc'
|
|
|
|
)
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
WebsocketController._assertClientAuthorization(
|
|
|
|
client,
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2021-07-13 07:04:45 -04:00
|
|
|
function (error) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (client.disconnected) {
|
|
|
|
metrics.inc('editor.join-doc.disconnected', 1, {
|
2021-07-13 07:04:45 -04:00
|
|
|
status: 'after-client-auth-check',
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
// the client will not read the response anyways
|
|
|
|
return callback()
|
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
if (joinLeaveEpoch !== client.joinLeaveEpoch) {
|
|
|
|
// another joinDoc or leaveDoc rpc overtook us
|
|
|
|
return callback(new JoinLeaveEpochMismatchError())
|
|
|
|
}
|
|
|
|
// ensure the per-doc applied-ops channel is subscribed before sending the
|
|
|
|
// doc to the client, so that no events are missed.
|
2023-03-20 10:10:40 -04:00
|
|
|
RoomManager.joinDoc(client, docId, function (error) {
|
2021-07-13 07:04:45 -04:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (client.disconnected) {
|
|
|
|
metrics.inc('editor.join-doc.disconnected', 1, {
|
|
|
|
status: 'after-joining-room',
|
|
|
|
})
|
|
|
|
// the client will not read the response anyways
|
|
|
|
return callback()
|
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
DocumentUpdaterManager.getDocument(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2021-07-13 07:04:45 -04:00
|
|
|
fromVersion,
|
|
|
|
function (error, lines, version, ranges, ops) {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (client.disconnected) {
|
|
|
|
metrics.inc('editor.join-doc.disconnected', 1, {
|
|
|
|
status: 'after-doc-updater-call',
|
|
|
|
})
|
|
|
|
// the client will not read the response anyways
|
|
|
|
return callback()
|
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
if (isRestrictedUser && ranges && ranges.comments) {
|
2021-07-13 07:04:45 -04:00
|
|
|
ranges.comments = []
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2021-07-13 07:04:45 -04: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
|
|
|
|
const encodeForWebsockets = text =>
|
|
|
|
unescape(encodeURIComponent(text))
|
|
|
|
const escapedLines = []
|
|
|
|
for (let line of lines) {
|
|
|
|
try {
|
|
|
|
line = encodeForWebsockets(line)
|
|
|
|
} catch (err) {
|
|
|
|
OError.tag(err, 'error encoding line uri component', { line })
|
|
|
|
return callback(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
escapedLines.push(line)
|
|
|
|
}
|
|
|
|
if (options.encodeRanges) {
|
|
|
|
try {
|
|
|
|
for (const comment of (ranges && ranges.comments) || []) {
|
|
|
|
if (comment.op.c) {
|
|
|
|
comment.op.c = encodeForWebsockets(comment.op.c)
|
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
for (const change of (ranges && ranges.changes) || []) {
|
|
|
|
if (change.op.i) {
|
|
|
|
change.op.i = encodeForWebsockets(change.op.i)
|
|
|
|
}
|
|
|
|
if (change.op.d) {
|
|
|
|
change.op.d = encodeForWebsockets(change.op.d)
|
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
} catch (err) {
|
|
|
|
OError.tag(err, 'error encoding range uri component', {
|
|
|
|
ranges,
|
|
|
|
})
|
|
|
|
return callback(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
AuthorizationManager.addAccessToDoc(client, docId, () => {})
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2021-07-13 07:04:45 -04:00
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
userId,
|
|
|
|
projectId,
|
|
|
|
docId,
|
2021-07-13 07:04:45 -04:00
|
|
|
fromVersion,
|
2023-03-20 10:10:40 -04:00
|
|
|
clientId: client.id,
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
|
|
|
'client joined doc'
|
|
|
|
)
|
|
|
|
callback(null, escapedLines, version, ops, ranges)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
_assertClientAuthorization(client, docId, callback) {
|
2020-07-13 05:42:50 -04:00
|
|
|
// Check for project-level access first
|
|
|
|
AuthorizationManager.assertClientCanViewProject(client, function (error) {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
// Check for doc-level access next
|
|
|
|
AuthorizationManager.assertClientCanViewProjectAndDoc(
|
|
|
|
client,
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2020-07-13 05:42:50 -04:00
|
|
|
function (error) {
|
|
|
|
if (error) {
|
|
|
|
// No cached access, check docupdater
|
2023-03-20 10:10:40 -04:00
|
|
|
const { project_id: projectId } = client.ol_context
|
2021-07-13 07:04:45 -04:00
|
|
|
DocumentUpdaterManager.checkDocument(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2021-07-13 07:04:45 -04:00
|
|
|
function (error) {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
} else {
|
|
|
|
// Success
|
2023-03-20 10:10:40 -04:00
|
|
|
AuthorizationManager.addAccessToDoc(client, docId, callback)
|
2021-07-13 07:04:45 -04:00
|
|
|
}
|
2020-07-13 05:42:50 -04:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
)
|
2020-07-13 05:42:50 -04:00
|
|
|
} else {
|
|
|
|
// Access already cached
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
leaveDoc(client, docId, callback) {
|
2020-06-23 13:29:44 -04:00
|
|
|
// client may have disconnected, but we have to cleanup internal state.
|
2020-07-13 05:42:50 -04:00
|
|
|
client.joinLeaveEpoch++
|
2021-05-10 05:16:50 -04:00
|
|
|
metrics.inc('editor.leave-doc', 1, { status: client.transport })
|
2023-03-20 10:10:40 -04:00
|
|
|
const { project_id: projectId, user_id: userId } = client.ol_context
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ userId, projectId, docId, clientId: client.id },
|
2020-06-23 13:29:44 -04:00
|
|
|
'client leaving doc'
|
|
|
|
)
|
2023-03-20 10:10:40 -04:00
|
|
|
RoomManager.leaveDoc(client, docId)
|
2020-06-23 13:29:44 -04: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
|
2020-07-07 06:06:02 -04:00
|
|
|
callback()
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
updateClientPosition(client, cursorData, callback) {
|
|
|
|
if (client.disconnected) {
|
|
|
|
// do not create a ghost entry in redis
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2021-05-10 05:16:50 -04:00
|
|
|
metrics.inc('editor.update-client-position', 0.1, {
|
2021-07-13 07:04:45 -04:00
|
|
|
status: client.transport,
|
2021-05-10 05:16:50 -04:00
|
|
|
})
|
2023-03-20 10:10:40 -04:00
|
|
|
const {
|
|
|
|
project_id: projectId,
|
|
|
|
first_name: firstName,
|
|
|
|
last_name: lastName,
|
|
|
|
email,
|
|
|
|
user_id: userId,
|
|
|
|
} = client.ol_context
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ userId, projectId, clientId: client.id, cursorData },
|
2020-06-23 13:29:44 -04:00
|
|
|
'updating client position'
|
|
|
|
)
|
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
AuthorizationManager.assertClientCanViewProjectAndDoc(
|
2020-06-23 13:29:44 -04:00
|
|
|
client,
|
|
|
|
cursorData.doc_id,
|
|
|
|
function (error) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (error) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ err: error, clientId: client.id, projectId, userId },
|
2020-06-23 13:29:44 -04:00
|
|
|
"silently ignoring unauthorized updateClientPosition. Client likely hasn't called joinProject yet."
|
|
|
|
)
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
cursorData.id = client.publicId
|
2023-03-20 10:10:40 -04:00
|
|
|
if (userId) {
|
|
|
|
cursorData.user_id = userId
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
if (email) {
|
2020-06-23 13:29:44 -04:00
|
|
|
cursorData.email = email
|
|
|
|
}
|
|
|
|
// Don't store anonymous users in redis to avoid influx
|
2023-03-20 10:10:40 -04:00
|
|
|
if (!userId || userId === 'anonymous-user') {
|
2020-06-23 13:29:44 -04:00
|
|
|
cursorData.name = ''
|
2020-06-29 13:11:33 -04:00
|
|
|
// consistent async behaviour
|
|
|
|
setTimeout(callback)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else {
|
|
|
|
cursorData.name =
|
2023-03-20 10:10:40 -04:00
|
|
|
firstName && lastName
|
|
|
|
? `${firstName} ${lastName}`
|
|
|
|
: firstName || lastName || ''
|
2020-06-23 13:29:44 -04:00
|
|
|
ConnectedUsersManager.updateUserPosition(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2020-06-23 13:29:44 -04:00
|
|
|
client.publicId,
|
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
first_name: firstName,
|
|
|
|
last_name: lastName,
|
2020-06-23 13:29:44 -04:00
|
|
|
email,
|
2023-03-20 10:10:40 -04:00
|
|
|
_id: userId,
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
row: cursorData.row,
|
|
|
|
column: cursorData.column,
|
2021-07-13 07:04:45 -04:00
|
|
|
doc_id: cursorData.doc_id,
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketLoadBalancer.emitToRoom(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2020-06-23 13:29:44 -04:00
|
|
|
'clientTracking.clientUpdated',
|
|
|
|
cursorData
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
CLIENT_REFRESH_DELAY: 1000,
|
|
|
|
getConnectedUsers(client, callback) {
|
|
|
|
if (client.disconnected) {
|
|
|
|
// they are not interested anymore, skip the redis lookups
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:51:26 -04:00
|
|
|
metrics.inc('editor.get-connected-users', { status: client.transport })
|
2023-03-20 10:10:40 -04:00
|
|
|
const {
|
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
|
|
|
is_restricted_user: isRestrictedUser,
|
|
|
|
} = client.ol_context
|
|
|
|
if (isRestrictedUser) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return callback(null, [])
|
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
if (!projectId) {
|
2020-08-20 08:07:27 -04:00
|
|
|
return callback(new NotJoinedError())
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ userId, projectId, clientId: client.id },
|
2020-06-23 13:29:44 -04:00
|
|
|
'getting connected users'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
AuthorizationManager.assertClientCanViewProject(client, function (error) {
|
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
WebsocketLoadBalancer.emitToRoom(projectId, 'clientTracking.refresh')
|
2020-07-07 06:06:02 -04:00
|
|
|
setTimeout(
|
2020-06-23 13:29:44 -04:00
|
|
|
() =>
|
2021-07-13 07:04:45 -04:00
|
|
|
ConnectedUsersManager.getConnectedUsers(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
2021-07-13 07:04:45 -04:00
|
|
|
function (error, users) {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ userId, projectId, clientId: client.id },
|
2021-07-13 07:04:45 -04:00
|
|
|
'got connected users'
|
|
|
|
)
|
|
|
|
callback(null, users)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
),
|
2020-06-23 13:29:44 -04:00
|
|
|
WebsocketController.CLIENT_REFRESH_DELAY
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
applyOtUpdate(client, docId, update, callback) {
|
2020-06-23 13:29:44 -04:00
|
|
|
// client may have disconnected, but we can submit their update to doc-updater anyways.
|
2023-03-20 10:10:40 -04:00
|
|
|
const { user_id: userId, project_id: projectId } = client.ol_context
|
|
|
|
if (!projectId) {
|
2020-08-20 08:07:27 -04:00
|
|
|
return callback(new NotJoinedError())
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketController._assertClientCanApplyUpdate(
|
2020-06-23 13:29:44 -04:00
|
|
|
client,
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2020-06-23 13:29:44 -04:00
|
|
|
update,
|
|
|
|
function (error) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
// Disconnect, but give the client the chance to receive the error
|
|
|
|
client.disconnect(),
|
|
|
|
100
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (!update.meta) {
|
|
|
|
update.meta = {}
|
|
|
|
}
|
|
|
|
update.meta.source = client.publicId
|
2023-03-20 10:10:40 -04:00
|
|
|
update.meta.user_id = userId
|
2021-05-10 05:16:50 -04:00
|
|
|
metrics.inc('editor.doc-update', 0.3, { status: client.transport })
|
2020-06-23 13:29:44 -04:00
|
|
|
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
userId,
|
|
|
|
docId,
|
|
|
|
projectId,
|
|
|
|
clientId: client.id,
|
2021-07-13 07:04:45 -04:00
|
|
|
version: update.v,
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'sending update to doc updater'
|
|
|
|
)
|
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
DocumentUpdaterManager.queueChange(
|
2023-03-20 10:10:40 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-06-23 13:29:44 -04:00
|
|
|
update,
|
|
|
|
function (error) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if ((error && error.message) === 'update is too large') {
|
2020-06-23 13:29:44 -04:00
|
|
|
metrics.inc('update_too_large')
|
2020-08-20 06:16:26 -04:00
|
|
|
const { updateSize } = error.info
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.warn(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ userId, projectId, docId, updateSize },
|
2020-06-23 13:29:44 -04:00
|
|
|
'update is too large'
|
|
|
|
)
|
|
|
|
|
|
|
|
// mark the update as received -- the client should not send it again!
|
|
|
|
callback()
|
|
|
|
|
|
|
|
// trigger an out-of-sync error
|
|
|
|
const message = {
|
2023-03-20 10:10:40 -04:00
|
|
|
project_id: projectId,
|
|
|
|
doc_id: docId,
|
2021-07-13 07:04:45 -04:00
|
|
|
error: 'update is too large',
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
setTimeout(function () {
|
|
|
|
if (client.disconnected) {
|
|
|
|
// skip the message broadcast, the client has moved on
|
|
|
|
return metrics.inc('editor.doc-update.disconnected', 1, {
|
2021-07-13 07:04:45 -04:00
|
|
|
status: 'at-otUpdateError',
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
client.emit('otUpdateError', message.error, message)
|
2020-07-07 06:06:02 -04:00
|
|
|
client.disconnect()
|
2020-06-23 13:29:44 -04:00
|
|
|
}, 100)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
if (error) {
|
2020-08-20 09:05:50 -04:00
|
|
|
OError.tag(error, 'document was not available for update', {
|
2021-07-13 07:04:45 -04:00
|
|
|
version: update.v,
|
2020-08-20 09:05:50 -04:00
|
|
|
})
|
2020-06-23 13:29:44 -04:00
|
|
|
client.disconnect()
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(error)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
_assertClientCanApplyUpdate(client, docId, update, callback) {
|
2020-07-07 06:06:02 -04:00
|
|
|
AuthorizationManager.assertClientCanEditProjectAndDoc(
|
2020-06-23 13:29:44 -04:00
|
|
|
client,
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2020-06-23 13:29:44 -04:00
|
|
|
function (error) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (
|
|
|
|
error &&
|
|
|
|
error.message === 'not authorized' &&
|
|
|
|
WebsocketController._isCommentUpdate(update)
|
|
|
|
) {
|
|
|
|
// This might be a comment op, which we only need read-only priveleges for
|
|
|
|
AuthorizationManager.assertClientCanViewProjectAndDoc(
|
|
|
|
client,
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2020-07-07 06:06:02 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
return
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(error)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
_isCommentUpdate(update) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (!(update && update.op instanceof Array)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for (const op of update.op) {
|
|
|
|
if (!op.c) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|