2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2014-11-13 06:48:49 -05:00
|
|
|
|
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) {
|
2020-06-23 13:29:44 -04:00
|
|
|
const client_id = ioClient.id
|
|
|
|
const {
|
|
|
|
project_id,
|
|
|
|
user_id,
|
|
|
|
first_name,
|
|
|
|
last_name,
|
|
|
|
email,
|
2021-07-13 07:04:45 -04:00
|
|
|
connected_time,
|
2020-06-23 13:29:44 -04:00
|
|
|
} = ioClient.ol_context
|
|
|
|
const client = {
|
|
|
|
client_id,
|
|
|
|
project_id,
|
|
|
|
user_id,
|
|
|
|
first_name,
|
|
|
|
last_name,
|
|
|
|
email,
|
2021-07-13 07:04:45 -04:00
|
|
|
connected_time,
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
client.rooms = Object.keys(ioClient.manager.roomClients[client_id] || {})
|
|
|
|
// 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) {
|
2020-06-23 13:29:44 -04:00
|
|
|
const { client_id } = req.params
|
|
|
|
const io = req.app.get('io')
|
|
|
|
const ioClient = io.sockets.sockets[client_id]
|
|
|
|
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
|
|
|
}
|