2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2021-07-12 12:47:18 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-06-23 13:29:44 -04:00
|
|
|
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')
|
2019-10-30 09:52:36 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
const RESTRICTED_USER_MESSAGE_TYPE_PASS_LIST = [
|
2020-06-23 13:29:44 -04:00
|
|
|
'connectionAccepted',
|
|
|
|
'otUpdateApplied',
|
|
|
|
'otUpdateError',
|
|
|
|
'joinDoc',
|
|
|
|
'reciveNewDoc',
|
|
|
|
'reciveNewFile',
|
|
|
|
'reciveNewFolder',
|
2021-07-13 07:04:45 -04:00
|
|
|
'removeEntity',
|
2020-06-23 13:29:44 -04:00
|
|
|
]
|
2019-10-04 08:41:49 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
let WebsocketLoadBalancer
|
2020-06-23 13:29:44 -04:00
|
|
|
module.exports = WebsocketLoadBalancer = {
|
|
|
|
rclientPubList: RedisClientManager.createClientList(Settings.redis.pubsub),
|
|
|
|
rclientSubList: RedisClientManager.createClientList(Settings.redis.pubsub),
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
emitToRoom(room_id, message, ...payload) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (!room_id) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.warn(
|
|
|
|
{ message, payload },
|
|
|
|
'no room_id provided, ignoring emitToRoom'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const data = JSON.stringify({
|
|
|
|
room_id,
|
|
|
|
message,
|
2021-07-13 07:04:45 -04:00
|
|
|
payload,
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
logger.log(
|
|
|
|
{ room_id, message, payload, length: data.length },
|
|
|
|
'emitting to room'
|
|
|
|
)
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
this.rclientPubList.map(rclientPub =>
|
2020-06-23 13:29:44 -04:00
|
|
|
ChannelManager.publish(rclientPub, 'editor-events', room_id, data)
|
|
|
|
)
|
|
|
|
},
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
emitToAll(message, ...payload) {
|
2020-07-07 06:06:02 -04:00
|
|
|
this.emitToRoom('all', message, ...payload)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
listenForEditorEvents(io) {
|
|
|
|
logger.log(
|
|
|
|
{ rclients: this.rclientSubList.length },
|
|
|
|
'listening for editor events'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
for (const rclientSub of this.rclientSubList) {
|
2020-06-23 13:29:44 -04:00
|
|
|
rclientSub.subscribe('editor-events')
|
|
|
|
rclientSub.on('message', function (channel, message) {
|
|
|
|
if (Settings.debugEvents > 0) {
|
|
|
|
EventLogger.debugEvent(channel, message)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
WebsocketLoadBalancer._processEditorEvent(io, channel, message)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
this.handleRoomUpdates(this.rclientSubList)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
handleRoomUpdates(rclientSubList) {
|
|
|
|
const roomEvents = RoomManager.eventSource()
|
|
|
|
roomEvents.on('project-active', function (project_id) {
|
2021-07-13 07:04:45 -04:00
|
|
|
const subscribePromises = rclientSubList.map(rclient =>
|
2020-06-23 13:29:44 -04:00
|
|
|
ChannelManager.subscribe(rclient, 'editor-events', project_id)
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
RoomManager.emitOnCompletion(
|
2020-06-23 13:29:44 -04:00
|
|
|
subscribePromises,
|
|
|
|
`project-subscribed-${project_id}`
|
|
|
|
)
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
roomEvents.on('project-empty', project_id =>
|
|
|
|
rclientSubList.map(rclient =>
|
2020-06-23 13:29:44 -04:00
|
|
|
ChannelManager.unsubscribe(rclient, 'editor-events', project_id)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
2014-11-13 11:03:37 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
_processEditorEvent(io, channel, message) {
|
2020-07-07 06:06:02 -04:00
|
|
|
SafeJsonParse.parse(message, function (error, message) {
|
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.error({ err: error, channel }, 'error parsing JSON')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (message.room_id === 'all') {
|
2020-07-07 06:06:02 -04:00
|
|
|
io.sockets.emit(message.message, ...message.payload)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else if (
|
|
|
|
message.message === 'clientTracking.refresh' &&
|
2020-07-07 06:06:02 -04:00
|
|
|
message.room_id
|
2020-06-23 13:29:44 -04:00
|
|
|
) {
|
2020-07-07 06:06:02 -04:00
|
|
|
const clientList = io.sockets.clients(message.room_id)
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.log(
|
|
|
|
{
|
|
|
|
channel,
|
|
|
|
message: message.message,
|
|
|
|
room_id: message.room_id,
|
|
|
|
message_id: message._id,
|
2021-07-13 07:04:45 -04:00
|
|
|
socketIoClients: clientList.map(client => client.id),
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'refreshing client list'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
for (const client of clientList) {
|
|
|
|
ConnectedUsersManager.refreshClient(message.room_id, client.publicId)
|
|
|
|
}
|
|
|
|
} else if (message.room_id) {
|
|
|
|
if (message._id && Settings.checkEventOrder) {
|
2020-06-23 13:29:44 -04:00
|
|
|
const status = EventLogger.checkEventOrder(
|
|
|
|
'editor-events',
|
|
|
|
message._id,
|
|
|
|
message
|
|
|
|
)
|
|
|
|
if (status === 'duplicate') {
|
|
|
|
return // skip duplicate events
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 08:39:34 -05:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
const is_restricted_message =
|
|
|
|
!RESTRICTED_USER_MESSAGE_TYPE_PASS_LIST.includes(message.message)
|
2020-02-24 08:39:34 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// send messages only to unique clients (due to duplicate entries in io.sockets.clients)
|
2020-07-07 06:06:02 -04:00
|
|
|
const clientList = io.sockets
|
2020-06-23 13:29:44 -04:00
|
|
|
.clients(message.room_id)
|
|
|
|
.filter(
|
2021-07-13 07:04:45 -04:00
|
|
|
client =>
|
2020-06-23 13:29:44 -04:00
|
|
|
!(is_restricted_message && client.ol_context.is_restricted_user)
|
|
|
|
)
|
2020-02-24 08:39:34 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// 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,
|
2021-07-13 07:04:45 -04:00
|
|
|
socketIoClients: clientList.map(client => client.id),
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'distributing event to clients'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
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)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
}
|
|
|
|
} else if (message.health_check) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.debug(
|
|
|
|
{ message },
|
|
|
|
'got health check message in editor events channel'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
HealthCheckManager.check(channel, message.key)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|