WebsocketController.coffee 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. logger = require "logger-sharelatex"
  2. WebApiManager = require "./WebApiManager"
  3. AuthorizationManager = require "./AuthorizationManager"
  4. DocumentUpdaterManager = require "./DocumentUpdaterManager"
  5. ConnectedUsersManager = require "./ConnectedUsersManager"
  6. WebsocketLoadBalancer = require "./WebsocketLoadBalancer"
  7. Utils = require "./Utils"
  8. module.exports = WebsocketController =
  9. # If the protocol version changes when the client reconnects,
  10. # it will force a full refresh of the page. Useful for non-backwards
  11. # compatible protocol changes. Use only in extreme need.
  12. PROTOCOL_VERSION: 2
  13. joinProject: (client, user, project_id, callback = (error, project, privilegeLevel, protocolVersion) ->) ->
  14. user_id = user?._id
  15. logger.log {user_id, project_id, client_id: client.id}, "user joining project"
  16. WebApiManager.joinProject project_id, user_id, (error, project, privilegeLevel) ->
  17. return callback(error) if error?
  18. if !privilegeLevel or privilegeLevel == ""
  19. err = new Error("not authorized")
  20. logger.error {err, project_id, user_id, client_id: client.id}, "user is not authorized to join project"
  21. return callback(err)
  22. client.join project_id
  23. client.set("privilege_level", privilegeLevel)
  24. client.set("user_id", user_id)
  25. client.set("project_id", project_id)
  26. client.set("owner_id", project?.owner?._id)
  27. client.set("first_name", user?.first_name)
  28. client.set("last_name", user?.last_name)
  29. client.set("email", user?.email)
  30. client.set("connected_time", new Date())
  31. client.set("signup_date", user?.signUpDate)
  32. client.set("login_count", user?.loginCount)
  33. callback null, project, privilegeLevel, WebsocketController.PROTOCOL_VERSION
  34. # No need to block for setting the user as connected in the cursor tracking
  35. ConnectedUsersManager.updateUserPosition project_id, client.id, user, null, () ->
  36. joinDoc: (client, doc_id, fromVersion = -1, callback = (error, doclines, version, ops) ->) ->
  37. Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
  38. logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joining doc"
  39. AuthorizationManager.assertClientCanViewProject client, (error) ->
  40. return callback(error) if error?
  41. client.get "project_id", (error, project_id) ->
  42. return callback(error) if error?
  43. return callback(new Error("no project_id found on client")) if !project_id?
  44. DocumentUpdaterManager.getDocument project_id, doc_id, fromVersion, (error, lines, version, ops) ->
  45. return callback(error) if error?
  46. # Encode any binary bits of data so it can go via WebSockets
  47. # See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
  48. escapedLines = []
  49. for line in lines
  50. try
  51. line = unescape(encodeURIComponent(line))
  52. catch err
  53. logger.err {err, project_id, doc_id, fromVersion, line, client_id: client.id}, "error encoding line uri component"
  54. return callback(err)
  55. escapedLines.push line
  56. client.join(doc_id)
  57. callback null, escapedLines, version, ops
  58. leaveDoc: (client, doc_id, callback = (error) ->) ->
  59. Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
  60. logger.log {user_id, project_id, doc_id, client_id: client.id}, "client leaving doc"
  61. client.leave doc_id
  62. callback()
  63. updateClientPosition: (client, cursorData, callback = (error) ->) ->
  64. Utils.getClientAttributes client, [
  65. "project_id", "first_name", "last_name", "email", "user_id"
  66. ], (error, {project_id, first_name, last_name, email, user_id}) ->
  67. return callback(error) if error?
  68. logger.log {user_id, project_id, client_id: client.id, cursorData: cursorData}, "updating client position"
  69. cursorData.id = client.id
  70. cursorData.user_id = user_id if user_id?
  71. cursorData.email = email if email?
  72. if first_name? and last_name?
  73. cursorData.name = first_name + " " + last_name
  74. ConnectedUsersManager.updateUserPosition(project_id, client.id, {
  75. first_name: first_name,
  76. last_name: last_name,
  77. email: email,
  78. user_id: user_id
  79. }, {
  80. row: cursorData.row,
  81. column: cursorData.column,
  82. doc_id: cursorData.doc_id
  83. }, callback)
  84. else
  85. cursorData.name = "Anonymous"
  86. callback()
  87. WebsocketLoadBalancer.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData)
  88. getConnectedUsers: (client, callback = (error, users) ->) ->
  89. Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
  90. logger.log {user_id, project_id, client_id: client.id}, "getting connected users"
  91. AuthorizationManager.assertClientCanViewProject client, (error) ->
  92. return callback(error) if error?
  93. client.get "project_id", (error, project_id) ->
  94. return callback(error) if error?
  95. return callback(new Error("no project_id found on client")) if !project_id?
  96. ConnectedUsersManager.getConnectedUsers project_id, (error, users) ->
  97. return callback(error) if error?
  98. callback null, users
  99. applyOtUpdate: (client, doc_id, update, callback = (error) ->) ->
  100. AuthorizationManager.assertClientCanEditProject client, (error) ->
  101. if error?
  102. logger.error {err: error, doc_id, client_id: client.id, version: update.v}, "client is not authorized to make update"
  103. setTimeout () ->
  104. # Disconnect, but give the client the chance to receive the error
  105. client.disconnect()
  106. , 100
  107. return callback(error)
  108. Utils.getClientAttributes client, ["user_id", "project_id"], (error, {user_id, project_id}) ->
  109. return callback(error) if error?
  110. update.meta ||= {}
  111. update.meta.source = client.id
  112. update.meta.user_id = user_id
  113. # metrics.inc "editor.doc-update", 0.3
  114. # metrics.set "editor.active-projects", project_id, 0.3
  115. # metrics.set "editor.active-users", user_id, 0.3
  116. logger.log {user_id, doc_id, project_id, client_id: client.id, version: update.v}, "sending update to doc updater"
  117. DocumentUpdaterManager.queueChange project_id, doc_id, update, (error) ->
  118. if error?
  119. logger.error {err: error, project_id, doc_id, client_id: client.id, version: update.v}, "document was not available for update"
  120. client.disconnect()
  121. callback(error)