mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
aa9d6c8dc9
* [misc] decaff cleanup: RoomManager * [misc] decaff cleanup: RedisClientManager * [misc] decaff cleanup: SafeJsonParse * [misc] decaff cleanup: WebApiManager * [misc] decaff cleanup: WebsocketController * [misc] decaff cleanup: WebsocketLoadBalancer * [misc] decaff cleanup: SessionSockets * [misc] decaff cleanup: HttpController * [misc] decaff cleanup: HttpApiController * [misc] decaff cleanup: HealthCheckManager * [misc] decaff cleanup: EventLogger * [misc] decaff cleanup: Errors o-error will eliminate most of it -- when we migrate over. * [misc] decaff cleanup: DrainManager * [misc] decaff cleanup: DocumentUpdaterManager * [misc] decaff cleanup: DocumentUpdaterController: no-unused-vars * [misc] decaff cleanup: DocumentUpdaterController: Array.from * [misc] decaff cleanup: DocumentUpdaterController: implicit return * [misc] decaff cleanup: DocumentUpdaterController: IIFE * [misc] decaff cleanup: DocumentUpdaterController: null checks * [misc] decaff cleanup: DocumentUpdaterController: simpler loops * [misc] decaff cleanup: DocumentUpdaterController: move module name def * [misc] decaff cleanup: ConnectedUsersManager: handle-callback-err * [misc] decaff cleanup: ConnectedUsersManager: implicit returns * [misc] decaff cleanup: ConnectedUsersManager: null checks * [misc] decaff cleanup: ChannelManager: no-unused-vars * [misc] decaff cleanup: ChannelManager: implicit returns * [misc] decaff cleanup: ChannelManager: other cleanup - var -> const - drop variable assignment before return * [misc] decaff cleanup: AuthorizationManager: handle-callback-err Note: This requires a change in WebsocketController to provide a dummy callback. * [misc] decaff cleanup: AuthorizationManager: Array.from * [misc] decaff cleanup: AuthorizationManager: implicit returns * [misc] decaff cleanup: AuthorizationManager: null checks * [misc] decaff cleanup: Router: handle-callback-err * [misc] decaff cleanup: Router: standard/no-callback-literal * [misc] decaff cleanup: Router: Array.from * [misc] decaff cleanup: Router: implicit returns * [misc] decaff cleanup: Router: refactor __guard__ wrapper * [misc] decaff cleanup: Router: null checks And a minor bug fix: user.id -> user._id * [misc] decaff cleanup: Router: move variable declarations to assignments * [misc] decaff cleanup: app: implicit returns * [misc] decaff cleanup: app: __guard__ * [misc] decaff cleanup: app: null checks * [misc] decaff cleanup: app: function definitions * [misc] decaff cleanup: app: drop unused next argument * [misc] decaff cleanup: app: var -> const
172 lines
5.1 KiB
JavaScript
172 lines
5.1 KiB
JavaScript
/* eslint-disable
|
|
camelcase,
|
|
*/
|
|
const Settings = require('settings-sharelatex')
|
|
const logger = require('logger-sharelatex')
|
|
const RedisClientManager = require('./RedisClientManager')
|
|
const SafeJsonParse = require('./SafeJsonParse')
|
|
const EventLogger = require('./EventLogger')
|
|
const HealthCheckManager = require('./HealthCheckManager')
|
|
const RoomManager = require('./RoomManager')
|
|
const ChannelManager = require('./ChannelManager')
|
|
const ConnectedUsersManager = require('./ConnectedUsersManager')
|
|
|
|
const RESTRICTED_USER_MESSAGE_TYPE_PASS_LIST = [
|
|
'connectionAccepted',
|
|
'otUpdateApplied',
|
|
'otUpdateError',
|
|
'joinDoc',
|
|
'reciveNewDoc',
|
|
'reciveNewFile',
|
|
'reciveNewFolder',
|
|
'removeEntity'
|
|
]
|
|
|
|
let WebsocketLoadBalancer
|
|
module.exports = WebsocketLoadBalancer = {
|
|
rclientPubList: RedisClientManager.createClientList(Settings.redis.pubsub),
|
|
rclientSubList: RedisClientManager.createClientList(Settings.redis.pubsub),
|
|
|
|
emitToRoom(room_id, message, ...payload) {
|
|
if (!room_id) {
|
|
logger.warn(
|
|
{ message, payload },
|
|
'no room_id provided, ignoring emitToRoom'
|
|
)
|
|
return
|
|
}
|
|
const data = JSON.stringify({
|
|
room_id,
|
|
message,
|
|
payload
|
|
})
|
|
logger.log(
|
|
{ room_id, message, payload, length: data.length },
|
|
'emitting to room'
|
|
)
|
|
|
|
this.rclientPubList.map((rclientPub) =>
|
|
ChannelManager.publish(rclientPub, 'editor-events', room_id, data)
|
|
)
|
|
},
|
|
|
|
emitToAll(message, ...payload) {
|
|
this.emitToRoom('all', message, ...payload)
|
|
},
|
|
|
|
listenForEditorEvents(io) {
|
|
logger.log(
|
|
{ rclients: this.rclientSubList.length },
|
|
'listening for editor events'
|
|
)
|
|
for (const rclientSub of this.rclientSubList) {
|
|
rclientSub.subscribe('editor-events')
|
|
rclientSub.on('message', function (channel, message) {
|
|
if (Settings.debugEvents > 0) {
|
|
EventLogger.debugEvent(channel, message)
|
|
}
|
|
WebsocketLoadBalancer._processEditorEvent(io, channel, message)
|
|
})
|
|
}
|
|
this.handleRoomUpdates(this.rclientSubList)
|
|
},
|
|
|
|
handleRoomUpdates(rclientSubList) {
|
|
const roomEvents = RoomManager.eventSource()
|
|
roomEvents.on('project-active', function (project_id) {
|
|
const subscribePromises = rclientSubList.map((rclient) =>
|
|
ChannelManager.subscribe(rclient, 'editor-events', project_id)
|
|
)
|
|
RoomManager.emitOnCompletion(
|
|
subscribePromises,
|
|
`project-subscribed-${project_id}`
|
|
)
|
|
})
|
|
roomEvents.on('project-empty', (project_id) =>
|
|
rclientSubList.map((rclient) =>
|
|
ChannelManager.unsubscribe(rclient, 'editor-events', project_id)
|
|
)
|
|
)
|
|
},
|
|
|
|
_processEditorEvent(io, channel, message) {
|
|
SafeJsonParse.parse(message, function (error, message) {
|
|
if (error) {
|
|
logger.error({ err: error, channel }, 'error parsing JSON')
|
|
return
|
|
}
|
|
if (message.room_id === 'all') {
|
|
io.sockets.emit(message.message, ...message.payload)
|
|
} else if (
|
|
message.message === 'clientTracking.refresh' &&
|
|
message.room_id
|
|
) {
|
|
const clientList = io.sockets.clients(message.room_id)
|
|
logger.log(
|
|
{
|
|
channel,
|
|
message: message.message,
|
|
room_id: message.room_id,
|
|
message_id: message._id,
|
|
socketIoClients: clientList.map((client) => client.id)
|
|
},
|
|
'refreshing client list'
|
|
)
|
|
for (const client of clientList) {
|
|
ConnectedUsersManager.refreshClient(message.room_id, client.publicId)
|
|
}
|
|
} else if (message.room_id) {
|
|
if (message._id && Settings.checkEventOrder) {
|
|
const status = EventLogger.checkEventOrder(
|
|
'editor-events',
|
|
message._id,
|
|
message
|
|
)
|
|
if (status === 'duplicate') {
|
|
return // skip duplicate events
|
|
}
|
|
}
|
|
|
|
const is_restricted_message = !RESTRICTED_USER_MESSAGE_TYPE_PASS_LIST.includes(
|
|
message.message
|
|
)
|
|
|
|
// send messages only to unique clients (due to duplicate entries in io.sockets.clients)
|
|
const clientList = io.sockets
|
|
.clients(message.room_id)
|
|
.filter(
|
|
(client) =>
|
|
!(is_restricted_message && client.ol_context.is_restricted_user)
|
|
)
|
|
|
|
// avoid unnecessary work if no clients are connected
|
|
if (clientList.length === 0) {
|
|
return
|
|
}
|
|
logger.log(
|
|
{
|
|
channel,
|
|
message: message.message,
|
|
room_id: message.room_id,
|
|
message_id: message._id,
|
|
socketIoClients: clientList.map((client) => client.id)
|
|
},
|
|
'distributing event to clients'
|
|
)
|
|
const seen = new Map()
|
|
for (const client of clientList) {
|
|
if (!seen.has(client.id)) {
|
|
seen.set(client.id, true)
|
|
client.emit(message.message, ...message.payload)
|
|
}
|
|
}
|
|
} else if (message.health_check) {
|
|
logger.debug(
|
|
{ message },
|
|
'got health check message in editor events channel'
|
|
)
|
|
HealthCheckManager.check(channel, message.key)
|
|
}
|
|
})
|
|
}
|
|
}
|