HttpController.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. let HttpController
  2. module.exports = HttpController = {
  3. // The code in this controller is hard to unit test because of a lot of
  4. // dependencies on internal socket.io methods. It is not critical to the running
  5. // of ShareLaTeX, and is only used for getting stats about connected clients,
  6. // and for checking internal state in acceptance tests. The acceptances tests
  7. // should provide appropriate coverage.
  8. _getConnectedClientView(ioClient) {
  9. const clientId = ioClient.id
  10. const {
  11. project_id: projectId,
  12. user_id: userId,
  13. first_name: firstName,
  14. last_name: lastName,
  15. email,
  16. connected_time: connectedTime,
  17. } = ioClient.ol_context
  18. const client = {
  19. client_id: clientId,
  20. project_id: projectId,
  21. user_id: userId,
  22. first_name: firstName,
  23. last_name: lastName,
  24. email,
  25. connected_time: connectedTime,
  26. }
  27. client.rooms = Object.keys(ioClient.manager.roomClients[clientId] || {})
  28. // drop the namespace
  29. .filter(room => room !== '')
  30. // room names are composed as '<NAMESPACE>/<ROOM>' and the default
  31. // namespace is empty (see comments in RoomManager), just drop the '/'
  32. .map(fullRoomPath => fullRoomPath.slice(1))
  33. return client
  34. },
  35. getConnectedClients(req, res) {
  36. const io = req.app.get('io')
  37. const ioClients = io.sockets.clients()
  38. res.json(ioClients.map(HttpController._getConnectedClientView))
  39. },
  40. getConnectedClient(req, res) {
  41. const { client_id: clientId } = req.params
  42. const io = req.app.get('io')
  43. const ioClient = io.sockets.sockets[clientId]
  44. if (!ioClient) {
  45. res.sendStatus(404)
  46. return
  47. }
  48. res.json(HttpController._getConnectedClientView(ioClient))
  49. },
  50. }