HttpController.coffee 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. Utils = require "./Utils"
  2. async = require "async"
  3. module.exports = HttpController =
  4. # The code in this controller is hard to unit test because of a lot of
  5. # dependencies on internal socket.io methods. It is not critical to the running
  6. # of ShareLaTeX, and is only used for getting stats about connected clients,
  7. # and for checking internal state in acceptance tests. The acceptances tests
  8. # should provide appropriate coverage.
  9. _getConnectedClientView: (ioClient, callback = (error, client) ->) ->
  10. client_id = ioClient.id
  11. Utils.getClientAttributes ioClient, [
  12. "project_id", "user_id", "first_name", "last_name", "email", "connected_time"
  13. ], (error, {project_id, user_id, first_name, last_name, email, connected_time}) ->
  14. return callback(error) if error?
  15. client = {client_id, project_id, user_id, first_name, last_name, email, connected_time}
  16. client.rooms = []
  17. for name, joined of ioClient.manager.roomClients[client_id]
  18. if joined and name != ""
  19. client.rooms.push name.replace(/^\//, "") # Remove leading /
  20. callback(null, client)
  21. getConnectedClients: (req, res, next) ->
  22. io = req.app.get("io")
  23. ioClients = io.sockets.clients()
  24. async.map ioClients, HttpController._getConnectedClientView, (error, clients) ->
  25. return next(error) if error?
  26. res.json clients
  27. getConnectedClient: (req, res, next) ->
  28. {client_id} = req.params
  29. io = req.app.get("io")
  30. ioClient = io.sockets.socket(client_id)
  31. HttpController._getConnectedClientView ioClient, (error, client) ->
  32. return next(error) if error?
  33. res.json client