HttpController.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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) {
  23. callback = function (error, client) {}
  24. }
  25. const client_id = ioClient.id
  26. const {
  27. project_id,
  28. user_id,
  29. first_name,
  30. last_name,
  31. email,
  32. connected_time
  33. } = ioClient.ol_context
  34. const client = {
  35. client_id,
  36. project_id,
  37. user_id,
  38. first_name,
  39. last_name,
  40. email,
  41. connected_time
  42. }
  43. client.rooms = []
  44. for (const name in ioClient.manager.roomClients[client_id]) {
  45. const joined = ioClient.manager.roomClients[client_id][name]
  46. if (joined && name !== '') {
  47. client.rooms.push(name.replace(/^\//, '')) // Remove leading /
  48. }
  49. }
  50. return callback(null, client)
  51. },
  52. getConnectedClients(req, res, next) {
  53. const io = req.app.get('io')
  54. const ioClients = io.sockets.clients()
  55. return async.map(
  56. ioClients,
  57. HttpController._getConnectedClientView,
  58. function (error, clients) {
  59. if (error != null) {
  60. return next(error)
  61. }
  62. return res.json(clients)
  63. }
  64. )
  65. },
  66. getConnectedClient(req, res, next) {
  67. const { client_id } = req.params
  68. const io = req.app.get('io')
  69. const ioClient = io.sockets.sockets[client_id]
  70. if (!ioClient) {
  71. res.sendStatus(404)
  72. return
  73. }
  74. return HttpController._getConnectedClientView(ioClient, function (
  75. error,
  76. client
  77. ) {
  78. if (error != null) {
  79. return next(error)
  80. }
  81. return res.json(client)
  82. })
  83. }
  84. }