2020-07-07 06:06:02 -04:00
|
|
|
let HttpController
|
2020-06-23 13:29:44 -04: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 06:06:02 -04:00
|
|
|
_getConnectedClientView(ioClient) {
|
2023-03-20 10:10:40 -04:00
|
|
|
const clientId = ioClient.id
|
2020-06-23 13:29:44 -04:00
|
|
|
const {
|
2023-03-20 10:10:40 -04:00
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
|
|
|
first_name: firstName,
|
|
|
|
last_name: lastName,
|
2020-06-23 13:29:44 -04:00
|
|
|
email,
|
2023-03-20 10:10:40 -04:00
|
|
|
connected_time: connectedTime,
|
2020-06-23 13:29:44 -04:00
|
|
|
} = ioClient.ol_context
|
|
|
|
const client = {
|
2023-03-20 10:10:40 -04:00
|
|
|
client_id: clientId,
|
|
|
|
project_id: projectId,
|
|
|
|
user_id: userId,
|
|
|
|
first_name: firstName,
|
|
|
|
last_name: lastName,
|
2020-06-23 13:29:44 -04:00
|
|
|
email,
|
2023-03-20 10:10:40 -04:00
|
|
|
connected_time: connectedTime,
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2023-03-20 10:10:40 -04:00
|
|
|
client.rooms = Object.keys(ioClient.manager.roomClients[clientId] || {})
|
2020-07-07 06:06:02 -04:00
|
|
|
// drop the namespace
|
2021-07-13 07:04:45 -04:00
|
|
|
.filter(room => room !== '')
|
2020-07-07 06:06:02 -04:00
|
|
|
// room names are composed as '<NAMESPACE>/<ROOM>' and the default
|
|
|
|
// namespace is empty (see comments in RoomManager), just drop the '/'
|
2021-07-13 07:04:45 -04:00
|
|
|
.map(fullRoomPath => fullRoomPath.slice(1))
|
2020-07-07 06:06:02 -04:00
|
|
|
return client
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2014-11-13 06:48:49 -05:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
getConnectedClients(req, res) {
|
2020-06-23 13:29:44 -04:00
|
|
|
const io = req.app.get('io')
|
|
|
|
const ioClients = io.sockets.clients()
|
2020-07-07 06:06:02 -04:00
|
|
|
|
|
|
|
res.json(ioClients.map(HttpController._getConnectedClientView))
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
getConnectedClient(req, res) {
|
2023-03-20 10:10:40 -04:00
|
|
|
const { client_id: clientId } = req.params
|
2020-06-23 13:29:44 -04:00
|
|
|
const io = req.app.get('io')
|
2023-03-20 10:10:40 -04:00
|
|
|
const ioClient = io.sockets.sockets[clientId]
|
2020-06-23 13:29:44 -04:00
|
|
|
if (!ioClient) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
res.json(HttpController._getConnectedClientView(ioClient))
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|