2020-07-07 10:06:02 +00:00
|
|
|
let HttpController
|
2020-06-23 17:29:44 +00:00
|
|
|
module.exports = HttpController = {
|
|
|
|
// The code in this controller is hard to unit test because of a lot of
|
|
|
|
// dependencies on internal socket.io methods. It is not critical to the running
|
|
|
|
// of ShareLaTeX, and is only used for getting stats about connected clients,
|
|
|
|
// and for checking internal state in acceptance tests. The acceptances tests
|
|
|
|
// should provide appropriate coverage.
|
2020-07-07 10:06:02 +00:00
|
|
|
_getConnectedClientView(ioClient) {
|
2023-03-20 14:10:40 +00:00
|
|
|
const clientId = ioClient.id
|
2020-06-23 17:29:44 +00:00
|
|
|
const {
|
2023-03-20 14:10:40 +00:00
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
|
|
|
first_name: firstName,
|
|
|
|
last_name: lastName,
|
2020-06-23 17:29:44 +00:00
|
|
|
email,
|
2023-03-20 14:10:40 +00:00
|
|
|
connected_time: connectedTime,
|
2020-06-23 17:29:44 +00:00
|
|
|
} = ioClient.ol_context
|
|
|
|
const client = {
|
2023-03-20 14:10:40 +00:00
|
|
|
client_id: clientId,
|
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
|
|
|
first_name: firstName,
|
|
|
|
last_name: lastName,
|
2020-06-23 17:29:44 +00:00
|
|
|
email,
|
2023-03-20 14:10:40 +00:00
|
|
|
connected_time: connectedTime,
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|
2023-03-20 14:10:40 +00:00
|
|
|
client.rooms = Object.keys(ioClient.manager.roomClients[clientId] || {})
|
2020-07-07 10:06:02 +00:00
|
|
|
// drop the namespace
|
2021-07-13 11:04:45 +00:00
|
|
|
.filter(room => room !== '')
|
2020-07-07 10:06:02 +00:00
|
|
|
// room names are composed as '<NAMESPACE>/<ROOM>' and the default
|
|
|
|
// namespace is empty (see comments in RoomManager), just drop the '/'
|
2021-07-13 11:04:45 +00:00
|
|
|
.map(fullRoomPath => fullRoomPath.slice(1))
|
2020-07-07 10:06:02 +00:00
|
|
|
return client
|
2020-06-23 17:29:44 +00:00
|
|
|
},
|
2014-11-13 11:48:49 +00:00
|
|
|
|
2020-07-07 10:06:02 +00:00
|
|
|
getConnectedClients(req, res) {
|
2020-06-23 17:29:44 +00:00
|
|
|
const io = req.app.get('io')
|
|
|
|
const ioClients = io.sockets.clients()
|
2020-07-07 10:06:02 +00:00
|
|
|
|
|
|
|
res.json(ioClients.map(HttpController._getConnectedClientView))
|
2020-06-23 17:29:44 +00:00
|
|
|
},
|
|
|
|
|
2020-07-07 10:06:02 +00:00
|
|
|
getConnectedClient(req, res) {
|
2023-03-20 14:10:40 +00:00
|
|
|
const { client_id: clientId } = req.params
|
2020-06-23 17:29:44 +00:00
|
|
|
const io = req.app.get('io')
|
2023-03-20 14:10:40 +00:00
|
|
|
const ioClient = io.sockets.sockets[clientId]
|
2020-06-23 17:29:44 +00:00
|
|
|
if (!ioClient) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2020-07-07 10:06:02 +00:00
|
|
|
res.json(HttpController._getConnectedClientView(ioClient))
|
2021-07-13 11:04:45 +00:00
|
|
|
},
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|