HttpController.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. async = require "async"
  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, callback = (error, client) ->) ->
  9. client_id = ioClient.id
  10. {project_id, user_id, first_name, last_name, email, connected_time} = ioClient.ol_context
  11. client = {client_id, project_id, user_id, first_name, last_name, email, connected_time}
  12. client.rooms = []
  13. for name, joined of ioClient.manager.roomClients[client_id]
  14. if joined and name != ""
  15. client.rooms.push name.replace(/^\//, "") # Remove leading /
  16. callback(null, client)
  17. getConnectedClients: (req, res, next) ->
  18. io = req.app.get("io")
  19. ioClients = io.sockets.clients()
  20. async.map ioClients, HttpController._getConnectedClientView, (error, clients) ->
  21. return next(error) if error?
  22. res.json clients
  23. getConnectedClient: (req, res, next) ->
  24. {client_id} = req.params
  25. io = req.app.get("io")
  26. ioClient = io.sockets.sockets[client_id]
  27. if !ioClient
  28. res.sendStatus(404)
  29. return
  30. HttpController._getConnectedClientView ioClient, (error, client) ->
  31. return next(error) if error?
  32. res.json client