HttpController.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let HttpController;
  14. const async = require("async");
  15. module.exports = (HttpController = {
  16. // The code in this controller is hard to unit test because of a lot of
  17. // dependencies on internal socket.io methods. It is not critical to the running
  18. // of ShareLaTeX, and is only used for getting stats about connected clients,
  19. // and for checking internal state in acceptance tests. The acceptances tests
  20. // should provide appropriate coverage.
  21. _getConnectedClientView(ioClient, callback) {
  22. if (callback == null) { callback = function(error, client) {}; }
  23. const client_id = ioClient.id;
  24. const {project_id, user_id, first_name, last_name, email, connected_time} = ioClient.ol_context;
  25. const client = {client_id, project_id, user_id, first_name, last_name, email, connected_time};
  26. client.rooms = [];
  27. for (const name in ioClient.manager.roomClients[client_id]) {
  28. const joined = ioClient.manager.roomClients[client_id][name];
  29. if (joined && (name !== "")) {
  30. client.rooms.push(name.replace(/^\//, "")); // Remove leading /
  31. }
  32. }
  33. return callback(null, client);
  34. },
  35. getConnectedClients(req, res, next) {
  36. const io = req.app.get("io");
  37. const ioClients = io.sockets.clients();
  38. return async.map(ioClients, HttpController._getConnectedClientView, function(error, clients) {
  39. if (error != null) { return next(error); }
  40. return res.json(clients);
  41. });
  42. },
  43. getConnectedClient(req, res, next) {
  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. return HttpController._getConnectedClientView(ioClient, function(error, client) {
  52. if (error != null) { return next(error); }
  53. return res.json(client);
  54. });
  55. }
  56. });