HttpController.js 1.6 KB

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